非阻塞式示例
- #include <REGX51.H>
- sbit KEY2 = P3^4;
- sbit KEY1 = P3^5;
- void Display(unsigned int num);
- unsigned int displaynumber = 0;
- unsigned char code smg_data[]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
- unsigned char KeyScan()
- {
- unsigned char Temp = 0;
- static bit Lock1 = 0, Lock2 = 0;
- if(KEY1 == 0)
- {
- if(Lock1 == 0)
- {
- Temp = 1;
- Lock1 = 1;
- }
- }
- else
- Lock1 = 0;
- if(KEY2 == 0)
- {
- if(Lock2 == 0)
- {
- Temp = 2;
- Lock2 = 1;
- }
- }
- else
- Lock2 = 0;
- return Temp;
- }
- void Display(unsigned int num)
- {
- static unsigned char i = 0;
- P2 = 0X08;
- switch (i)
- {
- case 0:
- P0 = smg_data[num %10];
- P2 = 0X88;
- break;
- case 1:
- P0 = smg_data[(num /10)%10];
- P2 = 0X48;
- break;
- case 2:
- P0 = smg_data[(num /100)%10];
- P2 = 0X28;
- break;
- case 3:
- P0 = smg_data[(num /1000)%10];
- P2 = 0X18;
- break;
- default:
- break;
- }
- i++;
- if(i>=4)i=0;
- }
- unsigned int Delay = 0;
- void main()
- {
- unsigned char key;
- while(1)
- {
- if(Delay++>=350)
- {
- Delay = 0;
- key = KeyScan();
- if(key == 1)
- {
- if(displaynumber<9999)
- displaynumber++;
- }
- else if(key == 2)
- {
- if(displaynumber>0)
- displaynumber--;
- }
- Display(displaynumber);
- }
- }
- }
复制代码
|