⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 r8025.c

📁 单片机RX8025实时时钟读写程序 单片机RX8025实时时钟读写程序
💻 C
字号:
#include"dbheader.h"
#define ID_R8025 0x01;
unsigned char Read8025(unsigned char address)
{
       unsigned char temp;
    // DisableInterrupts;
        selecbus=ID_RS8025;     
         I2C_SendByteWithStart(0x64); 
         I2C_SendByte(address); //register high address
		     I2C_SendByteWithStart(0x65);
         temp = I2C_GetLastByte();
         I2C_SendStop();     
    // EnableInterrupts;  
     return temp; 
}
void Write8025(unsigned char address,unsigned char value)
{
    
 
       selecbus=ID_RS8025;
       if(I2C_SendByteWithStart(0x64))
       {
         I2C_SendStop();
         SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
         SetBit(m_mainFlag,M_DATE_ERROR); 
         SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1); 
        return ;
        }
		if(I2C_SendByte(address)) //register low address   
       {
        I2C_SendStop();
         SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
         SetBit(m_mainFlag,M_DATE_ERROR); 
         SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1); 
        return  ;
        } 
        if(I2C_SendByte(value))
        {       
          I2C_SendStop();
           SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
           SetBit(m_mainFlag,M_DATE_ERROR);  
           SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1);    
          return ;       
        }     
        I2C_SendStop();
       
}

unsigned char RTCGetTime(unsigned char item)
{
     unsigned char time=0;
       
   time= Read8025(item);//read rtc;
 
  if(item==RTC_DAY)
   {
    if(time>0x31)
    time= Read8025(item);
     if(time>0x31)
     {
         SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
         SetBit(m_mainFlag,M_DATE_ERROR); 
          SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1);
        return 0xff;//error
     }
   }
   
 else if(item==RTC_MONTH)
  {
    if(time>0x12)
     time= Read8025(item)&0x1f;
     if(time>0x12)
     {
         SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
         SetBit(m_mainFlag,M_DATE_ERROR);  
         SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1);
        return 0xff;//error
     }
    
   }
   
  else if(item==RTC_YEAR)
  {
    if(time>0x99)
     time= Read8025(item);
     if(time>0x99)
     {
        SetBit(DbParam.Item.Db_Status,DBSTATUS_TIME);   
        SetBit(m_mainFlag,M_DATE_ERROR); 
        SaveData(0,EEP_DBPARAM_Db_Status, &DbParam.Item.Db_Status ,1,1);   
        return 0xff;//error
     }
    
   }
  return time; //BCD 
}

void GetTime8025(void)
{  
    TIME time;
//////////////////////////
     time.second=RTCGetTime(RTC_SECOND);
     time.minute=RTCGetTime(RTC_MINUTE);
     time.hour = RTCGetTime(RTC_HOUR);
     //if(!ValBit(DbParam.Item.Db_Status,DBSTATUS_TIME))
      m_Time=time;      
     
 
}

void GetDate8025(void)
{
      DATE date;
       date.week=RTCGetTime(RTC_WEEK);
       date.day=RTCGetTime(RTC_DAY);
       date.month=RTCGetTime(RTC_MONTH);
       date.year = RTCGetTime(RTC_YEAR);
    //   if(!ValBit(DbParam.Item.Db_Status,DBSTATUS_TIME))
       m_Date=date;
  
}

unsigned char SetTime(unsigned char hour,unsigned char minute,unsigned char second)
{ 
   if(CheckBCD(hour)||CheckBCD(minute)||CheckBCD(second))return 1;
   
   if(hour>0x23)return 1;
   if(minute>0x59)return 1;
   if(second>0x59)return 1;//非法时间数据
   
 DisableInterrupts;
   Write8025(0xe0,0x23);//set 24_hour
  
   Write8025(0xf0,0x00);
   Write8025(0x20,hour);
   Write8025(0x10,minute);
   Write8025(0x00,second);   
 EnableInterrupts;
 return 0;
}
//------返回0 成功 ,返回1失败(数据为非法数据
unsigned char SetDate(unsigned char year,unsigned char month,unsigned char day,unsigned char week)
{ 
 
  if(CheckBCD(year)||CheckBCD(month)||CheckBCD(day)||CheckBCD(week))return 1;
  if(day>0x31||day==0)return 1;
  if(month>0x12||month==0)return 1;
  if(week>9)return 1; //非法时间数据
   EnableRTCWrite();
    YRR=BCDtoByte(year);
    MTHR=BCDtoByte(month);
    DAYR=1;//现改为合法数据
    DAYR=BCDtoByte(day);
    DOWR=BCDtoByte(week);
    if(DAYR!= BCDtoByte(day)) 
    {  //恢复不变
     YRR=BCDtoByte(m_Date.year); 
     MTHR=BCDtoByte(m_Date.month);
     DAYR=BCDtoByte(m_Date.day);
     DOWR=BCDtoByte(m_Date.week);
     DisableRTCWrite();
     return 1; //非法时间数据
    }      
  DisableRTCWrite();
  
 DisableInterrupts;
   Write8025(0x60,year);
   Write8025(0x50,month);
   Write8025(0x40,day);
   if(week<7) 
   Write8025(0x30,week);
 EnableInterrupts;
 return 0;
}

void GetTimeMcu(void)
{
   TIME time;
//////////////////////////
     time.second=BytetoBCD(SECR);
     time.minute=BytetoBCD(MINR);
     time.hour = BytetoBCD(HRR);
     
      m_Time=time;      
 //////////////////////////// 
}
void GetDateMcu(void)
{

   DATE date;
       date.week=BytetoBCD(DOWR);
       date.day=BytetoBCD(DAYR);
       date.month=BytetoBCD(MTHR);
       date.year =BytetoBCD(YRR);
       
       m_Date=date;
}
void EnableRTCWrite(void) 
{
  RTCCOMR_RTCWE=2;
  RTCCOMR_RTCWE=2;
  
  RTCCOMR_RTCWE=0;
  RTCCOMR_RTCWE=1;
  RTCCOMR_RTCWE=3;
  RTCCOMR_RTCWE=2;
 
  
}
void DisableRTCWrite(void) 
{
  RTCCOMR_RTCWE=2;
  RTCCOMR_RTCWE=2;
}
void SetMcuTime(void)
{
  EnableRTCWrite();
  YRR=BCDtoByte(RTCGetTime(RTC_YEAR));
  MTHR=BCDtoByte(RTCGetTime(RTC_MONTH));
  DAYR=BCDtoByte(RTCGetTime(RTC_DAY));
  DOWR=BCDtoByte(RTCGetTime(RTC_WEEK));
  
  HRR=BCDtoByte(RTCGetTime(RTC_HOUR));
  MINR=BCDtoByte(RTCGetTime(RTC_MINUTE));
  SECR=BCDtoByte(RTCGetTime(RTC_SECOND));
  DisableRTCWrite();
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -