这个程序虽然非常简单,本着负责任的态度我还是用仿真软件验证无误才发出的。你说长按后,就像死机一样。我再次用TX-1C实验板验证,只改了端口定义,确定没有任何问题。你仔细检查你的硬件电路有没有问题。为防止直接发代码出错,用专用代码文字模式发。- #include <reg51.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit LED1=P1^0; //LED1端口定义
- sbit LED2=P1^1; //LED2端口定义
- sbit key =P3^4; //开关端口定义
- /****1ms延时子程序(11.0592MHz)****/
- void delayms(uint k)
- {
- uint i,j;
- for(i=k;i>0;i--)
- for(j=110;j>0;j--);
- }
- /******按键扫描程序*******/
- void keyscan()
- {
- static bit sign=0; //长按标志
- static uint count=0; //消抖、长短识别计数变量
-
- if(key==0) //检测按键如果按下
- {
- count++;
- if(count>500)
- count=501;
- if(count==500) //长按0.5s
- {
- sign=1; //长按标志置1
- LED2=~LED2;
- }
- }
- else //按键抬起
- {
- if(count>=10 && sign==0)//短按
- LED1=~LED1;
- count=0; //计数清0
- sign=0; //长按标志置清0
- }
- }
- void main()
- {
- while(1)
- {
- keyscan();
- //其它任务
- delayms(1);//控制主循环周期约1ms
- }
- }
复制代码
|