📄 ds1307.c
字号:
#include <stdio.h>
#include <ctype.h>
#include "standard.h"
#include "i2c.h"
#include "ds1307.h"
void Start_SEC_Timer(Data);
void Start_mSEC_Timer(Data);
// DS1307 address
#define I2C_DS1307_ID 0xD0
DS1307 xdata Ds1307;
u8 xdata nvram[56];
//#if 0
void I2C_Write_DS1307(u8 address,u8 content)
{
I2C_Send_Start(); // Generate START condition
// Send SLAVE address with write request
if (I2C_Write_Byte(I2C_DS1307_ID | I2C_WRITE))
{
printf("DS1307 Error Wr address (slave doesn't ack)\n");
return;
}
// Send DS1307 internal address
if (I2C_Write_Byte(address))
{
printf("DS1307 Error Wr content (slave doesn't ack)\n");
return;
}
// Send content to memory address
if (I2C_Write_Byte(content))
{
printf("DS1307 Error Wr content (slave doesn't ack)\n");
return;
}
I2C_Send_Stop();
return;
}
void I2C_Read_DS1307(u8 address,u8* content)
{
I2C_Send_Start(); // Generate START condition
// Send SLAVE address with write request
if (I2C_Write_Byte(I2C_DS1307_ID | I2C_WRITE))
{
printf("DS1307 Error Wr address (slave doesn't ack)\n");
return;
}
// Send DS1307 internal address
if (I2C_Write_Byte(address))
{
printf("DS1307 Error Wr content (slave doesn't ack)\n");
return;
}
I2C_Send_Stop();
Start_mSEC_Timer(5);
I2C_Send_Start(); // Generate START condition
// Send SLAVE address with read request
if (I2C_Write_Byte(I2C_DS1307_ID | I2C_READ))
{
printf("DS1307 Error Rd address (slave doesn't ack)\n");
return;
}
*content = I2C_Read_Byte(); // Read switch content
I2C_Send_Master_NAck();
I2C_Send_Stop();
}
void setRtc(void)
{
I2C_Write_DS1307(0,Ds1307.sec);
// printf("Sec=%bx\n",Ds1307.sec);
I2C_Write_DS1307(1,Ds1307.min);
// printf("Min=%bx\n",Ds1307.min);
I2C_Write_DS1307(2,Ds1307.hour);
// printf("Hour=%bx\n",Ds1307.hour);
I2C_Write_DS1307(3,Ds1307.day);
// printf("Day=%bx\n",Ds1307.day);
I2C_Write_DS1307(4,Ds1307.date);
// printf("Date=%bx\n",Ds1307.date);
I2C_Write_DS1307(5,Ds1307.month);
// printf("Month=%bx\n",Ds1307.month);
I2C_Write_DS1307(6,Ds1307.year);
// printf("Year=%bx\n",Ds1307.year);
I2C_Write_DS1307(7,Ds1307.sqw);
}
void readRtc(void)
{
// u8 bcd2dec;
I2C_Read_DS1307(0,&Ds1307.sec);
I2C_Read_DS1307(1,&Ds1307.min);
I2C_Read_DS1307(2,&Ds1307.hour);
I2C_Read_DS1307(3,&Ds1307.day);
I2C_Read_DS1307(4,&Ds1307.date);
I2C_Read_DS1307(5,&Ds1307.month);
I2C_Read_DS1307(6,&Ds1307.year);
#if 0
bcd2dec = (((Ds1307.sec&0x70)>>4)*10)+Ds1307.sec&0x0f;
Ds1307.sec=bcd2dec;
bcd2dec = (((Ds1307.min&0x70)>>4)*10)+Ds1307.min&0x0f;
Ds1307.min=bcd2dec;
bcd2dec = (((Ds1307.hour&0x70)>>4)*10)+Ds1307.hour&0x0f;
Ds1307.hour=bcd2dec;
bcd2dec = Ds1307.day&0x07;
Ds1307.day=bcd2dec;
bcd2dec = (((Ds1307.date&0x30)>>4)*10)+Ds1307.date&0x0f;
Ds1307.date=bcd2dec;
bcd2dec = (((Ds1307.month&0x30)>>4)*10)+Ds1307.month&0x0f;
Ds1307.month=bcd2dec;
bcd2dec = (((Ds1307.year&0xf0)>>4)*10)+Ds1307.year&0x0f;
Ds1307.year=bcd2dec;
#endif
}
#if 0
void testNvram(void)
{
u8 i=0;
for (i=0;i<56;i++) I2C_Write_DS1307(i+8,i);
i=0;
for (i=0;i<56;i++) {
I2C_Read_DS1307(i+8,&nvram[i]);
printf("nvram[%bu]=%bu\n",i,nvram[i]);
}
}
#endif
/*------------------------------------------------------------------*-
---- END OF FILE -------------------------------------------------
-*------------------------------------------------------------------*/
bit oneSecInt = 0;
void heartBeat(void)
{
I2C_Write_DS1307(7,0xf0);
ET2 = 1; //Enable Timer 2 interrupt
}
void Int_Timer2(void) interrupt 5 using 2
{
ET2=0;
EXF2=0;
oneSecInt = 1;
// printf("Enter T2EX\n");
ET2=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -