#include
#include <./lcd1302/lcd1302.h>
#include "intrins.h"
#include "./delay/delay.h"
#include "stdio.h"
bit ack = 0;
sbit ds = P1^0;
#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
sbit SCK = P2^2;
sbit IO = P2^1;
sbit RST = P2^0;
unsigned char timebuf[7] = {23,59,55,16,8,17,3};
unsigned char writebuf[7];
unsigned char readbuf[7];
unsigned char disbuf[7];
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;
}
}
unsigned char ds1302_read_byte(unsigned char addr)
{
unsigned char i;
unsigned char temp = 0;
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 temp;
unsigned char temp1;
unsigned char i;
for(i = 0; i < 7; i++)
{
temp = timebuf[i]/10;
temp1 = timebuf[i]%10;
disbuf[i] = (temp << 4) | temp1;
}
ds1302_write_byte(ds1302_control_add, 0x00);
ds1302_write_byte(ds1302_hr_add, disbuf[0]);
ds1302_write_byte(ds1302_min_add, disbuf[1]);
ds1302_write_byte(ds1302_sec_add, disbuf[2]);
ds1302_write_byte(ds1302_year_add, disbuf[3]);
ds1302_write_byte(ds1302_month_add, disbuf[4]);
ds1302_write_byte(ds1302_date_add, disbuf[5]);
ds1302_write_byte(ds1302_day_add, disbuf[6]);
ds1302_write_byte(ds1302_control_add, 0x80);
}
void ds1302_read_time()
{
unsigned char temp;
unsigned char temp1;
unsigned char i;
readbuf[0] = ds1302_read_byte(ds1302_hr_add);
readbuf[1] = ds1302_read_byte(ds1302_min_add);
readbuf[2] = ds1302_read_byte(ds1302_sec_add);
readbuf[3] = ds1302_read_byte(ds1302_year_add);
readbuf[4] = ds1302_read_byte(ds1302_month_add);
readbuf[5] = ds1302_read_byte(ds1302_date_add);
readbuf[6] = ds1302_read_byte(ds1302_day_add);
for(i = 0; i < 7; i++)
{
temp = readbuf[i] >> 4;
temp1 = readbuf[i] & 0x0f;
disbuf[i] = temp*10 + temp1;
}
}
void lcd_dis_time()
{
unsigned char lcddisbuf[9];
unsigned char datebuf[12];
unsigned char weekbuf[3] = {0};
lcddisbuf[0] = (disbuf[0]/10)+0x30;
lcddisbuf[1] = (disbuf[0]%10)+0x30;
lcddisbuf[2] = ':';
lcddisbuf[3] = (disbuf[1]/10)+0x30;
lcddisbuf[4] = (disbuf[1]%10)+0x30;
lcddisbuf[5] = ':';
lcddisbuf[6] = (disbuf[2]/10)+0x30;
lcddisbuf[7] = (disbuf[2]%10)+0x30;
lcddisbuf[8] = '\0';
lcd_str_display(0,0,lcddisbuf);
datebuf[0] = (disbuf[3]/10)+0x30;
datebuf[1] = (disbuf[3]%10)+0x30;
datebuf[2] = '-';
datebuf[3] = (disbuf[4]/10)+0x30;
datebuf[4] = (disbuf[4]%10)+0x30;
datebuf[5] = '-';
datebuf[6] = (disbuf[5]/10)+0x30;
datebuf[7] = (disbuf[5]%10)+0x30;
datebuf[8] = '\0';
lcd_str_display(2,1,datebuf);
weekbuf[0] = (disbuf[6]/10)+0x30;
weekbuf[1] = (disbuf[6]%10)+0x30;
if(weekbuf[1] == '1')
{
lcd_str_display(11,1,"Mon");
}
if(weekbuf[1] == '2')
{
lcd_str_display(11,1,"Tues");
}
if(weekbuf[1] == '3')
{
lcd_str_display(11,1,"Wed");
}
if(weekbuf[1] == '4')
{
lcd_str_display(11,1,"Thur");
}
if(weekbuf[1] == '5')
{
lcd_str_display(11,1,"FRI");
}
if(weekbuf[1] == '6')
{
lcd_str_display(11,1,"Sat");
}
if(weekbuf[1] == '7')
{
lcd_str_display(11,1,"SUN");
}
}
void ds_reset()
{
ds = 1;
ds = 0;
delay_us(200);
delay_us(200);
ds = 1;
delay_us(30);
if(ds == 0)
{
ack = 1;
}
else
{
ack = 0;
}
delay_us(200);
delay_us(100);
}
void ds_send_byte(unsigned char byte)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
ds = 0;
_nop_();
_nop_();
ds = byte & 0x01;
byte >>= 1;
delay_us(30);
ds = 1;
}
delay_us(30);
}
bit ds_read_bit()
{
bit temp;
ds = 1;
ds = 0;
_nop_();
_nop_();
ds = 1;
temp = ds;
delay_us(30);
return temp;
}
unsigned char ds_read_byte()
{
unsigned char i,j,k;
for(i = 0; i < 8; i++)
{
j = ds_read_bit();
k = (j << 7) | (k >> 1);
}
return k;
}
void main()
{
unsigned char a;
unsigned int b,temp;
float wendu;
unsigned char displaybuf[20];
lcd_init();
ds1302_write_time();
while(1)
{
ds1302_read_time();
lcd_dis_time();
ds_reset();
ds_send_byte(0xcc);
ds_send_byte(0x44);
ds_reset();
ds_send_byte(0xcc);
ds_send_byte(0xbe);
a = ds_read_byte();
b = ds_read_byte();
temp = (b << 8) | a;
wendu = (float)temp * 0.0625;
sprintf(displaybuf,"%7.3f",wendu);
lcd_str_display(9,0,displaybuf);
}
}