程序: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129#include"reg51.h"sbit RS = P3^0;sbit RW = P3^1;sbit E = P3^2;unsigned int count=0;unsigned int hour=0,min=0,sec=0;unsigned char str[]={"0123456789"};unsigned char str1[]={"Clock:"};void delay(unsigned int t){ int i=0,j=0; for(i=0;i<t;i++) for(j=0;j<=100;j++);}void writedat(unsigned char dat) //写入命令{ RS= 1; RW= 0; E = 0; P2=dat; delay(5); E=1; E=0; }void writecom(unsigned char com) //写入命令{ RS= 0; RW= 0; E = 0; P2=com; delay(5); E=1; E=0; E=1;}void display(){ int i=0; unsigned char temp0=0,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0; temp0=hour/10; temp1=hour%10; temp2=min/10; temp3=min%10; temp4=sec/10; temp5=sec%10; writecom(0x80); delay(5); while(str[i]!='\0') { writedat(str1[i]); delay(5); i++; } writecom(0x80+0x40+4); delay(5); writedat(str[temp0]); delay(5); writedat(str[temp1]); delay(5); writedat(':'); delay(5); writedat(str[temp2]); delay(5); writedat(str[temp3]); delay(5); writedat(':'); delay(5); writedat(str[temp4]); delay(5); writedat(str[temp5]); delay(5);}void initlcd() //初始化{ writecom(0x38); writecom(0x0c); writecom(0x06); writecom(0x01);}void inittimer(){ TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1; EA=1; TR0=1;}void main(){ initlcd(); inittimer(); while(1) { display(); }}void timer0_isr() interrupt 1{ TH0=(65536-50000)/256; //无法重装载 TL0=(65536-50000)%256; count++; if(count==20) { sec++; count=0; } if(sec==60) { min++; sec=0; } if(min==60) { hour++; min=0; } if(hour==24) { hour=0; }}