📄 rtc.c
字号:
#include "DEC2812.h"
#include "DSP28_Device.h"
/*********************************************************************************************
filename: void init_rtc(void)
*********************************************************************************************/
void init_rtc(void)
{
*IIC_SDA_DIR = 0;
*IIC_SCL_DIR = 0;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
*IIC_SDA_OUT = 1;
Set_Delay(200);
// Byte_Write(0xde,0,0x11,0x08);
// Set_Rtc(1,0x03,0x11,0x17,0x12,0x30,0x15);
}
void Set_Rtc(unsigned int date,unsigned int year,unsigned int month,unsigned int day,unsigned int hour,unsigned int minute,unsigned int second)
{
unsigned int i;
/*写WEL*/
Write_WEL();
/*写RWEL*/
Write_RWEL();
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xde);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(0x30);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DATE*/
Write_Rtc(second);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写YEAR*/
Write_Rtc(minute);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写MONTH*/
Write_Rtc(hour);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DAY*/
Write_Rtc(day);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写HOUR*/
Write_Rtc(month);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写MINUTE*/
Write_Rtc(year);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写SECOND*/
Write_Rtc(date);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写停止位*/
Write_Stop_bit();
Reset_Status();
}
void Set_Delay(unsigned int Delay_Num)
{
unsigned int i;
for(i = 0; i <= Delay_Num; i++)
{
NOP;
}
}
void Write_Start_bit(void)
{
*IIC_SDA_DIR = 0;
*IIC_SDA_OUT = 1;
Set_Delay(200);
*IIC_SCL_OUT = 1;
Set_Delay(200);
*IIC_SDA_OUT = 0;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
void Write_Stop_bit(void)
{
*IIC_SDA_DIR = 0;
*IIC_SDA_OUT = 0;
Set_Delay(200);
*IIC_SCL_OUT = 1;
Set_Delay(200);
*IIC_SDA_OUT = 1;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
void Write_WEL(void)
{
unsigned int i;
*IIC_SDA_DIR = 0;
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xde);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(0x3f);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DATE*/
Write_Rtc(0x02);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写停止位*/
Write_Stop_bit();
/*复位*/
// Reset_Status();
}
void Write_RWEL(void)
{
unsigned int i;
*IIC_SDA_DIR = 0;
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xde);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(0x3f);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DATE*/
Write_Rtc(0x06);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写停止位*/
Write_Stop_bit();
}
void Reset_Status(void)
{
unsigned int i;
*IIC_SDA_DIR = 0;
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xde);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(0x3f);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DATE*/
Write_Rtc(0x0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写停止位*/
Write_Stop_bit();
}
void Write_Rtc(unsigned int Rtc_Var)
{
int i;
*IIC_SDA_DIR = 0;
for(i = 7; i >= 0; i--)
{
*IIC_SDA_OUT = (Rtc_Var >> i) & 1;
Set_Delay(200);
*IIC_SCL_OUT = 1;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
}
unsigned int Read_Rtc_ACK(void)
{
unsigned int i;
*IIC_SDA_DIR = 1;
Set_Delay(200);
*IIC_SCL_OUT = 1;
Set_Delay(200);
i = *IIC_SDA_IN;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
return(i);
}
void Write_Rtc_ACK(void)
{
*IIC_SDA_DIR = 0;
*IIC_SDA_OUT = 0;
Set_Delay(200);
*IIC_SCL_OUT = 1;
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
unsigned int Read_Rtc_Data(unsigned int slave_address1,unsigned int slave_address0,unsigned int address1,unsigned int address0)
{
unsigned int i,j,var1[8];
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(slave_address1);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(address1);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(address0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(slave_address0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*读DATA*/
*IIC_SDA_DIR = 1;
j = 0;
for(i = 0; i < 8; i++)
{
*IIC_SCL_OUT = 1;
Set_Delay(200);
var1[i] = *IIC_SDA_IN;
j = j | (var1[i] << (7 - i));
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
/*写停止位*/
Write_Stop_bit();
return(j);
}
void Page_Read_Eeprom(unsigned int address1,unsigned int address0,unsigned int length)
{
unsigned int i,j,k,var1[8];
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xae);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(address1);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(address0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xaf);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
*IIC_SDA_DIR = 1;
k = 0;
for(i = 0; i < 8; i++)
{
*IIC_SCL_OUT = 1;
Set_Delay(200);
var1[i] = *IIC_SDA_IN;
k = k | (var1[i] << (7 - i));
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
RTC_ReadData[0] = k;
/*读DATA*/
for(j = 1; j < length; j++)
{
/*写ACK应答*/
Write_Rtc_ACK();
*IIC_SDA_DIR = 1;
k = 0;
for(i = 0; i < 8; i++)
{
*IIC_SCL_OUT = 1;
Set_Delay(200);
var1[i] = *IIC_SDA_IN;
k = k | (var1[i] << (7 - i));
Set_Delay(200);
*IIC_SCL_OUT = 0;
Set_Delay(200);
}
RTC_ReadData[j] = k;
}
/*写停止位*/
Write_Stop_bit();
}
void Byte_Write(unsigned int Slave_Address,unsigned int address1,unsigned int address0,unsigned int data)
{
unsigned int i;
/*写WEL*/
Write_WEL();
/*写RWEL*/
Write_RWEL();
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(Slave_Address);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(address1);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(address0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写DATE*/
Write_Rtc(data);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写停止位*/
Write_Stop_bit();
}
void Byte_Write_Eeprom(unsigned int address1,unsigned address0,unsigned int data)
{
/*写WEL*/
Write_WEL();
/*写RWEL*/
Write_RWEL();
/*写EEPROM*/
Byte_Write(0xae,address1,address0,data);
/*延时10MS*/
Delay_10ms();
/*清STATUS*/
Reset_Status();
}
void Page_Write_Eeprom(unsigned int address1,unsigned int address0,unsigned int *data,unsigned int length)
{
unsigned int i,j;
/*写WEL*/
Write_WEL();
/*写RWEL*/
Write_RWEL();
/*写开始位*/
Write_Start_bit();
/*写SLAVE BYTE*/
Write_Rtc(0xae);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 1*/
Write_Rtc(address1);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
/*写WORD ADDRESS 0*/
Write_Rtc(address0);
/*读ACK应答*/
i = Read_Rtc_ACK();
if(i == 1)
{
while(1);
}
for(j = 0; j < length; j++)
{
/*写DATE*/
Write_Rtc(*(data+j));
/*读ACK应答*/
i = Read_Rtc_ACK();
}
/*写停止位*/
Write_Stop_bit();
/*延时10MS*/
Delay_10ms();
/*清STATUS*/
Reset_Status();
}
void Delay_10ms(void)
{
unsigned int i;
for(i = 0; i < 10000; i++)
{
Set_Delay(200);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -