|
這個(gè)示例程序適合樓主要求,要按實(shí)際電路重新定義端口。
- #include<reg51.h>
- sbit key1=P3^2;
- sbit key2=P3^3;
- sbit OUT1=P1^0;
- sbit OUT2=P1^1;
- unsigned char state=0;//按鍵操作狀態(tài)
- void key_scan()//按鍵掃描函數(shù)
- {
- static bit sign1=0,sign2=0;
- static unsigned int count1=0,count2=0;
-
- if(!key1)
- {
- if(++count1>=500 && sign1==0)//count1/count2消抖依主循環(huán)周期取值,估測(cè)10~20ms即可
- {
- sign1=1;
- state=1;
- }
- }
- else
- {
- if(sign1==1)
- {
- sign1=0;
- state=2;
- }
- count1=0;
- }
- if(!key2)
- {
- if(++count2>=500 && sign2==0)
- {
- sign2=1;
- state=3;
- }
- }
- else
- {
- if(sign2)
- {
- sign2=0;
- state=4;
- }
- count2=0;
- }
- }
- void key_service()//按鍵服務(wù)程序
- {
- switch(state)
- {
- case 1: OUT1=0; state=0; break;//任務(wù)完成state清0
- case 2: OUT1=1; state=0; break;
- case 3: OUT2=0; state=0; break;
- case 4: OUT2=1; state=0; break;
- }
- }
- void main()
- {
- while(1)
- {
- key_scan();
- key_service();
- }
- }
復(fù)制代碼 |
評(píng)分
-
查看全部評(píng)分
|