可以通过定时器编写万年历中的1时钟程序,十分简单
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar count0,count1;
- uchar temp0,temp1 = 0x7f,temp2,temp3;
- uchar code leddata[] = {0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
- void Delay100us() //@11.0592MHz
- {
- unsigned char i,j;
- _nop_();
- _nop_();
- i = 2;
- j = 15;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay500ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- _nop_();
- i = 22;
- j = 3;
- k = 227;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void display(uchar t)
- {
- uchar ge,shi;
- shi = t/10;
- ge = t%10;
- P2 = 0xe0;P0 =leddata[ge];P2 = 0x00;
- P2 = 0xc0;P0 = 0x80;P2 = 0x00;
- Delay100us();
- P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
- P2 = 0xc0;P0 = 0x40;P2 = 0x00;
- Delay100us();
- P2 = 0xc0;P0 = 0x00;P2 = 0x00;
- P2 = 0xe0;P0 =0xbf;P2 = 0x00;
- P2 = 0xc0;P0 = 0x20;P2 = 0x00;
- Delay100us();
- }
- void display1(uchar a)
- {
- uchar ge,shi;
- ge = a%10;
- shi = a/10;
- P2 = 0xe0;P0 = leddata[ge];P2 = 0x00;
- P2 = 0xc0;P0 = 0x10;P2 = 0x00;
- Delay100us();
- P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
- P2 = 0xc0;P0 = 0x08;P2 = 0x00;
- Delay100us();
- P2 = 0xc0;P0 = 0x00;P2 = 0x00;
- P2 = 0xe0;P0 = 0xbf;P2 = 0x00;
- P2 = 0xc0;P0 = 0x04;P2 = 0x00;
- Delay100us();
- }
- void display2(uchar c)
- {
- uchar ge,shi;
- ge = c%10;
- shi = c/10;
- P2 = 0xe0;P0 =leddata[ge];P2 = 0x00;
- P2 = 0xc0;P0 = 0x02;P2 = 0x00;
- Delay100us();
- P2 = 0xe0;P0 =leddata[shi];P2 = 0x00;
- P2 = 0xc0;P0 = 0x01;P2 = 0x00;
- Delay100us();
- }
- /*void init() //定时器内部中断0
- {
- TMOD = 0x11;
- TH1 = TH0 = 0X4B;
- TL1 = TH0 = 0XFC;
- ET1 = ET0 = 1;
- TR1 = TR0 = 1;
- EX0 = 1; //开启外部中断0
- IT0 = 0; //设置外部中断启动方式为低电平启动
- EA = 1;
- }*/
- #define KEYPORT P3
- uchar Trg;
- uchar Cont,d;
- void Key_Read( void )
- {
- unsigned char ReadData = KEYPORT^0xff;
- Trg = ReadData & (ReadData ^ Cont);
- Cont = ReadData;
- }
- void shijianshezhi()
- {
- Key_Read();
- if(Trg&0x08) //S4
- {
- TR0 = 0;
- }
- if(Trg&0x04) //S5
- {
- TR0 = 1;
- }
- }
- void shijianhanshu()
- {
- if(TF0 == 1)
- {
- count0++;
- TF0 = 0;
- TH0 = 0x4b;
- TL0 = 0Xfc;
- }
- if(count0 == 20)
- {
- count0 = 0;
- temp0++;
- }
- if(temp0 == 60)
- {
- temp2++;
- P2=0xa0;P0=0xff;P2=0X00;
- Delay500ms();
- P2=0xa0;P0=0x00;P2=0X00;
- temp0 = 0;
- }
- if(temp2 == 60)
- {
- temp3++;
- temp2 = 0;
- }
- display(temp0);
- display1(temp2);
- display2(temp3);
- }
- void main()
- {
- TMOD = 0X01;
- TH0 = 0x4b;
- TL0 = 0Xfc;
- while(1)
- {
- shijianhanshu();
- shijianshezhi();
- }
- }
复制代码
所有资料51hei提供下载:
定时器实现时钟的51程序.docx
(12.71 KB, 下载次数: 8)
|