Source: Huaqing vision embedded School, source: http://www.embedu.org/Column/Column694.htm
Author: Instructor Yang,Hua Qing vision embedded college lecturer.
Real-time clock is short for RTC (real_time clock ). RTC is an integrated circuit, usually called a clock chip. RTC usually requires external 32.768khz crystal, matching capacitor, backup power and other components. Apart from the positioning of I/O Ports, RTC also has functional differences. For example, interfaces with MCU are commonly used as I2C interfaces (short distances can be shared with other devices) there are also the number of RAM, the size of static power consumption, and the number of interruptions, especially the difference in accuracy. The accuracy of RTC can be said to have a great relationship with the temperature, and the temperature will affect the crystal frequency. This chapter describes the clock function modules integrated into the chip.
1 RTC Introduction
In an embedded system, RTC is usually used to provide reliable system time, including hour, minute, second, and year, month, and day, it is also required that the system work properly when it is shut down (usually powered by a backup battery ). Its Peripheral does not require too many auxiliary circuits. Typically, it only needs a high-precision 32.768khz crystal and resistance capacitor, as shown in 1.
Figure 12-1 RTC external circuit
2 RTC Controller
The real-time clock (RTC) unit can be powered by a backup battery, so it can continue to work even if the system power is off. RTC can use the strb/ldrb command to send eight BCD code data to the CPU. The BCD data includes seconds, minutes, hours, dates, weeks, months, and years. The RTC Unit provides a clock through an external 32.768khz crystal oscillator. RTC has the timed alarm function, as shown in figure 2. RTC controller function description:
Figure 12-2 RTC Controller
● Clock data adopts BCD encoding.
● Automatically process the year, month, and day of a leap year.
● With the alarm function, an alarm is triggered when the system is shut down.
● It has independent power input.
● Millisecond-level clock interruption is provided, which can be used as the kernel clock of the embedded operating system.
3 RTC controller register details
Table 3 describes the related registers.
Table 3 RTC control registers
| Rtccon |
Bit |
Description |
Reset Value |
| Retained |
[31: 9] |
Retained |
0 |
| Ticen |
[8] |
Tick Timer 0 = disabled 1 = Enable |
0 |
| Ticcksel |
[7: 4] |
Select the clock source when the timer is tick 4 'b0000 = 32768Hz 4 'b0001 = 16384Hz 4 'b0010 = 8192Hz 4 'b0011 = 4096Hz 4 '0100 = 2048Hz 4 'b0101 = 1024Hz 4 'b0110 = 512Hz 4 'b0111 = 256Hz 4 'b1000 = 128Hz 4' b1001 = 64Hz 4 'b1010 = 32Hz 4 'b1011 = 16Hz 4 'b1100 = 8Hz 4' b1101 = 4Hz 4 'b1110 = 2Hz 4 'b1111 = 1Hz |
4 'b0000 |
| Clkrst |
[3] |
Reset RTC clock count 0 = No Reset 1 = Reset |
0 |
| Cntsel |
[2] |
BCD count Selection 0 = allocated BCD count 1 = Reserved |
0 |
| Clksel |
[1] |
BCD clock Selection 0 = xtal 1/2 divided clock 1 = reserved (xtal frequency Supply) |
0 |
| Rtcen |
[0] |
RTC control enabling 0 = disabled 1 = Enable |
0 |
Table 4 shows the BCD second register description.
Table 4 bcdsec registers
| Bcdsec |
Bit |
Description |
Reset Value |
| Retained |
[31: 7] |
Retained |
-- |
| Secdata |
[6: 4] |
BCD value 0 ~ 5 |
-- |
| [3: 0] |
0 ~ 9 |
-- |
Table 5 shows the BCD minute register description.
Table 5 bcdmin registers
| Bcdmin |
Bit |
Description |
Reset Value |
| Retained |
[31: 7] |
Retained |
-- |
| Mindata |
[6: 4] |
BCD value 0 ~ 5 |
-- |
| [3: 0] |
0 ~ 9 |
-- |
Table 6 shows the BCD hour register description.
Table 6 bcdhour registers
| Bcdhour |
Bit |
Description |
Reset Value |
| Retained |
[31: 7] |
Retained |
-- |
| Hourdata |
[5: 4] |
BCD value 0 ~ 5 |
-- |
| [3: 0] |
0 ~ 9 |
-- |
Table 7 shows the BCD date register description.
Table 7 bcddate registers
| Bcddate |
Bit |
Description |
Reset Value |
| Retained |
[31: 7] |
Retained |
-- |
| Datedata |
[5: 4] |
BCD value 0 ~ 3 |
-- |
| [3: 0] |
0 ~ 9 |
-- |
Table 8 shows the BCD weekly register description.
Table 8 bcdday registers
| Bcdday |
Bit |
Description |
Reset Value |
| Retained |
[31: 3] |
Retained |
-- |
| Daydata |
[2: 0] |
1 ~ 7 |
-- |
Table 9 describes the BCD monthly register.
Table 9 bcdmon registers
| Bcdmon |
Bit |
Description |
Reset Value |
| Retained |
[31: 5] |
Retained |
-- |
| Mondata |
[4] |
BCD value 0 ~ 1 |
-- |
| |
[3: 0] |
0 ~ 9 |
-- |
The BCD year register is described in table 10.
Table 10 bcdyear registers
| Bcdyear |
Bit |
Description |
Reset Value |
| Retained |
[31: 8] |
Retained |
-- |
| Yeardata |
[7: 4] |
BCD value 0 ~ 9 |
-- |
| [3: 0] |
0 ~ 9 |
-- |
4 RTC application example
1. Register Definition
Typedef struct {
Unsigned int bcdsec;
Unsigned int bcdmin;
Unsigned int bcdhour;
Unsigned int bcddate;
Unsigned int bcdday;
Unsigned int bcdmon;
Unsigned int bcdyear;
} Rtcbcd;
# Define rtcbcd (* (volatile rtcbcd *) 0xea300070)
2. Main Program
The following code implements the ability to read RTC's year, month, day, hour, minute, and second. You can open the commented-out code to reset the value.
Void rtc_init (void)
{
Rtccon = 0x01; // enables RTC to control functions
Rtcbcd. bcdsec = 0x0; // 2012 03 15, 12:59:00. Written in BCD format
Rtcbcd. bcdmin = 0x59;
Rtcbcd. bcdhour = 0x12;
Rtcbcd. bcddate = 0x15;
Rtcbcd. bcdmon = 0x03;
Rtcbcd. bcdyear = 0x12;
Rtccon = 0; // disable some RTC control functions
}
Int main ()
{
Unsigned int I = 0;
Uart0_init ();
Rtc_init (); // initialization time
While (1) // loop printing time
{
Printf ("hour % x: min % x: sec % x \ r", rtcbcd. bcdhour, rtcbcd. bcdmin, rtcbcd. bcdsec );
For (I = 0; I <10000000; I ++ );
}
Return 0;
}
2. experiment process and Symptom:
Compiled and generated. ELF File, hardware wiring. Connect the fs_jtag simulator suite. Compile the program and obtain the. ELF File. download the file from the simulator and run it on the target version. The terminal prints the following information.
Hour 12: min 59: sec 17
Hour 12: min 59: sec 18
Hour 12: min 59: sec 19
Hour 12: min 59: sec 20