这是一个基于定时器的电子时钟,以前总是觉得数码管和LED挺简单的,谁知道组合起来还是有些困难的,但还是一一的解决了程序中的bug,新手也可以借鉴一下,感觉还挺有趣的。如果发现程序中有什么bug可以回帖相互探讨一下。共同学习,共同进步。
(程序和仿真都在附件的压缩包中,需要的可以下载一下。)
电路仿真图
单片机源程序如下:
- #include<reg52.H>
- #include<intrins.H>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code huayang1[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf}; //花样1
- uchar code huayang2[]={0x7f,0xfe,0xbf,0xfd,0xdf,0xfb,0xef,0xf7,0xef,0xfb,0xdf,0xfd,0xbf,0xfe}; //花样2
- uchar code huayang3[]={0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff}; //花样3
- uchar code huayang4[]={0x55,0xaa,0xcc,0x33,0x99,0x66,0x0f,0xf0}; //花样4
- uchar code Segcode[13]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff,0xbf, 0xC7};
- uchar Dispbut[8]={10,10,10,10,10,10,10,10};
- uchar Bitselect[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
- uchar Displayer[8]={0x00};
- uint i,a,b;
- sbit DU=P2^0;
- sbit WE=P2^1;
- #define Led_rod_DU 11
- uchar Sec=0,Min=30,Hour=12;
- void Display(void)
- {
- static uchar Num=0;
- DU=1;
- P0=0xff;
- DU=0;
-
- WE=1;
- P0=Bitselect[Num];
- WE=0;
- DU=1;
- P0=Segcode[Dispbut[Num]];
- DU=0;
-
- Num++;
- if(Num>=8)
- Num=0;
- }
- void Timer0_int()interrupt 1
- {
- TH0=(65536-5000)/256;
- TL0=(65536-5000)%256;
- Display();
- }
- void Timer1_int()interrupt 3
- {
- static uint Timer1_Count=0;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- Timer1_Count++;
- if(Timer1_Count>=20)
- {
- Timer1_Count=0;
- Sec++;
- if(Sec>59)
- {
- Sec=0;
- Min++;
- if(Min>59)
- {
- Min=0;
- Hour++;
- if(Hour>23)
- Hour=0;
- }
- }
- Dispbut[0]=Hour/10;
- Dispbut[1]=Hour%10;
- Dispbut[2]=Led_rod_DU;
- Dispbut[3]=Min/10;
- Dispbut[4]=Min%10;
- Dispbut[5]=Led_rod_DU;
- Dispbut[6]=Sec/10;
- Dispbut[7]=Sec%10;
- }
- }
- void delay(uint i)
- {
- while(i--)
- for(b=0;b<100;b++)
- ;
- }
- void liushui()
- {
- a=0xfe;
- for(i=0;i<8;i++)
- {
- P1=a;
- a=_crol_(a,1);
- delay(200);
- }
- a=0xfc;
- for(i=0;i<8;i++)
- {
- P1=a;
- a=_crol_(a,1);
- delay(200);
- }
- for(i=0;i<14;i++)
- {
- P1=huayang1[i];
- delay(200);
- }
- for(i=0;i<14;i++)
- {
- P1=huayang2[i];
- delay(200);
- }
- for(i=0;i<14;i++)
- {
- P1=huayang3[i];
- delay(200);
- }
- for(i=0;i<14;i++)
- {
- P1=huayang4[i];
- delay(200);
- }
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
1.zip
(66.3 KB, 下载次数: 72)
|