📄 ds1307.c
字号:
#include "at8xc51snd1.h"
#include "system.h"
#include "twi.h"
#include "ds1307.h"
#include "lcd.h"
// ************************************************************ //
// *** This routine will read a byte from the DS1307 device *** //
// ************************************************************ //
uchar Read_DS1307(uchar addr)//addr表示要读取的地址
{
uchar i,temp,temp1=0;
Start();
Write_I2C_Byte(0xd0);
Write_I2C_Byte(addr);
Start();
Write_I2C_Byte(0xd1);
for(i = 0; i < 8; i++)
{
temp1 = temp1 << 1;
SDA=1;
SCL=1; // Set SCL High,Clock bit out
delayus();
if(SDA==1) temp1++;
SCL=0; // Clear SCL
delayus();
}
I2C_Clock();
Stop();
temp=((temp1&0xF0)>>4)*10+(temp1&0x0f);
return temp;
}
// *********************************************************** //
// *** This routine will write a byte to the DS1307 device *** //
// *********************************************************** //
void Write_DS1307(uchar addr,uchar dat)
{
Start();
Write_I2C_Byte(0xd0);
Write_I2C_Byte(addr);
Write_I2C_Byte(dat);
Stop();
}
//////////////////////////// 日期时钟获取 /////////////////////////////////////////
void rtc_get(void)
{ uchar temp;
second=Read_DS1307(0x00); //秒读取
if(second&0x80)
{ second=second&0x7f;
Write_DS1307(0,second);
}
minute=Read_DS1307(0x01); //分钟读取
temp =Read_DS1307(0x02); //小时读取
if(temp&0x40)
{ temp=temp&0xbf;
Write_DS1307(2,temp);
}
hour =Read_DS1307(0x02);
date =Read_DS1307(0x04); //日期读取
month =Read_DS1307(0x05); //月份读取
year =Read_DS1307(0x06); //年份读取
}
//////////////////////////// 日期时钟设置 ////////////////////////////////////////
void rtc_set(void)
{ Write_DS1307(0,0);//秒
Write_DS1307(1,(minute/10)<<4+(minute%10)&0x0f);//分
Write_DS1307(2,(hour/10)<<4+(hour%10)&0x0f); //时
Write_DS1307(4,(date/10)<<+(date%10)&0x0f); //日
Write_DS1307(5,(month/10)<<4+(month%10)&0x0f); //月
Write_DS1307(6,(year/10)<<4+(year%10)&0x0f); //年
}
void display_date(void)
{
//////////////////////////////日期显示///////////////////////////////
Sendlcd(0,0,18); //显示年份千位
Sendlcd(0,1,16); //显示年份百位
Sendlcd(0,2,year/10+16); //显示年份十位
Sendlcd(0,3,year%10+16); //显示年份个位
Sendlcd(0,4,96); //显示年
Sendlcd(0,5,month/10+16); //显示月份十位
Sendlcd(0,6,month%10+16); //显示月份个位
Sendlcd(0,7,97); //显示月
Sendlcd(0,8,date/10+16); //显示日期十位
Sendlcd(0,9,date%10+16); //显示日期个位
Sendlcd(0,10,98); //显示日
//////////////////////////////时钟显示////////////////////////////////
Sendlcd(0,11,hour/10+16); //显示小时十位
Sendlcd(0,12,hour%10+16); //显示小时个位
Sendlcd(0,13,26); //显示 :
Sendlcd(0,14,minute/10+16); //显示分钟十位
Sendlcd(0,15,minute%10+16); //显示分钟个位
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -