51单片机独立按键抖动演示及软件消抖

程序参考郭天祥老师的视频

开发板普中科技 HC6800 V2.8

连接方式如图所示,按下按键K1,LED D1点亮,数码管DS1显示计数,说明抖动现象。

抖动演示:

 

#include #include // 此表为 LED 的字模,0~F unsigned char code LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; /*按键码表*/ sbit K1 = P0^0; sbit K2 = P0^1; sbit K3 = P0^2; sbit K4 = P0^3; sbit K5 = P0^4; sbit K6 = P0^5; /*LED码表*/ sbit L1=P1^0; sbit lock=P3^5;//12,storage register clock input sbit clk=P3^6;//11,shift register clock input sbit dat=P3^4;//14,serial data input /*将显示数据串行发送到74hc595*/ void sendTo(unsigned char k) { unsigned char i; for(i=0;i<8;i++) { dat=k&0x01; clk=0;//移位寄存器移位 _nop_(); _nop_(); clk=1; k>>=1;//发送数据右移一位 } } /*数码管显示*/ void nixieDisplay(unsigned char num); unsigned char count; /*********************************************************/ main() { count=0; while(1) { P1 = 0xff; if(K1==0) { L1 = 0; count++; if(count==10)count=0; } else L1= 1; nixieDisplay(count); } } /**********************************************************/ void nixieDisplay(unsigned char num) { lock=0; // 将字模送到14,serial data input sendTo(LED7Code[num]); //LED7 0x7f为小数点 共阴和共阳此处也是不一样; _nop_(); _nop_(); lock=1;//所存数据 } /**********************************************************/

 

消除抖动:

#include #include // 此表为 LED 的字模,0~F unsigned char code LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; /*按键码表*/ sbit K1 = P0^0; sbit K2 = P0^1; sbit K3 = P0^2; sbit K4 = P0^3; sbit K5 = P0^4; sbit K6 = P0^5; /*LED码表*/ sbit L1=P1^0; sbit lock=P3^5;//12,storage register clock input sbit clk=P3^6;//11,shift register clock input sbit dat=P3^4;//14,serial data input void sendTo(unsigned char k); /*数码管显示*/ void nixieDisplay(unsigned char num); unsigned char count; void delay(unsigned int i); /*********************************************************/ main() { count=0; while(1) { P1 = 0xff; if(K1==0) { delay(10); if(K1==0) { L1 = 0; count++; if(count==10)count=0; } while(!K1); delay(10); while(!K1); } else L1= 1; nixieDisplay(count); } } /**********************************************************/ void nixieDisplay(unsigned char num) { lock=0; // 将字模送到14,serial data input sendTo(LED7Code[num]); //LED7 0x7f为小数点 共阴和共阳此处也是不一样; _nop_(); _nop_(); lock=1;//所存数据 } /**********************************************************/ /*******延时函数*************/ void delay(unsigned int i) { unsigned char j; for(i; i > 0; i--) for(j = 255; j > 0; j--); } /*将显示数据串行发送到74hc595*/ void sendTo(unsigned char k) { unsigned char i; for(i=0;i<8;i++) { dat=k&0x01; clk=0;//移位寄存器移位 _nop_(); _nop_(); clk=1; k>>=1;//发送数据右移一位 } }

你可能感兴趣的:(51单片机独立按键抖动演示及软件消抖)