#include<reg52.h>
#include<intrins.h>
#include"ds1302.h"
#include"delay.h"
#include"lcd1602.h"
uchar code table1[]="0123456789-:";
void ds1302_init()
{
write_ds1302(0x8e,0x00);//禁止写保护
write_ds1302(0x80,0x00);//写秒
write_ds1302(0x82,0x00);//写分
write_ds1302(0x84,0x00);//写时
write_ds1302(0x86,0x31);//写日
write_ds1302(0x88,0x08);//写月
write_ds1302(0x8a,0x02);//写周
write_ds1302(0x8c,0x12);//写年
write_ds1302(0x8e,0x80);//允许写保护
}
void main()
{
uchar t_sec,sec1,sec2;
uchar t_min,min1,min2;
uchar t_hr,hr1,hr2;
uchar t_mon,mon1,mon2;
uchar t_date,date1,date2;
uchar t_year,year1,year2;
uchar t_day,day1;
LCD_init();
delayus(5);
ds1302_init();
delayus(5);
while(1)
{
t_sec=read_ds1302(0x81);//读秒寄存器
sec1=t_sec&0x0f;
sec2=(t_sec>>4);
t_min=read_ds1302(0x83);//读分寄存器
min1=t_min&0x0f;
min2=(t_min>>4);
t_hr=read_ds1302(0x85);//读时寄存器
hr1=t_hr&0x0f;
hr2=(t_hr>>4);
t_date=read_ds1302(0x87);//读日寄存器
date1=t_date&0x0f;
date2=(t_date>>4);
t_mon=read_ds1302(0x89);//读月寄存器
mon1=t_mon&0x0f;
mon2=(t_mon>>4);
t_day=read_ds1302(0x8b);//读星期寄存器
day1=t_day&0x0f;
//day2=(t_day>>4);
t_year=read_ds1302(0x8d);//读年寄存器
year1=t_year&0x0f;
year2=(t_year>>4);
LCD_write_com(0x80);
LCD_write_Data(table1[2]);
LCD_write_com(0x81);
LCD_write_Data(table1[0]);
LCD_write_com(0x82);//写年寄存器
LCD_write_Data(table1[year2]);
LCD_write_com(0x83);
LCD_write_Data(table1[year1]);
LCD_write_com(0x84);
LCD_write_Data(table1[10]);
LCD_write_com(0x85);//写月寄存器
LCD_write_Data(table1[mon2]);
LCD_write_com(0x86);
LCD_write_Data(table1[mon1]);
LCD_write_com(0x87);
LCD_write_Data(table1[10]);
LCD_write_com(0x88);//写日寄存器
LCD_write_Data(table1[date2]);
LCD_write_com(0x89);
LCD_write_Data(table1[date1]);
LCD_write_com(0x80+0x40);//写时寄存器
LCD_write_Data(table1[hr2]);
LCD_write_com(0xc1);
LCD_write_Data(table1[hr1]);
LCD_write_com(0xc2);
LCD_write_Data(':');
LCD_write_com(0xc3);//写分寄存器
LCD_write_Data(table1[min2]);
LCD_write_com(0xc4);
LCD_write_Data(table1[min1]);
LCD_write_com(0xc5);
LCD_write_Data(table1[11]);
LCD_write_com(0xc6);//写秒寄存器
LCD_write_Data(table1[sec2]);
LCD_write_com(0xc7);
LCD_write_Data(table1[sec1]);
LCD_write_str(9,1,"week=");
LCD_write_com(0xce);//写星期寄存器
LCD_write_Data(table1[day1]);
}
} |