1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <reg51.h>
unsigned char code tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
unsigned char dspbuf[8]= {10,10,10,10,10,10,10,10}; unsigned char dspcom=0;
unsigned char flag_2ms=0;
unsigned int second=30,minute=15,hour=17;
void display();
void main() { TMOD |= 0x01; TH0 = (65536-2000)/256; TL0 = (65536-2000)/256; EA=1; ET0=1; TR0=1;
while(1) { if(flag_2ms == 1) { display(); flag_2ms=0; }
dspbuf[0] = hour/10; dspbuf[1] = hour%10;
dspbuf[2] = 11;
dspbuf[3] = minute/10; dspbuf[4] = minute%10;
dspbuf[5] = 11;
dspbuf[6] = second/10; dspbuf[7] = second%10; } }
void timer0_int() interrupt 1 {
static unsigned int counter=0; TH0=(65536-2000)/256; TL0=(65536-2000)/256;
counter++; flag_2ms=1; if(counter==500) { counter=0; second++; if(second==60) { second=0; minute++; if(minute==60) { minute=0; hour++; if(hour==24) { hour=0; } } } } }
void display() { P2=(P2&0x1f|0xe0); P0=tab[10]; P2=0x1f;
P2 = (P2 & 0x1f) | 0xc0; P0 = (0x01<<dspcom); P2 = P2 & 0x1f;
P2 = (P2 & 0x1f) | 0xe0; P0 = tab[dspbuf[dspcom]]; P2 = P2 & 0x1f;
dspcom++; if(dspcom == 8) { dspcom=0; } }
|