#include
#include "./lcd/lcd.h"
#define ds1302_sec_add 0x80
#define ds1302_min_add 0x82
#define ds1302_hr_add 0x84
#define ds1302_date_add 0x86
#define ds1302_month_add 0x88
#define ds1302_day_add 0x8a
#define ds1302_year_add 0x8c
#define ds1302_control_add 0x8e
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
sbit RST = P1^0;
sbit SCK = P1^1;
sbit IO = P1^2;
unsigned char timebuf[7] = {20,30,30,16,8,17,3};
unsigned char tempbuf[7];
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;
}
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 temp;
unsigned char temp1;
unsigned char i;
for(i = 0; i < 7; 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_year_add,writebuf[3]);
ds1302_write_byte(ds1302_month_add,writebuf[4]);
ds1302_write_byte(ds1302_date_add,writebuf[5]);
ds1302_write_byte(ds1302_day_add,writebuf[6]);
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);
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] = {0};
unsigned char lcddisdata[9] = {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;
lcddisdata[0] = (disbuf[3] / 10) + 0x30;
lcddisdata[1] = (disbuf[3] % 10) + 0x30;
lcddisdata[2] = '.';
lcddisdata[3] = (disbuf[4] / 10) + 0x30;
lcddisdata[4] = (disbuf[4] % 10) + 0x30;
lcddisdata[5] = '.';
lcddisdata[6] = (disbuf[5] / 10) + 0x30;
lcddisdata[7] = (disbuf[5] % 10) + 0x30;
lcd_dis_str(1,1,"20");
lcd_dis_str(1,3,lcddisdata);
lcd_dis_str(2,1,"TIME:");
lcd_dis_str(2,7,lcddisbuf);
switch(readbuf[6])
{
case 1:
{
lcd_dis_str(1,13,"MON");
break;
}
case 2:
{
lcd_dis_str(1,13,"TUE");
break;
}
case 3:
{
lcd_dis_str(1,13,"WED");
break;
}
case 4:
{
lcd_dis_str(1,13,"THU");
break;
}
case 5:
{
lcd_dis_str(1,13,"FRI");
break;
}
case 6:
{
lcd_dis_str(1,13,"SAT");
break;
}
case 7:
{
lcd_dis_str(1,13,"SUN");
break;
}
}
}
void main()
{
lcd_init();
ds1302_write_time();
while(1)
{
ds1302_read_time();
lcd_dis_time();
}
}
单片机——ds1302代码
最新推荐文章于 2022-05-20 20:47:04 发布