- #include <reg52.h>
- #define ufloat unsigned float
- #define uchar unsigned char
- #define uint unsigned int
- uchar LedOnCount, CONT, JJ;
- uint Flash_Delay, m, n;//<<<<<<<<<<<<<<<<<<<注意大于255的变数要用uint
- sbit SW = P1^0;
- sbit LED = P2^0; //输出口;
- bit key_In;
- static uchar f_SW=1; //按键松开标志
- static uchar f_long_SW=0; //长按动作标志,为1时,长按已动作,为0时长按未动作
- int main (void)
- {
- P2 = 0;
- P1 = 0xff;
- SW = 1;
- //key_In = 1;//<<<<<<<你还是不了解这按键标置位的用法
- while (1)
- {
- if(SW == 0)
- {
- f_SW=0; //按键按下标志
- if(++n>100)//<<<<<<<<<<<<<<<一个循环的时间太短,m一下子就加爆了,延时一下
- {
- n=0;
- m++;
- }
- }else{
- f_SW=1; //按键松开标志
- n=0;//<<<<<<<<<<<清零
- }
- if((f_SW==1)&&(m>10)&&(m<200)) //按键松开,并且m值处于10~200之间,为短按//<<<<<<<<<<<<<长按和短按的时间要有差别
- {
- m=0;
- CONT++;
- if(CONT == 2)CONT = 0 ;
- //if(CONT == 1)
- {
- }
- //if(CONT == 0) LED = 0;
- key_In = 1;
- }
- if(key_In == 1) //按键标置位bit
- {
- if(++Flash_Delay>1000) //闪灯延时int
- {
- Flash_Delay = 0;
- if(LED == 1) //闪灯脚sbit
- {
- LedOnCount++;//闪灯次数char
- LED = 0;
- } else
- {
- LED = 1;
- }
- if(LedOnCount == 2)
- {
- key_In = 0;
- LedOnCount = 0;
- }
- }
- }
- if(m>=500)&&(f_long_SW==0)
- {
- f_long_SW=1;
- {
- f_long_SW=0;//<<<<<<<<<<不明白这里想干啥
- m=0;
- }
- }
- }
- }
复制代码
|