本程序所用的原理图下载:http://www.51hei.com/f/HL-1V6.5.pdf,单片机芯片运用的stc89c52;找到1602液晶部分的原理图即可.这是一张单片机开发板的全体图.
下面是程序的源代码:
/**
*功用:在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 != )
{
write_data(*str++);
}
}
//dis:date
void dates()
{
nqian = nian / 1000 % 10;
nbai = nian / 100 % 10;
nshi = nian / 10 % 10;
nge = nian % 10;
yshi = yue / 10 % 10;
yge = yue % 10;
rshi = ri / 10 % 10;
rge = ri % 10;
write_pos(0x00);
send_string(time);
write_pos(0x09);
send_string(date);
write_pos(0x46);
write_data(nqian + 0x30);
write_pos(0x47);
write_data(nbai + 0x30);
write_pos(0x48);
write_data(nshi + 0x30);
write_pos(0x49);
write_data(nge + 0x30);
write_pos(0x4A);
write_data(-);
write_pos(0x4B);
write_data(yshi + 0x30);
write_pos(0x4C);
write_data(yge + 0x30);
write_pos(0x4D);
write_data(-);
write_pos(0x4E);
write_data(rshi + 0x30);
write_pos(0x4F);
write_data(rge + 0x30);
}
//dis:time
void times()
{
sshi = tshi / 10 % 10;
sge = tshi % 10;
fshi = tfen / 10 % 10;
fge = tfen % 10;
mshi = tmiao / 10 % 10;
mge = tmiao % 10;
write_pos(0x40);
write_data(sshi + 0x30);
write_pos(0x41);
write_data(sge + 0x30);
write_pos(0x43);
write_data(fshi + 0x30);
write_pos(0x44);
write_data(fge + 0x30);
write_pos(0x06);
write_data(mshi + 0x30);
write_pos(0x07);
write_data(mge + 0x30);
}
//keyscan:
void marixKeyscan()
{
uchar temp;
P3 = 0xfe;
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
delay(5);
temp = P3;
temp &= 0xf0;
if(temp != 0xf0)
{
FM = 0;
temp = P3;
switch(temp)
{
case 0xee: tshi++; if(tshi >= 24) tshi = 0; break;
case 0xde: tfen++; if(tfen >= 60) tfen = 0; break;
case 0xbe: nian++; if(nian >= 10000) nian = 0; break;
case 0x7e: yue++; if(yue >= 13) yue = 1; break;
}
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
delay(2);
while(temp != 0xf0)
{
temp = P3;
temp &= 0xf0;
}
FM = 1;
}
}