《数码管电子秒表程序.doc》由会员分享,可在线阅读,更多相关《数码管电子秒表程序.doc(9页珍藏版)》请在金锄头文库上搜索。
1、数码管电子秒表程序 8位数码管电子秒表,显示精确到0.01秒。使用共阳数码管,51单片机的P0口输出段码,P2口输出位码。晶振11.0592MHz。 控制按钮K1接P1.0口,单片机检测低电平。第一次按下时,启动开始计时;第二次按下时,停止计时、显示当前值;第三次按下时,显示归零,做好重新计时准备。完整源程序:#include #include unsigned char data dis_digit;unsigned char key_s, key_v;unsigned char code dis_code11=0xc0,0xf9,0xa4,0xb0,/ 0, 1, 2, 30x99,0x9
2、2,0x82,0xf8,0x80,0x90, 0xff;/ 4, 5, 6, 7, 8, 9, off unsigned char dis_buf8;/ 显示缓冲区unsigned char sec_bcd8; / 秒计数值, BCD码unsigned char dis_index;/ unsigned char key_times;/ K1 按下次数void clr_time();void update_disbuf();bitscan_key();void proc_key();void delayms(unsigned char ms);sbitK1 = P10;void main(vo
3、id)P0 = 0xff;P2 = 0xff;TMOD = 0x11;/ 定时器0, 1工作模式1, 16位定时方式TH1 = 0xdc;TL1 = 0;TH0 = 0xFC;TL0 = 0x17;clr_time();/ dis_digit = 0x7f;/ 初始显示P20口数码管dis_index = 0;/ key_times = 0;key_v = 0x01;IE = 0x8a;/ 使能timer0, timer1中断TR0 = 1;TR1 = 0;while(1)if(scan_key()delayms(10);if(scan_key()key_v = key_s;proc_key(
4、);void clr_time()sec_bcd0 = 0x0;sec_bcd1 = 0x0;sec_bcd2 = 0x0;sec_bcd3 = 0x0;sec_bcd4 = 0x0;sec_bcd5 = 0x0;sec_bcd6 = 0x0;sec_bcd7 = 0x0; update_disbuf();bit scan_key()key_s = 0x00;key_s |= K1;return(key_s key_v);void proc_key()if(key_v & 0x01) = 0)key_times+;if(key_times = 1)TR1 = 1;else if(key_tim
5、es = 2)TR1 = 0;elseclr_time();key_times = 0;void timer0() interrupt 1/ 定时器0中断服务程序, 用于数码管的动态扫描/ dis_index - 显示索引, 用于标识当前显示的数码管和缓冲区的偏移量/ dis_digit - 位选通值, 传送到P2口用于选通当前数码管的数值, 如等于0xfe时,/选通P2.0口数码管/ dis_buf - 显于缓冲区基地址TH0 = 0xFC;TL0 = 0x17;P2 = 0xff;/ 先关闭所有数码管P0 = dis_bufdis_index;/ 显示代码传送到P0口P2 = dis_di
6、git;/ dis_digit = _cror_(dis_digit,1);/ 位选通值右移(P20-P27), 下次中断时选通下一位数码管dis_index+;/ dis_index &= 0x07;/ 8个数码管全部扫描完一遍之后,再回到第一个开始下一次扫描void timer1() interrupt 3/unsigned char i;TH1 |= 0xdc;for(i = 0; i 8; i+)sec_bcdi+;/ 低位加1if(sec_bcdi 10)/ 如果低位满10则向高位进1break;/ 低位未满10sec_bcdi = 0;/ 低位满10清0update_disbuf(
7、);/ 更新显示缓冲区void update_disbuf()/ 更新显示缓冲区dis_buf0 = dis_codesec_bcd0;dis_buf1 = dis_codesec_bcd1;dis_buf2 = dis_codesec_bcd2 & 0x7f;/ 加上小数点dis_buf3 = dis_codesec_bcd3;dis_buf4 = dis_codesec_bcd4;dis_buf5 = dis_codesec_bcd5;dis_buf6 = dis_codesec_bcd6;dis_buf7 = dis_codesec_bcd7;void delayms(unsigned
8、char ms)/ 延时子程序unsigned char i;while(ms-)for(i = 0; i 120; i+);#include sbitksec=P30; sbitkmin=P31; sbitkhour=P32; sbitP33=P33; unsignedchar secshi=0,secge=0,minshi=0,minge=0,hourshi=0,hourge=0; unsignedintnum=0,sec=0,min=0,hour=0,x,n; unsignedcharcodetable10=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
9、0x80,0x90; voiddelay(unsignedint); voidkeyscan(); voiddisplay(); main() TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; TH1=(65536-50000)/256; TL1=(65536-50000)%256; EA=1;ET0=1;TR0=1;TR1=0;ET1=1; while(1) display(); keyscan(); voiddelay(unsignedintz) unsignedintx,y; for(x=z;x0;x-) for(y=110
10、;y0;y-); voidtime0()interrupt1 num+; TH1=(65536-50000)/256; TL1=(65536-50000)%256; voidtime1()interrupt3 n+; TH0=(65536-50000)/256; TL0=(65536-50000)%256; voiddisplay() if(min=0) TR1=1; else TR1=0; if(n10) n=0; P33=P33; if(num=20) num=0; sec+; if(sec=60) sec=0; min+; if(min=60) min=0; hour+; if(hour
11、=24) hour=0; min=0; sec=0; secge=sec%10; secshi=sec/10; minge=min%10; minshi=min/10; hourge=hour%10; hourshi=hour/10; P2=1; P0=tablesecge; delay(5); P2=2; P0=tablesecshi; delay(5); P2=4; P0=191; delay(5); P2=8; P0=tableminge; delay(5); P2=16; P0=tableminshi; delay(5); P2=32; P0=191; delay(5); P2=64; P0=tablehourge; delay(5); P2=128; P0=tablehourshi; delay(5); voidkeyscan() if(ksec=0) delay(10); if(ksec=0) sec+; if(sec=60) sec=0; while(ksec=0) display(); if(kmin=0) delay(10); if(kmin=0) min+; if(min=60) min=0; while(kmin=0) display(); if(khour=0) delay(10); if(khour=0) hour+; if(h