- /****************************
- 独立按键,按一次按键,LED灯
- 左移一位
- ****************************/
- #include <reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit Key=P1^0;
- void delayms(uint z);
- void main()
- {
- P0=0xFE;
- Key=0x01;
- while(1)
- {
- if(!Key)
- {
- delayms(10); //延时10MS前沿消抖
- if(!Key)
- {
- P0=_crol_(P0,1);
- //while(!Key); //如果不加按键释放语句,LED灯会不断左移动直到释放按键,
- //人为按键时间要大于10MS,所以导致Key的状态多次检测。
- //同时,这个语句是消耗CPU的,最好用定时中断解决。
- //delayms(10); //后沿没有必要消抖,因为按键都弹上去了,除非按键损坏。
- }
- }
- }
- }
- void delayms(uint z) //延时约1MS
- {
- uint x,y;
- for(x=z;x>0;x--)
- {
- for(y=114;y>0;y--);
- }
- }
|