📄 rtc.c
字号:
/*********************************************************************
FILENAME: CODEC.C
DESIGNER: 戴展波
DATE: 2004/10/10
*********************************************************************/
#include <stdio.h>
#include <csl.h>
#include <csl_cache.h>
#include <csl_gpio.h>
#include "RTC.h"
static Uint32 gpgc = GPIO_GPGC_RMK(
GPIO_GPGC_GP0M_GPIOMODE,
GPIO_GPGC_GPINT0M_DEFAULT,
GPIO_GPGC_GPINTPOL_DEFAULT,
GPIO_GPGC_LOGIC_DEFAULT,
GPIO_GPGC_GPINTDV_DEFAULT
);
static Uint32 gpen = GPIO_GPEN_OF(0x09);
static Uint32 gpdir = GPIO_GPDIR_OF(0x09);
static Uint32 gpval = GPIO_GPVAL_OF(0x09);
static Uint32 gphm = GPIO_GPHM_RMK(GPIO_GPHM_GPXHM_DEFAULT);
static Uint32 gplm = GPIO_GPLM_RMK(GPIO_GPLM_GPXLM_DEFAULT);
static Uint32 gppol = GPIO_GPPOL_RMK(GPIO_GPPOL_GPINTXPOL_DEFAULT);
GPIO_Handle hGpio;
extern far void vectors();
unsigned int RTC_Status,RTC_Date,RTC_Year,RTC_Month,RTC_Day,RTC_Hour,RTC_Minute,RTC_Second;
unsigned int i1,RTC_DATA[64];
unsigned int RTC_ReadData[512];
void main()
{
//初始化CSL
CSL_init();
//打开GPIO句柄
hGpio = GPIO_open(GPIO_DEV0,GPIO_OPEN_RESET);
//GPIO配置
GPIO_configArgs(hGpio,gpgc,gpen,gpdir,gpval,gphm,gplm,gppol);
Set_Rtc(1,0x03,0x11,0x17,0x12,0x30,0x51);
for(i1 = 0; i1 < 64; i1++)
{
RTC_DATA[i1] = i1;
}
Page_Write_Eeprom(0,0,&RTC_DATA[0],64);
Page_Read_Eeprom(0,0,64);
for(i1 = 0; i1 < 64; i1++)
{
if(Read_Rtc_Data(0xae,0xaf,0,i1) != i1)
{
while(1);
}
}
/* for(i1 = 0; i1 < 0x100; i1++)
{
Byte_Write_Eeprom(0,i1,0x55);
if(Read_Rtc_Data(0xae,0xaf,0x0,i1) != 0x55)
{
while(1);
}
Byte_Write_Eeprom(0,i1,0xaa);
if(Read_Rtc_Data(0xae,0xaf,0x00,i1) != 0xaa)
{
while(1);
}
Byte_Write_Eeprom(1,i1,0x55);
if(Read_Rtc_Data(0xae,0xaf,0x01,i1) != 0x55)
{
while(1);
}
Byte_Write_Eeprom(1,i1,0xaa);
if(Read_Rtc_Data(0xae,0xaf,0x01,i1) != 0xaa)
{
while(1);
}
}*/
for(;;)
{
//KickDog();
RTC_Status = Read_Rtc_Data(0xde,0xdf,0,0x3f)&0xff;
RTC_Date = Read_Rtc_Data(0xde,0xdf,0,0x36)&0x07;
RTC_Year = Read_Rtc_Data(0xde,0xdf,0,0x35)&0xff;
RTC_Month = Read_Rtc_Data(0xde,0xdf,0,0x34)&0x1f;
RTC_Day = Read_Rtc_Data(0xde,0xdf,0,0x33)&0x3f;
RTC_Hour = Read_Rtc_Data(0xde,0xdf,0,0x32)&0x3f;
RTC_Minute = Read_Rtc_Data(0xde,0xdf,0,0x31)&0x7f;
RTC_Second = Read_Rtc_Data(0xde,0xdf,0,0x30)&0x7f;
}
}
void init_rtc(void)
{
IIC_SCL_DIR(OUTPUT);
IIC_SDA_DIR(OUTPUT);
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(600);
IIC_SDA_OUT(HIGH_LEVEL);
delay_time(600);
}
void delay_time(Uint32 value)
{
Uint32 i;
for(i = 0; i < value; i++);
}
void IIC_SCL_DIR(Uint32 dir)
{
Uint32 Current_dir;
switch(dir)
{
case OUTPUT:
//set sclk is output
Current_dir = GPIO_pinDirection(hGpio,GPIO_PIN0,GPIO_OUTPUT);
break;
case INPUT:
//set sdin is input
Current_dir = GPIO_pinDirection(hGpio,GPIO_PIN0,GPIO_INPUT);
break;
default:
break;
}
}
void IIC_SDA_DIR(Uint32 dir)
{
Uint32 Current_dir;
switch(dir)
{
case OUTPUT:
//set sclk is output
Current_dir = GPIO_pinDirection(hGpio,GPIO_PIN3,GPIO_OUTPUT);
break;
case INPUT:
//set sdin is input
Current_dir = GPIO_pinDirection(hGpio,GPIO_PIN3,GPIO_INPUT);
break;
default:
break;
}
}
void IIC_SCL_OUT(Uint32 data)
{
//sclk keep high level
GPIO_pinWrite(hGpio,GPIO_PIN0,data);
}
void IIC_SDA_OUT(Uint32 data)
{
//sdin start status is low_edge
GPIO_pinWrite(hGpio,GPIO_PIN3,data);
}
unsigned int IIC_SDA_IN(void)
{
unsigned int i;
i = GPIO_pinRead(hGpio,GPIO_PIN3);
return i;
}
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 Write_WEL(void)
{
unsigned int i;
IIC_SDA_DIR(OUTPUT);
/*写开始位*/
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();
/*复位*/
}
void Write_RWEL(void)
{
unsigned int i;
//sdata 方向输出
IIC_SDA_DIR(OUTPUT);
/*写开始位*/
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 Write_Start_bit(void)
{
//sdata 输出
IIC_SDA_DIR(OUTPUT);
//sclk 输出高电平
IIC_SCL_OUT(HIGH_LEVEL);
//sdata 输出高电平
IIC_SDA_OUT(HIGH_LEVEL);
delay_time(600);
IIC_SDA_OUT(LOW_LEVEL);
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
void Write_Stop_bit(void)
{
//sdata 方向输出
IIC_SDA_DIR(OUTPUT);
//sdata 数据输出
IIC_SDA_OUT(LOW_LEVEL);
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(600);
IIC_SDA_OUT(HIGH_LEVEL);
delay_time(1300);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
void Write_Rtc(unsigned int Rtc_Var)
{
int i;
//sdata 输出,sclk下降沿有效
IIC_SDA_DIR(OUTPUT);
for(i = 7; i >= 0; i--)
{
//sdata
IIC_SDA_OUT((Rtc_Var >> i) & 1);
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
}
unsigned int Read_Rtc_ACK(void)
{
unsigned int i;
//sdata 方向输入,sclk上降沿有效
IIC_SDA_DIR(INPUT);
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(100);
i = IIC_SDA_IN();
i = i & 1;
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
return(i);
}
void Reset_Status(void)
{
unsigned int i;
//SDATA 方向输出
IIC_SDA_DIR(OUTPUT);
/*写开始位*/
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 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 Delay_10ms(void)
{
unsigned int i;
for(i = 0; i < 10000; i++)
{
delay_time(1000);
}
}
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(INPUT);
j = 0;
for(i = 0; i < 8; i++)
{
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(100);
var1[i] = IIC_SDA_IN()&1;
j = j | (var1[i] << (7 - i));
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
/*写停止位*/
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(INPUT);
k = 0;
for(i = 0; i < 8; i++)
{
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(100);
var1[i] = IIC_SDA_IN()&1;
k = k | (var1[i] << (7 - i));
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
RTC_ReadData[0] = k;
/*读DATA*/
for(j = 1; j < length; j++)
{
/*写ACK应答*/
Write_Rtc_ACK();
IIC_SDA_DIR(INPUT);
k = 0;
for(i = 0; i < 8; i++)
{
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(100);
var1[i] = IIC_SDA_IN()&1;
k = k | (var1[i] << (7 - i));
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(200);
}
RTC_ReadData[j] = k;
}
/*写停止位*/
Write_Stop_bit();
}
void Write_Rtc_ACK(void)
{
//SDATA 方向输出
IIC_SDA_DIR(OUTPUT);
//sdata输出低电平
IIC_SDA_OUT(LOW_LEVEL);
delay_time(100);
IIC_SCL_OUT(HIGH_LEVEL);
delay_time(600);
IIC_SCL_OUT(LOW_LEVEL);
delay_time(1300);
}
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();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -