下面是程序的源代码:
/**
*功能:在LCD1602上显示当前时间(Time)和日期(Date)
* 自动计时,可计时年月日(包括平年和闰年)
* 可用按键调整时间和日期
* L *****************************
* C * Time: 00 Date: *
* D * 00:00 2013-06-05 *
* ******************************
*作者:徐冉
*日期:2013-06-05-19:10-22:50~2013-06-06-07:20-10:20
*备注:此程序已经通过调试,已做修改。耗时:5小时30分钟
**/
/****************AT89C52-RC MCU******************************/
/********************51hei单片机实验板****************************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P2^5;
sbit wela = P2^7;
sbit dula = P2^6;
sbit BUSY = P0^7;
sbit FM = P2^3;
uchar counter,tshi, tfen, tmiao;
uchar sshi, sge, fshi, fge, mshi, mge;
uint nian;
uchar yue, ri;
uchar nqian, nbai, nshi, nge, yshi, yge, rshi, rge;
uchar code time[] = "Time:";
uchar code date[] = "Date:";
//delay:xms
void delay(uint xms)
{
uchar i, j;
for(i = 0; i < xms; i++)
for(j = 0; j < 125; j++);
}
//wait:
void wait()
{
P0 = 0xff;
RS = 0;
RW = 1;
EN = 0;
EN = 1;
while(BUSY == 1);
EN = 0;
}
//write:cmd
void write_cmd(uchar cmd)
{
wait(); //写数据和命令时,必须进行判忙等待!!!
RS = 0;
RW = 0;
EN = 0;
P0 = cmd;
EN = 1;
EN = 0;
}
//write:data
void write_data(uchar dat)
{
wait();//写入数据之前必须进行判忙等待,否则数据将写入不成功!!!
RS = 1;
RW = 0;
EN = 0;
P0 = dat;
EN = 1;
EN = 0;
}
//LCD1602:init
void init()
{
nian = 2013;
yue = 6;
ri = 5;
wela = 0;
dula = 0;
P1 = 0xff;
delay(10);
write_cmd(0x38);
delay(5);
write_cmd(0x38);
delay(5);
write_cmd(0x38);
wait();
write_cmd(0x38);
wait();
write_cmd(0x0c);
wait();
write_cmd(0x06);
wait();
write_cmd(0x01);
}
//timer1:init
void timer1_init()
{
TMOD = 0x10;
TH1 = 0xB8;
TL1 = 0x00;
TR1 = 1;
EA = 1;
ET1 = 1;
}
//write:byteaddress
void write_pos(uchar add)
{
write_cmd(add | 0x80);
}
//write:string
void send_string(uchar *str)
{
while(*str != '