分类: 嵌入式
2011-07-11 10:20:45
/*
* IDE环境: TX2440A ADS v1.2
* 功能描述:按键控制LED(查询法)
* 硬件连接:KEY1 -- GPF4
KEY2 -- GPF5
KEY3 -- GPF6
KEY4 -- GPF7
维护记录:Lzy 2011-7-11 V1.0
*
*/
#define GPFCON (*(volatile unsigned int *)0x56000050)
#define GPFDAT (*(volatile unsigned int *)0x56000054)
#define GPFUP (*(volatile unsigned int *)0x56000058)
/*
* K1-K4对应GPF0、GPF1、GPF2、GPF3
*/
#define GPF0_out 01<<(2*0)
#define GPF1_out 01<<(2*1)
#define GPF2_out 01<<(2*2)
#define GPF3_out 01<<(2*3)
#define LED1ON 0xFE
#define LED2ON 0xFD
#define LED3ON 0xFB
#define LED4ON 0xF7
#define LEDOFF 0xff
/*
* KEY1-4对应GPF4、GPF5、GPF6、GPF7
*/
#define GPF4_in ~(11<<(2*4))
#define GPF5_in ~(11<<(2*5))
#define GPF6_in ~(11<<(2*6))
#define GPF7_in ~(11<<(2*7))
#define KEY1 0xE0
#define KEY2 0xD0
#define KEY3 0xB0
#define KEY4 0x70
void delay(void)
{
unsigned int i,j;
for(i = 0; i < 0xff; i++)
for(j = 0; j < 0xff; j++)
;
}
int Main(void)
{
GPFCON &= GPF4_in | GPF5_in | GPF6_in | GPF7_in; //KEY对应的引脚设为输入
GPFCON |= GPF0_out | GPF1_out | GPF2_out | GPF3_out; // LED1-LED4对应的4根引脚设为输出
GPFDAT = 0x0f; //GPF初始化
GPFUP = 0xFF00; //使能GPF上拉电阻
while(1)
{
switch(GPFDAT & 0xf0)
{
case KEY1: //KEY1按下,则点亮LED1
GPFDAT = LED1ON;
delay();
break;
case KEY2: //KEY1按下,则点亮LED1
GPFDAT = LED2ON;
delay();
break;
case KEY3: //KEY1按下,则点亮LED1
GPFDAT = LED3ON;
delay();
break;
case KEY4: //KEY1按下,则点亮LED1
GPFDAT = LED4ON;
delay();
break;
default :
GPFDAT = LEDOFF;
break;
}
}
return 0;
}