RTC.c 4.8 KB
Newer Older
hu's avatar
hu committed
1 2 3 4 5 6 7 8

#include "r_typedefs.h"
#include "dr7f701441.dvf.h"
#include "RTC.h"

volatile uint8_t g_RTCLeapYear; /*1:LeapYear , 0:Not a leap year*/
volatile RTC_Information_st_t g_stRTCInformation;

hu's avatar
hu committed
9
static uint8_t Cal_RTC_Week(uint16_t Year, uint8_t Month, uint8_t Day);
hu's avatar
hu committed
10 11 12 13 14

/* BCD Convert   decimalism*/
/*The valid range of parameters is not judged*/
static uint8_t RTC_BCD_To_Dec(uint8_t u8RTCBCD)
{
hu's avatar
hu committed
15 16
    uint8_t u8RTCData = ((u8RTCBCD >> 4U) * 10U) + (u8RTCBCD & 0X0FU);
    return u8RTCData;
hu's avatar
hu committed
17 18 19 20
}
/* decimalism Convert  BCD */
static uint8_t RTC_Dec_To_BCD(uint8_t u8RTCDec)
{
hu's avatar
hu committed
21 22
    uint8_t u8RTCData = (((u8RTCDec / 10U) % 10U) << 4U) + (u8RTCDec % 10U);
    return u8RTCData;
hu's avatar
hu committed
23 24 25 26 27 28 29 30 31 32
}

/**************************************************************************/ /**
  * \brief      Determine if a year is leap year
  * \param      Year: the year to be determined
  * \retval     \arg 0: Not leap year
  *             \arg 1: Leap year
******************************************************************************/
static uint8_t RTC_Determine_Leap_Year(uint16_t Year)
{
hu's avatar
hu committed
33
    uint16_t u16RTCLeapYear = Year;
hu's avatar
hu committed
34

hu's avatar
hu committed
35
    /*u16RTCLeapYear = ((Year >> 4) & 0x0F) * 10 + (Year & 0x0F);*/
hu's avatar
hu committed
36

hu's avatar
hu committed
37 38 39 40 41 42 43 44
    if (u16RTCLeapYear & 0x0003U)
    {
        u16RTCLeapYear = 0U;
    }
    else
    {
        u16RTCLeapYear = 1U;
    }
hu's avatar
hu committed
45

hu's avatar
hu committed
46
    return (uint8_t)u16RTCLeapYear;
hu's avatar
hu committed
47 48 49 50
}

static void RTC_Stop(void)
{
hu's avatar
hu committed
51 52 53 54 55 56 57
    /*Stop sub-counter */
    RTCA0CE = 0U;
    /*Wait  sub-counter  stop*/
    while (RTCA0CEST != 0)
    {
        ;
    }
hu's avatar
hu committed
58 59 60
}
void RTC_Pre_Init(void)
{
hu's avatar
hu committed
61
    RTC_Stop(); /* Stop RTCA*/
hu's avatar
hu committed
62 63

#if (RTC_MODE_SELECT == RTC_MODE_MAIN)
64
    RTCA0SLSB = 1U;            /*Frequency selection mode*/
hu's avatar
hu committed
65
    RTCA0SCMP = 4000000U - 1U; /*4M*/
hu's avatar
hu committed
66
#else
hu's avatar
hu committed
67 68
    RTCA0SLSB = 0U; /*32.768 kHz mode*/
    RTCA0SUBU = 0U; /*Reserved Later modified,Error Correction*/
hu's avatar
hu committed
69 70
#endif

hu's avatar
hu committed
71
    RTCA0AMPM = 1U; /*fix 24Hour Format*/
hu's avatar
hu committed
72

hu's avatar
hu committed
73 74 75 76 77 78 79
    /*Write start values*/
    RTCA0YEAR = RTC_Dec_To_BCD(RTC_DEFAULT_YEAR);
    RTCA0MONTH = RTC_Dec_To_BCD(RTC_DEFAULT_MONTH);
    RTCA0DAY = RTC_Dec_To_BCD(RTC_DEFAULT_DATE);
    RTCA0HOUR = RTC_Dec_To_BCD(RTC_DEFAULT_HOUR);
    RTCA0MIN = RTC_Dec_To_BCD(RTC_DEFAULT_MINUTE);
    RTCA0SEC = RTC_Dec_To_BCD(RTC_DEFAULT_SECOND);
hu's avatar
hu committed
80

hu's avatar
hu committed
81 82
    /*Starts sub-counter*/
    RTCA0CE = 1U;
hu's avatar
hu committed
83

hu's avatar
hu committed
84 85 86 87 88
    /*Wait  sub-counter  enable*/
    while (RTCA0CEST != 1U)
    {
        ;
    }
hu's avatar
hu committed
89 90 91 92
}

void RTC_Set_Time(RTC_Information_st_t *pstRTCTime)
{
hu's avatar
hu committed
93 94 95 96
    while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
    {
        ;
    }
97
    RTCA0WAIT = 1U;        /*Stop all clock counters*/
hu's avatar
hu committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    while (RTCA0WST != 1U) /*Wait  all clock counters  stop*/
    {
        ;
    }
    /*Write start values*/
    RTCA0YEAR = RTC_Dec_To_BCD(pstRTCTime->u8RTCYear);
    RTCA0MONTH = RTC_Dec_To_BCD(pstRTCTime->u8RTCMonth);
    RTCA0DAY = RTC_Dec_To_BCD(pstRTCTime->u8RTCDayOfMonth);
    RTCA0HOUR = RTC_Dec_To_BCD(pstRTCTime->u8RTCHour);
    RTCA0MIN = RTC_Dec_To_BCD(pstRTCTime->u8RTCMinute);
    RTCA0SEC = RTC_Dec_To_BCD(pstRTCTime->u8RTCSecond);

    /*Start all clock counters*/
    RTCA0WAIT = 0U;
    while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
    {
        ;
    }
hu's avatar
hu committed
116 117 118 119 120 121 122 123
}
/**************************************************************************/ /**
  * \brief      Rreal time clock timing control
  * \attention  Call this function every 100 ms.
  * \retval     None
******************************************************************************/
void RTC_Timing_Service(void)
{
hu's avatar
hu committed
124 125 126 127 128
    uint8_t Week = 0 ;
    while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
    {
        ;
    }
129
    RTCA0WAIT = 1U;        /*Stop all clock counters*/
hu's avatar
hu committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    while (RTCA0WST != 1U) /*Wait  all clock counters  stop*/
    {
        ;
    }
    /*Read  data*/
    g_stRTCInformation.u8RTCYear = RTC_BCD_To_Dec(RTCA0YEAR);
    g_stRTCInformation.u8RTCMonth = RTC_BCD_To_Dec(RTCA0MONTH);
    g_stRTCInformation.u8RTCDayOfMonth = RTC_BCD_To_Dec(RTCA0DAY);
    g_stRTCInformation.u8RTCHour = RTC_BCD_To_Dec(RTCA0HOUR);
    g_stRTCInformation.u8RTCMinute = RTC_BCD_To_Dec(RTCA0MIN);
    g_stRTCInformation.u8RTCSecond = RTC_BCD_To_Dec(RTCA0SEC);


    Week = Cal_RTC_Week(g_stRTCInformation.u8RTCYear, g_stRTCInformation.u8RTCMonth, g_stRTCInformation.u8RTCDayOfMonth);

145
    g_stRTCInformation.u8RTCWeek = Week ;
hu's avatar
hu committed
146 147 148 149 150 151 152 153 154


    g_RTCLeapYear = RTC_Determine_Leap_Year(g_stRTCInformation.u8RTCYear);
    /*Start all clock counters*/
    RTCA0WAIT = 0U;
    while (RTCA0WST != 0U) /*Check that all clock counters are running.*/
    {
        ;
    }
hu's avatar
hu committed
155 156 157
}
void RTC_Init(void)
{
hu's avatar
hu committed
158
    RTC_Timing_Service();
hu's avatar
hu committed
159 160 161 162
}


/*计算 星期几*/
hu's avatar
hu committed
163
static uint8_t Cal_RTC_Week(uint16_t Year, uint8_t Month, uint8_t Day)
hu's avatar
hu committed
164
{
165
    if (Month < 3)
hu's avatar
hu committed
166
    {
167 168
        Month += 12;
        Year--;
hu's avatar
hu committed
169 170
    }

171 172 173 174 175
    uint8_t tmp_0 = (uint8_t)(Year % 100);
    uint8_t tmp_1 = (uint8_t)(Year / 100);
    uint8_t week  = (uint8_t)((tmp_0 + (tmp_0 >> 2) + (tmp_1 >> 2) + 13 * (Month + 1) / 5 + Day - 1 - (tmp_1 << 1)) % 7);

    return week;
hu's avatar
hu committed
176
}