ds1302.h
#include "myhead.h"
#define ds1302_sec_add 0x80 //??êy?Yµ??·
#define ds1302_min_add 0x82 //·?êy?Yµ??·
#define ds1302_hr_add 0x84 //ê±êy?Yµ??·
#define ds1302_date_add 0x86 //è?êy?Yµ??·
#define ds1302_month_add 0x88 //??êy?Yµ??·
#define ds1302_day_add 0x8a //D??úêy?Yµ??·
#define ds1302_year_add 0x8c //?êêy?Yµ??·
#define ds1302_control_add 0x8e //????êy?Yµ??·
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
unsigned char timebuf[3] = {17,59,5};
unsigned char datebuf[3] = {16,8,17};
unsigned char daybuf = 3;
unsigned char writebuf[3] = {0};
unsigned char readbuf[3] = {0};
unsigned char disbuf[3] = {0};
unsigned char writebuf1[3] = {0};
unsigned char readbuf1[3] = {0};
unsigned char disbuf1[3] = {0};
unsigned char writebuf2 = 0;
unsigned char readbuf2 = 0;
unsigned char disbuf2 = 0;
// void ds1302_write_byte(unsigned char addr,unsigned char byte)
// {
// unsigned char i;
//
// addr = addr & 0xfe;
// SCK = 0;
// RST = 0;
// RST = 1;
// for(i = 0;i < 8;i++)
// {
// IO = addr & 0x01;
// SCK = 0;
// SCK = 1;
// addr >>= 1;
// }
//
// for(i = 0;i < 8;i++)
// {
// IO = byte & 0x01;
// SCK = 0;
// SCK = 1;
// byte >>= 1;
// }
//
// SCK = 0;
// RST = 0;
// }
unsigned char ds1302_read_byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
addr = addr & 0xfe;
SCK = 0;
RST = 0;
RST = 1;
addr = addr + 1;
for(i = 0;i < 8;i++)
{
IO = addr & 0x01;
SCK = 0;
SCK = 1;
addr >>= 1;
}
for(i = 0;i < 8;i++)
{
SCK = 1;
SCK = 0;
temp >>= 1;
if(IO)
{
temp += 0x80;
}
}
RST = 0;
return temp;
}
// void ds1302_write_time()
// {
// unsigned char i;
// unsigned char temp;
// unsigned char temp1;
//
// for(i = 0;i < 3;i++)
// {
// temp = timebuf[i] / 10;
// temp1 = timebuf[i] % 10;
// writebuf[i] = (temp << 4) | temp1;
// }
// ds1302_write_byte(ds1302_control_add,0x00);
// ds1302_write_byte(ds1302_hr_add,writebuf[0]);
// ds1302_write_byte(ds1302_min_add,writebuf[1]);
// ds1302_write_byte(ds1302_sec_add,writebuf[2]);
// ds1302_write_byte(ds1302_control_add,0x80);
// }
void ds1302_read_time()
{
unsigned char i;
unsigned char temp;
unsigned char temp1;
readbuf[0] = ds1302_read_byte(ds1302_hr_add);
readbuf[1] = ds1302_read_byte(ds1302_min_add);
readbuf[2] = ds1302_read_byte(ds1302_sec_add);
for(i = 0;i < 3;i++)
{
temp = readbuf[i] >> 4;
temp1 = readbuf[i] & 0x0f;
disbuf[i] = temp * 10 + temp1;
}
}
void lcd_dis_time1()
{
unsigned char lcdbuf[9] = {0};
lcdbuf[0] = (disbuf[0] / 10) + 0x30;
lcdbuf[1] = (disbuf[0] % 10) + 0x30;
lcdbuf[2] = ':';
lcdbuf[3] = (disbuf[1] / 10) + 0x30;
lcdbuf[4] = (disbuf[1] % 10) + 0x30;
lcdbuf[5] = ':';
lcdbuf[6] = (disbuf[2] / 10) + 0x30;
lcdbuf[7] = (disbuf[2] % 10) + 0x30;
lcd_dis_str(0,0,lcdbuf);
}
// void ds1302_write_day()
// {
// writebuf2 = daybuf % 10;
//
// ds1302_write_byte(ds1302_control_add,0x00);
// ds1302_write_byte(ds1302_day_add,writebuf2);
// ds1302_write_byte(ds1302_control_add,0x80);
// }
void ds1302_read_day()
{
readbuf2 = ds1302_read_byte(ds1302_day_add);
disbuf2 = readbuf2 & 0x0f;
}
void lcd_dis_day()
{
switch(disbuf2)
{
case 0x01:
{
lcd_dis_str(13,1,"Mon");
break;
}
case 0x02:
{
lcd_dis_str(13,1,"Tue");
break;
}
case 0x03:
{
lcd_dis_str(13,1,"Wed");
break;
}
case 0x04:
{
lcd_dis_str(13,1,"Thu");
break;
}
case 0x05:
{
lcd_dis_str(13,1,"Fri");
break;
}
case 0x06:
{
lcd_dis_str(13,1,"Sat");
break;
}
case 0x07:
{
lcd_dis_str(13,1,"Sun");
break;
}
}
}
// void ds1302_write_date()
// {
// unsigned char i;
// unsigned char temp;
// unsigned char temp1;
//
// for(i = 0;i < 3;i++)
// {
// temp = datebuf[i] / 10;
// temp1 = datebuf[i] % 10;
// writebuf1[i] = (temp << 4) | temp1;
// }
// ds1302_write_byte(ds1302_control_add,0x00);
// ds1302_write_byte(ds1302_year_add,writebuf1[0]);
// ds1302_write_byte(ds1302_month_add,writebuf1[1]);
// ds1302_write_byte(ds1302_date_add,writebuf1[2]);
// ds1302_write_byte(ds1302_control_add,0x80);
// }
void ds1302_read_date()
{
unsigned char i;
unsigned char temp;
unsigned char temp1;
readbuf1[0] = ds1302_read_byte(ds1302_year_add);
readbuf1[1] = ds1302_read_byte(ds1302_month_add);
readbuf1[2] = ds1302_read_byte(ds1302_date_add);
for(i = 0;i < 3;i++)
{
temp = readbuf1[i] >> 4;
temp1 = readbuf1[i] & 0x0f;
disbuf1[i] = temp * 10 + temp1;
}
}
void lcd_dis_date()
{
unsigned char lcdbuf[9] = {0};
lcdbuf[0] = (disbuf1[0] / 10) + 0x30;
lcdbuf[1] = (disbuf1[0] % 10) + 0x30;
lcdbuf[2] = '-';
lcdbuf[3] = (disbuf1[1] / 10) + 0x30;
lcdbuf[4] = (disbuf1[1] % 10) + 0x30;
lcdbuf[5] = '-';
lcdbuf[6] = (disbuf1[2] / 10) + 0x30;
lcdbuf[7] = (disbuf1[2] % 10) + 0x30;
lcd_dis_str(0,1,lcdbuf);
}
void ds1302()
{
unsigned char flag = 1;
unsigned char key;
lcd_init();
//ds1302_write_time();
//ds1302_write_date();
//ds1302_write_day();
while(flag)
{
ds1302_read_time();
ds1302_read_date();
ds1302_read_day();
lcd_dis_time1();
lcd_dis_date();
lcd_dis_day();
key = key_scan();
if(0xe7 == key)
{
lcd_init();
lcd_over();
flag = 0;
}
}
}
ds1302.h
#ifndef _DS1302_H
#define _DS1302_H
extern void ds1302_write_byte(unsigned char addr,unsigned char byte);
extern unsigned char ds1302_read_byte(unsigned char addr);
//extern void ds1302_write_time();
extern void ds1302_read_time();
extern void lcd_dis_time1();
//extern void ds1302_write_day();
extern void ds1302_read_day();
extern void ds1302_dis_day();
//extern void ds1302_write_date();
extern void ds1302_read_date();
extern void lcd_dis_date();
extern void ds1302();
#endif