#include "RX8025T.h"
#include "I2C.h"
#include "virtual_uart.h"
RTC_TIME_DEF stDateTime;
//DEVSTATE devState;
u8 pubRam[10];
const unsigned char* COMPLICE_DATA = __DATE__;//(注意是英文状态下两个_符号的)
const unsigned char* COMPLICE_TIME = __TIME__;
/*******************************************************************************
* Function Name : BCD2DEC
* Description : BCD码转二位十进制
* parameter : temp,BCD码
* Return : 对应数据的二位十进制
*******************************************************************************/
u8 BCD2DEC(u8 temp)
{
temp = (temp >> 4) * 10 + (temp & 0x0f);
return temp;
}
/*******************************************************************************
* Function Name : DEC2BCD
* Description : 二位十进制转BCD码
* parameter : temp,二位十进制
* Return : 数据BCD码
*******************************************************************************/
u8 DEC2BCD(u8 temp)
{
temp = (temp / 10) * 16 + (temp % 10);
return temp;
}
//void R8025AC_Read(u8 sadd, u8 *buf, u8 len)
//{
// IIC_Read_Buffer(sadd, buf, len, RX8025_ADDR_WRITE, RX8025_ADDR_READ);
//}
//
//void R8025AC_Write(u8 sadd,u8 *buf,u8 len)
//{
// IIC_Write_Buffer(sadd, buf, len, RX8025_ADDR_WRITE);
//}
//字符串截取
char *myStrncpy(char *dest, const char *src, int n)
{
int size = sizeof(char)*(n + 1);
char *tmp = (char*)malloc(size); // 开辟大小为n+1的临时内存tmp
if (tmp)
{
memset(tmp, '\0', size); // 将内存初始化为0
memcpy(tmp, src, size - 1); // 将src的前n个字节拷贝到tmp
memcpy(dest, tmp, size); // 将临时空间tmp的内容拷贝到dest
free(tmp); // 释放内存
return dest;
}
else
{
return NULL;
}
}
//得到软件编译时间?
COMPILE_TIME_DEF GetSoftWareBuildTargetTime(void)
{
char arrDate[20]; //Jul 03 2018
char arrTime[20]; //06:17:05
char pDest[20];
COMPILE_TIME_DEF stTime;
sprintf(arrDate,"%s",__DATE__);//Jul 03 2018
sprintf(arrTime,"%s",__TIME__);//06:17:05
//char *strncpy(char *dest, const char *src, int n)
//(char*)(&(pDest[0])) = myStrncpy(pDest, arrDate, 3);
sprintf(pDest, "%s", myStrncpy(pDest, arrDate, 3));
if (strcmp(pDest, "Jan") == 0) stTime.nMonth = 1;
else if (strcmp(pDest, "Feb") == 0) stTime.nMonth = 2;
else if (strcmp(pDest, "Mar") == 0) stTime.nMonth = 3;
else if (strcmp(pDest, "Apr") == 0) stTime.nMonth = 4;
else if (strcmp(pDest, "May") == 0) stTime.nMonth = 5;
else if (strcmp(pDest, "Jun") == 0) stTime.nMonth = 6;
else if (strcmp(pDest, "Jul") == 0) stTime.nMonth = 7;
else if (strcmp(pDest, "Aug") == 0) stTime.nMonth = 8;
else if (strcmp(pDest, "Sep") == 0) stTime.nMonth = 9;
else if (strcmp(pDest, "Oct") == 0) stTime.nMonth = 10;
else if (strcmp(pDest, "Nov") == 0) stTime.nMonth = 11;
else if (strcmp(pDest, "Dec") == 0) stTime.nMonth = 12;
else stTime.nMonth = 1;
sprintf(pDest, "%s", myStrncpy(pDest, arrDate+4, 2));
//int atoi(const char *nptr);
stTime.nDay = atoi(pDest);
sprintf(pDest, "%s", myStrncpy(pDest, arrDate + 4 + 3, 4));
//int atoi(const char *nptr);
stTime.nYear = atoi(pDest);
//time
sprintf(pDest, "%s", myStrncpy(pDest, arrTime, 2));
stTime.nHour = atoi(pDest);
sprintf(pDest, "%s", myStrncpy(pDest, arrTime+3, 2));
stTime.nMinute = atoi(pDest);
sprintf(pDest, "%s", myStrncpy(pDest, arrTime + 3 + 3, 2));
stTime.nSecond = atoi(pDest);
return stTime;
}
/*******************************************************************************
* Function Name : Get8025
* Description : 读取8025T i2c数据
* parameter : addr,起始地址
data,读数据缓冲区
counter,缓冲区数据长度
* Return : None
*******************************************************************************/
void Get8025( u8 addr, u8 *data,u8 counter)
{
u8 i;
I2C_Start();
I2C_Send_Byte(0x64);
I2C_Send_Byte(addr);
I2C_Start();
I2C_Send_Byte(0x65);
for (i = 0; i < counter - 1 ; i++)
*data++ = I2C_Rcv_Byte(TRUE);
*data++ = I2C_Rcv_Byte(FALSE);
I2C_Stop();
}
/*******************************************************************************
* Function Name : Set8025
* Description : 设置8025T i2c数据
* parameter : addr,起始地址
data,写数据缓冲区
counter,缓冲区数据长度
* Return : None
*******************************************************************************/
void Set8025( u8 addr, u8 *data,u8 counter)
{
u8 i;
I2C_Start();
I2C_Send_Byte(0x64);
I2C_Send_Byte(addr);
for(i = 0; i <counter; i++)
I2C_Send_Byte(*data++);
I2C_Stop();
}
/*******************************************************************************
* Function Name : Init8025
* Description : 时钟芯片8025T初始化
* parameter : None
* Return : None
*******************************************************************************/
void Init8025(void)
{
u8 da[3];
da[0]=0x00;
da[1]=0x00; // 24小时模式设置,1Hz 频率输出
da[2]=0x60;
Set8025(RTC8025T_Control1,da,3);
memset(pubRam,0,3);
Get8025(RTC8025T_Control1,pubRam,3);
// if(pubRam[2] != da[2])
// {
// my_printf("RX8025T Init err\r\n");
// }
// else
// {
// my_printf("RX8025T Init ok\r\n");
// }
}
/*******************************************************************************
* Function Name : TimerDataHandle
* Description :
* parameter :
* Return : None
*******************************************************************************/
void TimerDataHandle(u8* pDate)
{
stDateTime.second = BCD2DEC(pDate[0]);
stDateTime.minute = BCD2DEC(pDate[1]);
if(pDate[2]==0x24)
pDate[2] = 0;
stDateTime.hour = BCD2DEC(pDate[2]);
if(pDate[3] == 0x01)
stDateTime.week = 0;
else if(pDate[3] == 0x02)
stDateTime.week = 1;
else if(pDate[3] == 0x04)
stDateTime.week = 2;
else if(pDate[3] == 0x08)
stDateTime.week = 3;
else if(pDate[3] == 0x10)
stDateTime.week = 4;
else if(pDate[3] == 0x20)
stDateTime.week = 5;
else if(pDate[3] == 0x40)
stDateTime.week = 6;
stDateTime.date = BCD2DEC(pDate[4]);
stDateTime.month = BCD2DEC(pDate[5]);
stDateTime.year = BCD2DEC(pDate[6]);
}
//void RTCTimeDis(void)
//{
// my_printf ("%d:%d:%d\r\n ", 1900+stDateTime.year, stDateTime.month+1, stDateTime.date);
// my_printf("%d:%d:%d\n", stDateTime.hour, stDateTime.minute, stDateTime.second);
//}
/*******************************************************************************
* Function Name : RtcSetDateTime
* Description : 设置RTC时间
* parameter : 时间结构体
* Return : None
*******************************************************************************/
void RtcSetDateTime(RTC_TIME_DEF *pTime)
{
u8 Timebuf[7];
Timebuf[0] = DEC2BCD(pTime->second);
Timebuf[1] = DEC2BCD(pTime->minute);
Timebuf[2] = DEC2BCD(pTime->hour);
Timebuf[3] = (0x01)<<(pTime->week);
Timebuf[4] = DEC2BCD(pTime->date);
Timebuf[5] = DEC2BCD(pTime->month);
Timebuf[6] = DEC2BCD(pTime->year);
Set8025(0,Timebuf,7); //Timebuf中数据为BCD码
TimerDataHandle(Timebuf);
}
/*******************************************************************************
* Function Name : RtcSetLocalTime
* Description : 更新本地时间
* parameter : None
* Return : None
*******************************************************************************/
void RtcSetLocalTime(void)
{
COMPILE_TIME_DEF now_ptm;
RTC_TIME_DEF se
时钟芯片RX8025T的STM32驱动例程
需积分: 5 5 浏览量
2023-05-23
08:28:34
上传
评论
收藏 6KB RAR 举报
章鱼哥嵌入式开发
- 粉丝: 5088
- 资源: 36