A9按键灯程序
按键灯
#define GPM4CON (*(volatile unsigned int *)0x110002E0)
#define GPM4DAT (*(volatile unsigned int *)0x110002E4)
#define GPX3CON (*(volatile unsigned int *)0x11000C60)
#define GPX3DAT (*(volatile unsigned int *)0x11000C64)
// 延时函数
void delay(unsigned long count)
{
volatile unsigned long i = count;
while (i--);
}
int main(void)
{
unsigned char keyValue = 0 ;
unsigned int flag_key = 0;
// 配置GPM4_0,GPM4_1,GPM4_2,GPM4_3为输出引脚,对应LED1/2/3/4
GPM4CON &= ~ ((0xf << 0) | (0xf << 4) | (0xf << 8) | (0xf << 12));
GPM4CON |= ((0x1 << 0) | (0x1<< 4) | (0x1 << 8) | (0x1<< 12));
// 设置K1、K2、K3、K4对应的GPIO(GPX3_2、GPX3_3、GPX3_4、GPX3_5)为输入
GPX3CON &= ~ ((0xf << 8) | (0xf << 12) | (0xf << 16) | (0xf << 20));
while(1)
{
//读按键引脚状态
keyValue = GPX3DAT;
keyValue = (keyValue & (0xf << 2)) >> 2;
if(keyValue == 0xf)
{
GPM4DAT = 0xffff;
}
if(keyValue != 0xf) //说明有按键被按下,再详细判断是哪个按键被按下
{
delay(1000); //按键消抖
keyValue = GPX3DAT;
keyValue = (keyValue & (0xf << 2)) >> 2;
/*********1234全亮**************************************************/
if(keyValue == 0xe)
{
GPM4DAT = 0x0;
delay(100000);
GPM4DAT = 0xf;
delay(100000);
}
else
{
GPM4DAT = 0xf;
}
/***********1、3同时亮灭************************************************/
if(keyValue == 0xd)
{
GPM4DAT = 0xa;
delay(1000000);
GPM4DAT = 0xf;
delay(1000000);
}
else
{
GPM4DAT = 0xf;
}
/**********2、4交叉亮灭*************************************************/
if(keyValue == 0xb)
{
GPM4DAT = 0x7;
GPM4DAT = 0xd;
delay(1000000);
GPM4DAT = 0xf;
GPM4DAT = 0xd;
GPM4DAT = 0x7;
delay(1000000);
GPM4DAT = 0xf;
delay(1000000);
}
else
{
GPM4DAT = 0xf;
}
/************4灯同时灭***********************************************/
if(keyValue == 0x7)
{
if(flag_key==0)flag_key=1;
else flag_key=0;
}
else
{
GPM4DAT = 0xf;
}
if(flag_key==1)
{
GPM4DAT = 0xffff;
}
}
}
return 0;
}