#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();
}
}
上一篇:单片机——红外遥控器——代码
下一篇:单片机—串口—代码
推荐阅读最新更新时间:2024-03-16 16:06
设计资源 培训 开发板 精华推荐
- 参会领开发板盲盒:2023 RT-Thread开发者大会报名啦!
- 有奖直播:ADI在中国能源互联网应用中的技术及产品 1月8日上午10:00-11:30 准时开启!
- 有奖直播|多款MSP430™片上Sigma-Delta ADC助力高精度信号检测应用 报名中
- 万人学习51单片机
- 100%获奖,EE新年福利!
- MSP430 多款开发工具优惠促销,总有一款会适合你!
- 直播报名:TI 用于感测应用、带可配置信号链元素的新型MSP430™ MCU,报名直播赢双重好礼!
- 人人都爱易电源——转发有礼!
- 推荐有礼,分享有你!DSP学习季开学了,推荐你最喜欢的TI DSP资料!
- 直播报名:TI 用于感测应用、带可配置信号链元素的新型MSP430™ MCU,报名直播赢双重好礼!