#include "stdint.h"
// IO port for controling LEDs
#define GPM04_BASE 0xE0200340 // GPM04 Base Address
#define GPD0_BASE 0xE02000A0
#define GPCON_OFS 0x00 // Control Register Offset
#define GPDAT_OFS 0x04 // Data Register Offset
#define MP0_4CON_REG (*(vola
tile uint32_t *)(GPM04_BASE+GPCON_OFS))
#define MP0_4DAT_REG (*(volatile uint32_t *)(GPM04_BASE+GPDAT_OFS))
#define GPD0CON_REG (*(volatile uint32_t *)(GPD0_BASE+GPCON_OFS))
#define GPD0DAT_REG (*(volatile uint32_t *)(GPD0_BASE+GPDAT_OFS))
#define LED1 4 // GPM0_4->LED1
#define LED2 5 // GPM0_5->LED2
#define LED3 6 // GPM0_6->LED3
#define LED4 7 // GPM0_7->LED4
#define BEEP 0 // GPD0_0->BEEP
GPH0CON=(0x0<<31); //配置按键为输入
GPH0PUD=0X5555; //配置按键电平为上拉
GPH0DAT=0XFF;//11111111
GPH0DRV=0x0;
void Delay_ms(uint32_t Count)
{
//延时1ms,共延时nCountms
// Arm clock为400M,循环体每次30个Arm clock
int32_t temp1 = 13333;
int32_t temp2 = 0;
asm volatile (
"Delay_ms_0:n"
"mov %0, %2n"
"Delay_ms_1:n"
"subs %0, %0, #1n" // 单发射 cycle 1
// 跳转清流水线,以下指令均只用作填充流水线
"mov %1, %1n" // 双发射 cycle 1
"mov %1, %1n" // 单发射 cycle 2
"mov %1, %1n" // 单发射 cycle 3
"mov %1, %1n" // 单发射 cycle 4
"mov %1, %1n" // 单发射 cycle 5
"mov %1, %1n" // 单发射 cycle 6
"mov %1, %1n" // 单发射 cycle 7
"mov %1, %1n" // 单发射 cycle 8
"mov %1, %1n" // 单发射 cycle 9
"mov %1, %1n" // 单发射 cycle 10
"mov %1, %1n" // 单发射 cycle 11
"mov %1, %1n" // 单发射 cycle 12
"mov %1, %1n" // 单发射 cycle 13
"mov %1, %1n" // 单发射 cycle 14
"mov %1, %1n" // 单发射 cycle 15
"mov %1, %1n" // 单发射 cycle 16
"mov %0, %0n" // 单发射 cycle 17
"bne.w Delay_ms_1n" // 跳转会清流水线,13级流水线,cycle 30
"subs %1, %1, #1n" // 调用者确保nCount不为0
"bne.w Delay_ms_0n"
: "+r"(temp2), "+r"(Count): "r"(temp1): "cc"
);
}
void Gpio_LED1(uint8_t On)
{
if (On) {
MP0_4DAT_REG &= ~(1<
} else {
MP0_4DAT_REG |= (1<
}
}
void Gpio_LED2(uint8_t On)
{
if (On) {
MP0_4DAT_REG &= ~(1<
} else {
MP0_4DAT_REG |= (1<
}
}
void Gpio_LED3(uint8_t On)
{
if (On) {
MP0_4DAT_REG &= ~(1<
} else {
MP0_4DAT_REG |= (1<
}
}
void Gpio_LED4(uint8_t On)
{
if (On) {
MP0_4DAT_REG &= ~(1<
} else {
MP0_4DAT_REG |= (1<
}
}
void Gpio_Beep(uint8_t On)
{
if (On) {
GPD0DAT_REG |= (1<
} else {
GPD0DAT_REG &= ~(1<
}
}
void Gpio_Init(void)
{
// GPM04_4 output led1, off
MP0_4CON_REG = (MP0_4CON_REG & (~(0xf<<(LED1<<2)))) | (0x1<<(LED1<<2));
MP0_4DAT_REG |= (1<
// GPM04_5 output led2, off
MP0_4CON_REG = (MP0_4CON_REG & (~(0xf<<(LED2<<2)))) | (0x1<<(LED2<<2));
MP0_4DAT_REG |= (1<
// GPM04_6 output led3, off
MP0_4CON_REG = (MP0_4CON_REG & (~(0xf<<(LED3<<2)))) | (0x1<<(LED3<<2));
MP0_4DAT_REG |= (1<
// GPM04_7 output led4, off
MP0_4CON_REG = (MP0_4CON_REG & (~(0xf<<(LED4<<2)))) | (0x1<<(LED4<<2));
MP0_4DAT_REG |= (1<
// Beep output, off
GPD0CON_REG = (GPD0CON_REG & (~(0xf<<(BEEP<<2)))) | (0x1<<(BEEP<<2));
GPD0DAT_REG &= ~(1<
}
void main(void)
{
Gpio_Init();
while (1) {
Gpio_LED1(1);
//Delay_ms(1000);
//Gpio_LED1(0);
Gpio_LED2(1);
//Delay_ms(1000);
//Gpio_LED2(0);
if (GPH0DAT==0xF7) //k1按下 D1亮
Gpio_LED3(1);
//Delay_ms(1000);
//Gpio_LED3(0);
Gpio_LED4(0);
//Gpio_Beep(1);
//Delay_ms(1000);
Gpio_LED4(0);
Gpio_Beep(0);
}
}