C51用定时器0做八位时钟原理图和代码
1 #include<reg52.h>
2 #define uchar unsigned char
3 #define uint unsigned int
4 uint i;
5 uint h=0,m=0,f=0,t4ms=0;
6 void init()
7 {
8 TMOD = 0x01;
9 TH0 = (65535-50000)/256;
10 TL0 = (65535-50000)%256;
11 ET0 = 1;
12 EA = 1;
13 TR0 = 1;
14 }
15 void delay(uint j){
16 while(j--);
17 }
18 uchar duan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
19 uint wei[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
20 void main()
21 {
22 init();
23 while(1)
24 {
25 for(i=0;i<8;i++)
26 {
27 P1=wei[i];
28 switch(i)
29 {
30 case 0:P2=duan[h/10];break;
31 case 1:P2=duan[h%10];break;
32 case 2:P2=0x40;break;
33 case 3:P2=duan[f/10];break;
34 case 4:P2=duan[f%10];break;
35 case 5:P2=0x40;break;
36 case 6:P2=duan[m/10];break;
37 case 7:P2=duan[m%10];break;
38 default:break;
39 }
40 delay(30);
41 }
42 }
43 }
44 void ljj() interrupt 1
45 {
46 t4ms++;
47 if(t4ms==13)
48 {
49 t4ms=0;
50 m++;
51 if(m==60)
52 {
53 m=0;
54 f++;
55 if(f==60)
56 {
57 f=0;
58 h++;
59 if(h==12)
60 {
61 h=0;
62 }
63 }
64 }
65 }
66
67 }
68