| 
 
| #include <reg52.h> unsigned char dispbuf[8];
 unsigned char temp[8];
 unsigned char T0count;
 unsigned char timecount;
 sbit EN=P2^0;
 sbit RS=P2^1;
 sbit RW=P2^2;
 bit flag;
 unsigned long x;
 unsigned int num;
 unsigned char i;
 void delay1ms(unsigned int ms)//延时1毫秒(不够精确的)
 {
 
 unsigned int i,j;
 
 for(i=0;i<ms;i++)
 
 for(j=0;j<100;j++);
 }
 void write_command(unsigned char command)//写指令//
 {
 delay1ms(1);
 
 RS=0;
 
 RW=0;
 
 EN=0;
 
 P0=command;
 delay1ms(1);
 
 EN=1;
 delay1ms(1);
 
 EN=0;
 }
 void write_data(unsigned char date)//写数据//
 {
 delay1ms(1);;
 
 RS=1;
 
 RW=0;
 
 EN=0;
 P0=date;
 delay1ms(1);
 EN=1;
 delay1ms(1);
 EN=0;
 }
 
 void init()
 {
 
 EN=0;
 
 write_command(0x38);
 
 write_command(0x0c);
 
 
 write_command(0x06);
 
 
 write_command(0x01);
 
 
 }
 void main(void)
 {
 
 
 TMOD=0x15;
 TH0=0;
 TL0=0;
 TH1=(65536-5000)/256;
 TL1=(65536-5000)%256;
 ET0=1;
 ET1=1;
 EA=1;
 init();
 write_command(0x80+0x04);
 TR1=1;
 TR0=1;
 while(1)
 {
 if(flag==1)
 {
 flag=0;
 x=T0count*65536+TH0*256+TL0;
 
 for(i=0;i<8;i++)
 {
 temp[i]=0;
 }
 i=0;
 while(x/10)
 {
 temp[i]=x%10;
 x=x/10;
 i++;
 };
 temp[i]=x;
 for(i=0;i<8;i++)
 {
 dispbuf[i]=temp[i];
 }
 timecount=0;
 T0count=0;
 TH0=0;
 TL0=0;
 TR0=1;
 }
 }
 }
 void t0(void) interrupt 1 using 0
 {
 T0count++;
 }
 void t1(void) interrupt 3 using 0
 {
 TH1=(65536-5000)/256;
 TL1=(65536-5000)%256;
 timecount++;
 if(timecount==200)
 {
 TR0=0;
 timecount=0;
 flag=1;
 
 }
 
 for(num=7;num<=0;num--)
 
 {
 
 write_data(dispbuf[num]);
 
 delay1ms(1);
 
 }
 }
 | 
 |