论坛» 嵌入式开发» MCU

按键程序

菜鸟
2013-04-19 20:55 1楼

#include

#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))

#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long

void delayms(uint xms);
uchar key_checkin;

void main(void)
{
WDTCTL=WDTPW+WDTHOLD; // Stop watchdog timer to prevent time out reset
P2SEL=0x00; //P2普通IO功能
P2DIR&=0x7f; // 01111111
P5SEL=0x00; //P2普通IO功能
P5DIR=0xff; // 11111111
while(1)
{
key_checkin=P2IN;
key_checkin&=0xf0;
if(key_checkin!=0xf0)
{
delayms(20);
key_checkin=P2IN;
key_checkin&=0xf0;
if(key_checkin!=0xf0)
{
while(key_checkin!=0xf0); //等待按键释放
P5OUT=0xff;
}
}
}
}

void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}

请问这个程序有什么错误,为何下载到板子上没有反应呢???

菜鸟
2013-04-19 20:57 2楼
补充一下:P5控制LED,P2.4~~~P2.5控制按键
助工
2013-04-22 14:54 3楼
LED是高电平点亮还是低电平点亮?
菜鸟
2013-04-23 08:59 4楼
if(key_checkin!=0xf0)
菜鸟
2013-04-23 18:55 5楼

楼主说的对,改好了,谢谢大家/

共5条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册]