按键有效,判断即将进入case 1:
没有进入case:1 直接跳出switch
- #include "BS87D20A-3.h"
- #define com1 _pc1
- #define com2 _pc0
- #define com3 _pc2
- #define com4 _pc3
- #define key1 _pd2
- #define key2 _pd3
- #define key3 _pa1
- #define key4 _pa4
- //#define key_state_0 0 //按键的初始状态
- //#define key_state_1 1 //按键按下的状态
- //#define key_state_2 2 //按键释放的状态
- volatile bit Flag_T5ms;
- volatile bit Flag_T10ms;
- volatile bit Flag_T1s;
- const unsigned char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- unsigned char strtab[3];//缓冲区
- unsigned char key_number=0;
- unsigned char sec=0,min=0,hour=0;
- unsigned char temp;
- unsigned long s; //秒级
- unsigned long ms10;//毫秒
- unsigned long ms5;
- unsigned char n;
- enum key_states_e{
- key_s1,
- key_s2,
- key_s3,
- key_s4
- };
- void read_key()
- {
- static enum key_states_e key_state=key_s1;
- // static char key_state;
- static int press=0;
- unsigned char key_press; //按键是否被按下
- //key_state=0; //按键的状态
- key_press=key1; // 读按键I/O电
- switch (key_state)
- {
- case key_s1: // 按键初始态
- if (!key_press)
- {
- key_state=key_s2; // 键被按下,状态转换到键确认态
-
- }
- else key_state=key_s1;
- break;
-
- case key_s2: // 按键确认态
- if (!key_press)
- {
- key_state=key_s3;
- }
- else
- {
- key_state=key_s1; // 按键已抬起,转换到按键初始态
- }
- break;
-
- case key_s3:
- if (!key_press)
- {
- key_state=key_s3;//按键确认按下状态
-
- com4=~com4;
- _pb=tab[2];
- }
- else
- {
- key_state=key_s1;
- }
- case key_s4:
- if(key_press)
- {
- key_state=key_s1;
- }
- break;
- default: key_state=key_s1; press=0; break;
- }
- }
- void displaypro()
- {
- strtab[0]=tab[sec%10];
- strtab[1]=tab[sec/10];
- strtab[2]=tab[min%10];
- strtab[3]=tab[min/10];
- }
- void display_drive()
- {
- com1=1;com2=1;com3=1;com4=1;
- switch(temp)
- {
- case 0:com4=0;
- _pb=strtab[temp]; break;
- case 1:com3=0;
- _pb=strtab[temp]; break;
- case 2:com2=0;
- _pb=strtab[temp]; break;
- case 3:com1=0;
- _pb=strtab[temp]; break;
- default:break;
- }
- temp++;
- if(temp==4)
- temp=0;
- }
- void init()
- {
- _ctm0c0=0x20; // fh/16 2us
- _ctm0c1=0xc1; // 定时/计数器模式 比较器a
- _ctm0al=0xf4; // 1ms
- _ctm0ah=0x01;
-
- _pbc=0; //输出
- _pcc0=0;
- _pcc1=0;
- _pcc2=0;
- _pcc3=0;
-
- _pac1=1;
- _pac4=1;
- _pdc2=1;
- _pdc3=1;
- _pdpu3=1;
- _pdpu2=1;
- _papu1=1;
- _papu4=1;
- s=1000;
- ms10=10;
- ms5=5;
- temp=0;
- n=0;
- _mfe=1; //开多功能中断
- _ctm0ae=1; //CTM a中断使能
- _emi=1; //开总中断
- _ct0on=1; //开定时器
- com1=1;com2=1;com3=1;
- com4=1;
- }
- void main()
- {
- init();
- while(1)
- {
-
- if(Flag_T5ms)
- {
- Flag_T5ms=0;
-
- }
- if(Flag_T1s)
- {
- Flag_T1s=0;
- //displaypro();
- }
-
- if(Flag_T10ms) //10MS进入一次
- {
- Flag_T10ms=0;
- read_key();
- }
-
- GCC_CLRWDT();
- }
- }
-
- void __attribute__((interrupt(0x14))) ISR_CTMO(void)
- {
- _ctm0af=0;
- s--;
- ms5--;
- ms10--;
- if(! ms5)
- {
- ms5=5;//5MS 视觉暂留
- Flag_T5ms=1;
-
- }
- if(!ms10)
- {
- ms10=10;
- Flag_T10ms=1;
- }
- if(! s)
- {
- Flag_T1s=1;
- s=1000;//1s电子钟计数
- sec++;
- if(sec==60)
- {
- sec=0;
- min++;
- if(min==60)
- {
- min=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
复制代码 |