基于51单片机的跑表,秒表程序c语言程序
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar table[]=" 00:00:00:00 ";
- bit flag=0;
- sbit en=P2^0;
- sbit rs=P2^1;
- sbit s1=P1^0;
- sbit s2=P1^1;
- sbit bb=P1^2;
- uchar shi,fen,miao,biao,tt,num1,aa;
- void delay(uint z)
- {
- uint i,j;
- for(i=z;i>0;i--)
- for(j=110;j>0;j--);
- }
- void write_com(uchar com)
- {
- rs=0;
- P0=com;
- delay(10);
- en=1;
- delay(10);
- en=0;
- }
- void write_date(uchar date)
- {
- rs=1;
- P0=date;
- delay(10);
- en=1;
- delay(10);
- en=0;
- }
- void display(uchar com1,uchar date1)
- {
-
- uchar aa,bb;
- aa=date1/10;
- bb=date1%10;
- write_com(0x80+com1);
- write_date(0x30+aa);
- write_date(0x30+bb);
- }
- void init()
- {
- TMOD=0x01;
- ET0=1;
- TR0=0;
- EA=1;
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- en=0;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80+0x40);
- for(num1=0;num1<17;num1++)
- {
- write_date(table[num1]);
- delay(5);
- }
- }
- void keyscan()
- {
- if(s1==0)
- {
- delay(5);
- if(s1==0)
- {
- TR0=~TR0;
- }
- }
- }
- void display1()
- {
- display(0x40+3,shi);
- display(0x40+6,fen);
- display(0x40+9,miao);
- display(0x40+12,tt);
- }
- void main()
- {
- init();
- while(1)
- {
- keyscan();
- display1();
- }
- }
- void T0_time() interrupt 1
- {
- TH0=(65536-10000)/256;
- TL0=(65536-10000)%256;
- tt++;
- if(tt>99)
- {
- tt=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- }
- }
- }
- }
复制代码
|