單片機(jī)定時器實現(xiàn)實時時鐘程序-lcd1602顯示
下面是程序的源代碼:
/**
*功能:在LCD1602上顯示當(dāng)前時間(Time)和日期(Date)
* 自動計時,可計時年月日(包括平年和閏年)
* 可用按鍵調(diào)整時間和日期
* 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
*備注:此程序已經(jīng)通過調(diào)試,已做修改。耗時:5小時30分鐘
**/
/****************AT89C52-RC MCU******************************/
/********************51hei單片機(jī)實驗板****************************/
#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(); //寫數(shù)據(jù)和命令時,必須進(jìn)行判忙等待?。?!
RS = 0;
RW = 0;
EN = 0;
P0 = cmd;
EN = 1;
EN = 0;
}
//write:data
void write_data(uchar dat)
{
wait();//寫入數(shù)據(jù)之前必須進(jìn)行判忙等待,否則數(shù)據(jù)將寫入不成功?。?!
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 !=