電路圖:程序?qū)崿F(xiàn)功能:
每次按下芯片右下角的按鍵,LED顯示按鍵按下的次數(shù),代碼如下:
#include
sbit P3_7=P3^7;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char count;
void delay(unsigned int time){
unsigned int j = 0;
for(;time>0;time--)
for(j=0;j<125;j++);
}
void main(void) {
count=0; //計(jì)數(shù)器賦初值
P0=table[count/10]; //P0口顯示初值
P2=table[count]; //P2口顯示初值
while(1) { //進(jìn)入無(wú)限循環(huán)
if(P3_7==0){ //軟件消抖,檢測(cè)按鍵是否壓下
delay(10);
if(P3_7==0) { //若按鍵壓下
count++; //計(jì)數(shù)器增1
if(count==100) //判斷循環(huán)是否超限
count=0;
P0=table[count/10]; //P0口輸出顯示
P2=table[count]; //P2口輸出顯示
while(P3_7==0); //等待按鍵松開(kāi),防止連續(xù)計(jì)數(shù)
}
}
}
}
評(píng)論