#include<reg51.h> #define uchar unsigned char #define uint unsigned int unsigned char code table []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char count=0,b; sbit key=P3^7; //定義按鍵 uchar ds_time = 0; uchar i; uchar t=0; void delay(uint i) //延時 { char j; for(i; i > 0; i--) for(j = 200; j > 0; j--); } void main() //主函數 { TMOD=0x01; //方式1 TH0=0x3c; // TL0=0xb0; ET0=1; //中斷開 //TR0=1; EA=1; //總中斷開 key=1; while(1){ if(key==0) //按鍵按下 { delay(10); TR0=~TR0; while(!key) //消抖 { b++; // delay(1000); if(b>100) { b=0; count=0; TR0=0; } } } if(ds_time==1){ ds_time=0; P0=table[count/10]; //十位 P2=table[count%10]; //個位 count++; //數碼管加1 } } } timer0() interrupt 1{ //中斷 t++; if(t==20){ t=0; ds_time=1; } TH0=0x3c; TL0=0xb0; }
|