📄 example_dps2812m_rtc.c
字号:
/* =================================================================================
File name: Example_DPS2812M_DA.C
Originator: SEED R&D Group
Description:
DPS2812M Device Digital to RTC test program.
=====================================================================================
History:
-------------------------------------------------------------------------------------
10-20-2005 Release Rev 1.0 Jijunhui
------------------------------------------------------------------------------*/
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "Example_DPS2812M_RTC.h"
unsigned int RTC_Status,RTC_Date,RTC_Year,RTC_Month,RTC_Day,RTC_Hour,RTC_Minute,RTC_Second;
unsigned int i1,RTC_DATA[64];
void main(void)
{
//初始化变量
//InitVariable();
//初始化系统
InitSysCtrl();
// Disable and clear all CPU interrupts:
DINT;
IER = 0x0000;
IFR = 0x0000;
//初始化PIE控制
InitPieCtrl();
//初始化PIE参数表
InitPieVectTable();
//初始化外设
//InitPeripherals();
init_rtc();
//设置PE02为SDA输出
//设置RTC时钟
//初始化时编译这条指令,以后读时,可以不用这条指令
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);
}
}
//开中断
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
for(;;)
{
KickDog();
RTC_Status = Read_Rtc_Data(0xde,0xdf,0,0x3f);
RTC_Date = Read_Rtc_Data(0xde,0xdf,0,0x36);
RTC_Year = Read_Rtc_Data(0xde,0xdf,0,0x35);
RTC_Month = Read_Rtc_Data(0xde,0xdf,0,0x34);
RTC_Day = Read_Rtc_Data(0xde,0xdf,0,0x33);
RTC_Hour = Read_Rtc_Data(0xde,0xdf,0,0x32);
RTC_Minute = Read_Rtc_Data(0xde,0xdf,0,0x31);
RTC_Second = Read_Rtc_Data(0xde,0xdf,0,0x30);
}
}
void init_rtc(void)
{
*IIC_SCL_OUT = 1;
Set_Delay(200);
*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);
// 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);
*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&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 + -