forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

jinlicong
2024-04-19 d91ac630ac0e13ea31919052ea2bc54cafef583b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "extern_rtc.h"
#include "i2c.h"
#include "uart.h"
#include "gpio.h"
#include "system_general_para.h"
 
__IO sClockBCD sys_clockBCD_g = {24,1,1,0,0,0};
 
 
void RTC_GPIO_Init(void)
{
    I2c_Scl_Gpio_Init();
    
    InputtIO(EXT_RTC_INT_PORT,EXT_RTC_INT_PIN,IN_PULLUP);
}
 
 
 
//ÅжÏ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
    {
        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¸ñʽ
void 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);
    }
}
 
//»ñÈ¡Íⲿ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);
}