#include "extern_rtc.h" #include "i2c.h" #include "uart.h" #include "gpio.h" #include "system_general_para.h" __IO sClockBCD sys_clockBCD_g = {0x24,1,1,0,0,0}; void RTC_GPIO_Init(void) { I2c_Scl_Gpio_Init(); InputtIO(EXT_RTC_INT_PORT,EXT_RTC_INT_PIN,IN_NORMAL); } //ÅжÏBCDʱ¼äÊÇ·ñÓÐЧ£¬ FAIL±íʾʱ¼äÓÐÎó£¬PASS±íʾʱ¼äÓÐЧ ErrorStatus TimeEffectJudgm(const sClockBCD TimeBCD) { uint8_t yearDec,monthDec,dayDec,hourDec,minDec,secDec; yearDec = BCD_2_DECIMAL(TimeBCD.year); monthDec = BCD_2_DECIMAL(TimeBCD.month); dayDec = BCD_2_DECIMAL(TimeBCD.day); hourDec = BCD_2_DECIMAL(TimeBCD.hour); minDec = BCD_2_DECIMAL(TimeBCD.min); secDec = BCD_2_DECIMAL(TimeBCD.sec); if((yearDec>99)||(yearDec<20)||(monthDec>12)||(monthDec==0)||(dayDec>31)||(dayDec==0)||(hourDec>23)||(minDec>59)||(secDec>59)) { //ʱ¼äÎÞЧ return FAIL; }else { switch(monthDec) { case 4: case 6: case 9: case 11: if(dayDec>30) return FAIL; case 2: if(((yearDec&0x3) != 0) && (dayDec>28)){ return FAIL; } else if(((yearDec&0x3) == 0) && (dayDec > 29)){ return FAIL; } break; default: break; } return PASS; } } void R8025T_Write(uint8_t sadd,uint8_t * pBuffer,uint8_t length) { #if SOFT_OR_HARD I2c_Start(); I2c_Send(RX8025_WRITE_ADDR); I2c_Send(sadd);//д¼Ä´æÆ÷´¢´æµØÖ· for(uint8_t i = 0;i < length;i++) I2c_Send(pBuffer[i]); I2c_Stop(); #else I2C_Send_Bit(I2C0,STARTBIT); I2C_Send_Byte(I2C0,RX8025_WRITE_ADDR); I2C_Send_Byte(I2C0,sadd); for(uint8_t i = 0;i < length;i++) I2C_Send_Byte(I2C0,pBuffer[i]); I2C_Send_Bit(I2C0,STOPBIT); #endif } void R8025T_Read(uint8_t sadd,uint8_t * pBuffer,uint8_t length) { uint8_t i = 0; #if SOFT_OR_HARD I2c_Start(); I2c_Send(RX8025_WRITE_ADDR); I2c_Send(sadd);//·¢ËͼĴæÆ÷µØÖ· I2c_Start();//SrÌõ¼þ£¬RESTART I2c_Send(RX8025_READ_ADDR); for(i = 0;i < length - 1;i++) { pBuffer[i] = I2c_Read(); I2c_Ack(); } pBuffer[i] = I2c_Read(); I2c_No_Ack(); I2c_Stop(); #else I2C_Send_Bit(I2C0,STARTBIT); I2Cx_CR_RCEN_Setable(I2C0,DISABLE ); I2C_Send_Byte(I2C0,RX8025_WRITE_ADDR); I2C_Send_Byte(I2C0,sadd); I2C_Send_Bit(I2C0,RESTARTBIT); I2C_Send_Byte(I2C0,RX8025_READ_ADDR); for(i = 0;i < length - 1;i++) { I2C_Receive_Byte(I2C0,&pBuffer[i]); I2C_SEND_ACK_0(I2C0); } I2C_Receive_Byte(I2C0,&pBuffer[i]); I2C_SEND_ACK_1(I2C0);; I2C_Send_Bit(I2C0,STOPBIT); #endif } //ÉèÖÃÍⲿRTCʱ¼ä //²ÎÊý£ºBCD¸ñʽ ErrorStatus Set_Extern_Rtc_Time(uint16_t year,uint8_t month,uint8_t day,uint8_t week,uint8_t hour,uint8_t min,uint8_t sec) { uint8_t rtc_time[7] = {0,0,0,0,1,1,0x15}; sClockBCD calendar_l; calendar_l.year = year & 0x00FF; calendar_l.month = month; calendar_l.day = day; calendar_l.hour = hour; calendar_l.min = min; calendar_l.sec = sec; if(TimeEffectJudgm(calendar_l)==PASS)//ʱ¼äÕýÈ·²ÅÉèÖÃRTC { rtc_time[0] = sec; rtc_time[1] = min; rtc_time[2] = hour; rtc_time[3] = week; rtc_time[4] = day; rtc_time[5] = month; rtc_time[6] = year; R8025T_Write(0,rtc_time,7); return PASS; } return FAIL; } //»ñÈ¡ÍⲿRTCʱ¼ä ErrorStatus Get_Extern_Rtc_Time(__IO sClockBCD * calendar_p) { uint8_t rtc_time[7]; sClockBCD calendar_l; R8025T_Read(0,rtc_time,7); calendar_l.year = rtc_time[6]; calendar_l.month = rtc_time[5]; calendar_l.day = rtc_time[4]; calendar_l.hour = rtc_time[2]; calendar_l.min = rtc_time[1]; calendar_l.sec = rtc_time[0]; if(TimeEffectJudgm(calendar_l)==PASS) //¶ÁÈ¡µÄʱ¼äÕýÈ·²Å²Ù×÷Ö¸Õë { calendar_p->sec = rtc_time[0]; calendar_p->min = rtc_time[1]; calendar_p->hour = rtc_time[2]; calendar_p->day = rtc_time[4]; calendar_p->month = rtc_time[5]; calendar_p->year = rtc_time[6]; #ifdef RS232_PRINTF printf("EX_RTC_TIME = %X-%02X-%02X-%02X-%02X-%02X \r\n",calendar_p->year,calendar_p->month,calendar_p->day, calendar_p->hour,calendar_p->minute,calendar_p->second); #endif return PASS; }else { //ÔÙ¶ÁÒ»´Î R8025T_Read(0,rtc_time,7); calendar_l.year = rtc_time[6]; calendar_l.month = rtc_time[5]; calendar_l.day = rtc_time[4]; calendar_l.hour = rtc_time[2]; calendar_l.min = rtc_time[1]; calendar_l.sec = rtc_time[0]; if(TimeEffectJudgm(calendar_l)==PASS) //¶ÁÈ¡µÄʱ¼äÕýÈ·²Å²Ù×÷Ö¸Õë { calendar_p->sec = rtc_time[0]; calendar_p->min = rtc_time[1]; calendar_p->hour = rtc_time[2]; calendar_p->day = rtc_time[4]; calendar_p->month = rtc_time[5]; calendar_p->year = rtc_time[6]; #ifdef RS232_PRINTF printf("EX_RTC_TIME = %X-%02X-%02X-%02X-%02X-%02X \r\n",calendar_p->year,calendar_p->month,calendar_p->day, calendar_p->hour,calendar_p->minute,calendar_p->second); #endif return PASS; }else { return FAIL; } } } void Set_Rtc_UpdataInterrupt(void) { uint8_t rtc_reg[5]; // rtc_reg[0] = 0x00; // rtc_reg[1] = 0x05; // Counter Ϊ0x0500£¬¼´1280¼ÆÊý£¬ÅäºÏ64HzµÄƵÂÊ£¬²úÉú20SµÄ¼ÆÊ±ÄÖÖÓ // rtc_reg[2] = 0x19; //¿ªÆôTime interrupt£¬FOUT Ϊ1hz(Õâ¸öËæ±ãÅä²»Ó°Ïì)£¬ ¼ÆÊ±µÄƵÂÊΪ64Hz¡£ // rtc_reg[3] = 0x00; // rtc_reg[4] = 0x50; //ζȲ¹³¥ÆµÂÊ2s£¨Ä¬ÈÏ£©£¬Ê¹ÄÜTime interrupt // rtc_reg[0] = 0; // rtc_reg[1] = 0x00; //ǰÁ½¸ö×Ö½Ú´ú±í¼ÆÊýÖµ = 0 // rtc_reg[2] = 0x02; //´ú±í1s¸üÐÂÒ»´Î // rtc_reg[3] = 0x00; // rtc_reg[4] = 0x60; //´ú±íUpdate Interrupt Enable /* Ïê¼û¡¶RX-8025T Application Manual¡·*/ rtc_reg[0] = TIMER_COUNTER_0; //×¢£º¼ÆÊýÆ÷×Üʱ¼äΪTIMER_COUNTER_0 + TIMER_COUNTER_1£» rtc_reg[1] = TIMER_COUNTER_1; rtc_reg[2] = TEST_BIT_FLAG | ALARM_BIT_FLAG | UPDATA_INTER_SELECT | TIMER_EN | FOUT_FRE_SELECT | EXTENSION_TIMER_SELECT; rtc_reg[3] = FLAG_REG; rtc_reg[4] = TEMP_BIT_FLAG | UPDATA_BIT_FLAG| TIMER_BIT_FLAG | ALARM_BIT_EN | RESET_BIT_FLAG; R8025T_Write(0x0B,rtc_reg,5); }