#include<reg52.h>sbit Key0=P1^0; //P1^0sbit Key1=P1^1;// P1^1sbit PWM=P1^7;unsigned char num=0,Duty=5;void Delay(unsigned int time){unsigned int i=0,j=0;for(i=0;i<time;i++)for(j=0;j<114;j++);}void Key_Scanf(void) {if(Key0==0){Delay(20);if(Key0==0)Duty-=1;while(!Key0);}if(Key1==0){Delay(20);if(Key1==0)Duty+=1;while(!Key1);}Duty=Duty>1?Duty:1;Duty=Duty<9?Duty:9;}void Timer0_Inits(void){TMOD|=0x01;TH0=(65535-10000)/256;TL0=(65535-10000)%256;TR0=1;ET0=1;EA=1;}void main(void){Timer0_Inits();while(1){Key_Scanf();}}void PIT0(void) interrupt 1{TH0=(65535-10000)/256;TL0=(65535-10000)%256;++num;if(num<=Duty)PWM=1;elsePWM=0;if(num>9)num=0; }
1 回答

牧羊人nacy
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
去抖動(dòng)。按鍵按下的時(shí)候,實(shí)際上接觸點(diǎn)有可能在不停的抖動(dòng),導(dǎo)致按鍵一開一閉,單片機(jī)會(huì)檢測(cè)到很多次的按鍵按下事件,因此延遲一段時(shí)間,在延遲這段時(shí)間,可以去除由于抖動(dòng)產(chǎn)生的影響。
- 1 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報(bào)
0/150
提交
取消