?? rtc.c
字號:
#include "DSK2812.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)
{
int i;
for(i = 0; i <= Delay_Num; i++)
{
asm(" 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);
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&1;
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&1;
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&1;
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&1;
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 + -