文档库 最新最全的文档下载
当前位置:文档库 › 使用 STM32F10xxx 微控制器的 RTC 实现时钟日历

使用 STM32F10xxx 微控制器的 RTC 实现时钟日历

使用 STM32F10xxx 微控制器的 RTC 实现时钟日历
使用 STM32F10xxx 微控制器的 RTC 实现时钟日历

AN2821

Application note

Clock/calendar implementation

on the STM32F10xxx microcontroller RTC The internal RTC (real-time clock) of the 32-bit, ARM? Cortex?-M3-based medium-density

STM32F10xxx microcontroller is an independent timer. It provides a set of continuously

running counters, which, using suitable software, can be used to implement a clock/calendar

function. The RTC and backup registers are supplied through a switch that draws power

from either the V DD supply (when present) or the backup battery. Thus the RTC runs even

when the main supply is switched off.

This application note gives an example of how to implement the clock/calendar functionality

using the medium-density STM32F10xxx microcontroller in low-power and standard

applications. The firmware example also performs leap year counting and tamper time

stamping. It provides a low-power mode demonstration and a basic RTC calibration routine

to compensate for crystal and temperature variations.

The STM3210B-EVAL board supports the firmware example described in this application

note without any modification to its hardware.

Figure 1.STM3210B-EVAL board

December 2008 Rev 11/28

https://www.wendangku.net/doc/8b4069803.html,

Contents AN2821

Contents

1Overview of the medium-density STM32F10xxx backup domain . . . . . 5

1.1Main backup domain features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.2Main RTC features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2Configuring the RTC registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3Clock/calendar functionality features . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.1Clock/calendar basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3.1.1Implementing the clock function on the medium-density STM32F10xxx . 8

3.1.2Implementing calendar function on the medium-density STM32F10xxx . 8

3.1.3Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.2Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.3Calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.4Low-power modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.4.1Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.4.2Standby mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.5Tamper detection and time stamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.5.1TAMPER-RTC pin functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.5.2Tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.5.3Battery tamper timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 4Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

6Running the demonstrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1Clock/calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1.1Setting the time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1.2Setting the alarm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1.3Setting the date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1.4Summer time correction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.2Low-power demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.2.1Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.2.2Standby mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2/28

AN2821Contents

6.3Timestamping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.3.1Tamper events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.3.2Battery removal and installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

6.4Clock calibration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.5Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.5.150 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

6.5.260 Hz time base selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Appendix A Calibration table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Appendix B Additional notes on calibration and clock accuracy . . . . . . . . . . . 25 7Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

3/28

List of figures AN2821 List of figures

Figure 1.STM3210B-EVAL board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Figure 2.RTC block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 3.Time system flowchart(1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 4.Calendar algorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 5.Leap year flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 6.Calibration process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 7.Stop mode entry sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Figure 8.Standby mode entry sequence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Figure 9.STM3210B-EVAL board description. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Figure 10.RTC time base connection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Figure 11.Clock source selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 4/28

1 Overview of the medium-density STM32F10xxx

backup domain

Figure2 shows the RTC block diagram.

1.1 Main backup domain features

The backup domain of the medium-density STM32F10xxx:

●includes ten 16-bit backup data registers, backup control/status registers and RTC

registers.

●is powered by V BA T when the V DD main power is switched off.

●is not reset due to system reset, power reset or when the microcontroller wakes up

from the Standby mode.

●is protected from parasitic write access after reset (access to the backup domain is

disabled after reset)

●consists of two main units:

–an APB1 interface used to interface with the APB1 bus. The backup domain

registers are accessible through the APB1 bus in read/write access mode.

–an RTC core which is clocked by the RTC clock.

5/28

1.2 Main RTC features

The RTC:

●has three clock sources:

–LSE (low-speed external) clock that can also be bypassed by an external clock

–LSI (low-speed internal) RC oscillator

–HSE (high-speed external) clock divided by 128.

●has a 20-bit programmable prescaler that can be programmed to generate the RTC

time base. The prescaler load register contains the period counting value of the

prescaler.

●has three interrupt sources:

–second

–overflow

–alarm (also connected to EXTI Line 17 for auto-wakeup from Stop mode)

●its clock can be output on the T AMPER-RTC pin for calibration purposes.

Please refer to Figure2 for an illustration of the medium-density STM32F10xxx RTC block

diagram.

All of the above mentioned features of the medium-density STM32F10xxx backup domain

can be used to develop a clock/calendar application equivalent to dedicated real-time

clocks.

Note:Be careful when choosing the 32.768 kHz crystal for your application. It is strongly recommended to use a crystal with a load capacitance less than or equal to 7pF. Never use

a crystal with a load capacitance of 12.5pF.

6/28

AN2821Configuring the RTC registers

7/28

2 Configuring the RTC registers

On startup, follow the steps below to configure the RTC to generate an interrupt every second:

●Enable the APB1 backup domain and power interface clocks by writing the BKPEN and PWREN bits to ‘1’ in the RCC_APB1ENR register

●Enable access to backup domain by writing the DBP bit to ‘1’ in the PWR_CR register ●Enable the LSE clock by writing the LSEON bit to ‘1’ (also write LSEBYP to “1” when the external clock has to be bypassed)

●Poll the LSERDY flag in the RCC_BDCR register until the LSE clock is ready (if the external crystal is used as the clock source).

●Select LSE as the RTC clock source by writing ‘01’ to the RTCSEL bits in the RCC_BDCR register.

●Enable the RTC clock by setting the RTCEN bit in the RCC_BDCR register

●Poll the RSF bit in the RTC_CRL register until the RTC registers are synchronized (if a 50/60 Hz external clock source is used this step may take up to a minute to complete)●Poll the RTOFF bit in the RTC_CRL register until the last operation on the RTC registers is over

●Enable the RTC second global interrupt by setting the SECIE bit in the RTC_CRH register

●Wait for the last task to complete

Set the RTC prescaler value using the following formula:f TRCLK = f RTCCLK /(PRL[19:0]+1), where:–f RTCCLK is the input clock frequency

f TRCLK is the time base generated from the prescaler block

For example, if an external 32.768 kHz (32 kHz) crystal oscillator is used, set the prescaler to 32767. If an external 50 Hz supply is used set the prescaler value to 49.

3 Clock/calendar functionality features

basics

3.1 Clock/calendar

A real-time clock keeps track of the time (hours, minutes, seconds) and date (day, month,

year). It should also take into account leap years. A leap year is a year where an extra day is

added to the calendar in order to synchronize it to the seasons. Since the tropical year is

365.242190 days long, a leap day must be added roughly once every 4 years (4 × 0.242190

= 0.968760). Thus, every four years, the month of February counts 29 days instead of 28.

clock function on the medium-density STM32F10xxx

the

3.1.1 Implementing

The used registers are RTC registers: prescaler register, counter register and alarm register.

The medium-density STM32F10xxx RTC peripheral consists of a chain of programmable

counters consisting of two main units:

●the RTC prescaler block generates the RTC time base. Depending on the clock period

applied to the LSE input, this prescaler can be adjusted to generate a time base

(RTC_CLK) of 1 second by writing to the prescaler load register.

●the 32-bit programmable counter can be initialized to the current system time. The

system time is incremented at the RTC_CLK rate and compared with the alarm register

value. When it reaches the alarm register value, an interrupt is generated.

The current system time can be inferred from the value in the counter register by following

the steps shown in Figure3 (“/” implies division, “%” implies modulus)

(1)

1.% is for modulo.

3.1.2 Implementing calendar function on the medium-density STM32F10xxx

The used registers are the 16-bit backup data registers.

When the 32-bit counter register value reaches 86399, this means that one day has

elapsed, and that the date has to be updated. The counter value is then reset. Whenever the

system date is updated, the new date is stored into the 16-bit backup data registers so that

8/28

the system remains at the current date even after system reset, power reset or wakeup from the Standby mode.

The calendar algorithm given in Figure4 can be used to develop the date update function.

Leap year correction: each time the date is updated, the possibility of a leap year also has to be considered (please refer to Figure5).

9/28

Caution:In the case of a device in low-power mode or whose external supply is off when the counter reaches 86399, the counter cannot be reset and so the date is not updated. This is why, just

after power reset, it is necessary to check the counter value and update the date as many

times as the number of days during which the device remained in low-power mode or had its

external main supply switched off.

3.1.3 Summer

correction

time

This correction is also known as daylight saving and it is used in some countries to save

power. The principle is to increment the time of a given country by an hour in summer time

so that the sun rises an hour later in the morning and daylight is present longer in the

evening. Typically, an hour is added to all clocks near the start of spring and, likewise, an

hour is removed in autumn. Exact time and date for summer time correction differs from

location to location.

3.2 Clock source selection

The external LSE (low-speed external) crystal oscillator can be a crystal oscillator usually

with a frequency of 32.768 kHz (32 kHz), which provides a low-power and highly accurate

clock source to the RTC peripheral for clock calendar applications. The external LSE can

also be bypassed by an external clock. Thus, in countries where the mains power line

frequency is significantly stable, a 50 Hz or 60 Hz clock can also be used to provide the RTC

clock. Assuming that there is already an isolating circuit and an overcurrent protection in the

system, all that is needed is to bypass the LSE crystal and adjust the prescaler load register

value so as to generate a time base of 1 second, and hence the second interrupt, if

configured.

This application only covers the case of an emulated 50/60 Hz clock source.

The RTC time base of 1 second is derived from the following formula:

f TRCLK = f RTCCLK/(PRL[19:0]+1), where:

●f TRCLK is the derived time base

●f RTCCLK is the clock frequency applied on the low-speed oscillator input

50 Hz selection: the prescaler load register value is set to 49 (0x31)

60 Hz selection: the prescaler load register value is set to 59 (0x3B)

32.768 kHz selection: the prescaler load register value is set to 32767 (0x7FFF).

3.3 Calibration

Real-clock precision is a requirement in most embedded applications. The external crystal

oscillator used to provide the time base is subject to frequency variations due to external

conditions. It is therefore necessary to compensate for crystal and temperature variations.

The calibration routine used in this application is based on RTC calibration as described in

application note AN2604.

The medium-density STM32F10xxx RTC comes with a digital calibration circuit that gives

the user software control over the calibration process. The digital calibration circuit removes

0 to 127 cycles every 220 clock cycles. The number of times the pulses are blanked depends

on the value loaded into the seven least significant bits of the RTC clock calibration register.

The calibration circuit can only subtract clock cycles, which means that only higher

10/28

frequencies can be compensated whereas lower frequencies cannot. The frequency range

that can be calibrated is [32772, 32768]. The workaround consists in setting the prescaler to

32766 (instead of 32767), in which case the frequency range that can be calibrated shifts

from [32772, 32768] to [32770, 32766].

The calibration process, illustrated in Figure6, is the following:

First, the TAMPER-RTC pin functionality is disabled and the clock calibration register ASOS

and ASOE bits are cleared. The (RTC clock) /64) frequency is output on the TAMPER-RTC

pin by setting the CCO (clock calibration output) bit in the clock calibration register.

There are two modes of calibration: auto-calibration and manual calibration. Manual

calibration consists in actually measuring the LSE/64 value with an external calibrated

frequency counter, calculating and manually entering the calibration value via the joystick.

●Auto-calibration:

The frequency output on the TAMPER-RTC pin is fed to the timer input. The frequency

is calculated by using the PWM input mode of the medium-density STM32F10xxx

general-purpose timer peripheral. The calibration value to be entered in the calibration

register can be inferred on the basis of the deviation from 511.968 Hz (the calibration

table giving calibration values for various error values in ppm is given in Appendix A.)

●Manual calibration:

In this case, the frequency output on the TAMPER-RTC pin has to be measured by

using an external precision instrument. The calibration value has to be calculated, and

then entered manually using the joystick.

Note:1When the RTC clock/64 is output on the TAMPER-RTC pin, the tamper detection functionality is not available.

2When the auto-calibration feature is used, the obtained calibration accuracy depends on the accuracy of the on-board HSE clock. This is because the HSE is used as the time base to

measure the LSE frequency. As a consequence, if a very accurate oscilloscope or

frequency counter is available, it is recommended to implement manual calibration.

11/28

3.4 Low-power

modes

After a system or power reset, the microcontroller is in Run mode. In this mode, the CPU is

clocked by HCLK, and the CPU and its peripherals are running. Several low-power modes

are available to save power when there is no need for the CPU to be running.

The medium-density STM32F10xxx low-power modes have no effect on the internal RTC,

whose counter values keep being updated. The RTC alarm can also be used to auto-wake

up the microprocessor from the low-power mode.

The Stop and Standby low-power modes are described in the following paragraphs.

mode

3.4.1 Stop

In the Stop mode, the:

●CPU clock is off

●peripheral clocks, PLL, HSI, HSE are disabled

●SRAM and register contents are preserved

●RTC and IWDG are kept running

●voltage regulator can be on or in low-power mode

Any of the EXTI line source, RTC Alarm, PVD or USB Wakeup can be used to release the

microcontroller from the Stop mode. On exiting the Stop mode, the clock configuration

returns to its reset state (HSI used as the system clock).

Entering the Stop mode

The sequence used to enter the Stop mode is illustrated in Figure7 and described below:

1. Select the regulator state in Stop mode through the LPDS bit in the PWR_CR register:

–LPDS = ‘0’, the regulator is ON

–LPDS = ‘1’, the regulator is in low-power mode

2. Reset the PDDS bit in PWR_CR

3. Set the SLEEPDEEP bit in the Cortex-M3 system control register

4. Request Wait for interrupt or Wait for event depending on how the Stop mode is

entered.

12/28

After the microcontroller has exited the Stop mode, the basic reset and clock control circuitry

has to be reconfigured and access to the backup domain has to be re-enabled.

mode

3.4.2 Standby

In the Standby mode, the:

●CPU clock is off

●SRAM and register contents are lost except for the backup domain

●RTC and IWDG keep running

●entire 1.8 V domain is off

●Exit through WAKEUP pin rising edge, RTC Alarm, external reset.

The Standby mode is exited on the WAKEUP signal’s rising edge, by external reset or by

triggering an RTC Alarm.

Entering the Standby mode

The sequence used to enter the Standby mode is illustrated in Figure8 and described

below:

1.Clear the WAKEUP flag in the PWR_CSR register.

2. Enable the WAKEUP pin functionality by setting the EWUP bit in the PWR_CSR

register

3. Set the PDDS bit in PWR_CR

4. Set the SLEEPDEEP bit in the cortex system control register

5. Request Wait for interrupt

The Standby mode is the lowest power consumption mode. After the microcontroller wakes

up from the Standby mode, program execution restarts in the same way as after a

system/power reset.

13/28

3.5 Tamper detection and time stamping

The medium-density STM32F10xxx features an internal supervisor (in the backup domain)

that can be used to detect external tampering.

pin functionality

3.5.1 TAMPER-RTC

The TAMPER-RTC pin generates a tamper detection event when the level applied to it

changes from ‘0’ to ‘1’ or from ‘1’ to ‘0’ depending on the TPAL bit in the backup control

register (BKP_CR). A tamper detection event resets all the backup data registers.

Configuring the TAMPER-RTC pin:

1.Set the TAMPER-RTC pin level by configuring the TPAL bit in the backup control

register

2. Enable the T AMPER-RTC pin interrupt by setting the TPIE bit in the backup

control/status register (BKP_CSR)

3. Enable the T AMPER-RTC pin by setting the TPE bit in the backup control register

(BKP_CR)

timestamping

3.5.2 Tamper

The tamper detection feature and the RTC of the medium-density STM32F10xxx can be

used to timestamp the tamper events, that is, to store the exact time and date of each

tamper event. In this application note, the microprocessor’ s internal Flash memory was

emulated as an EEPROM. The last few pages of the Flash memory can be used to store

these timestamps. Every time a tamper event is detected, the backup data registers are

cleared, which means that after every tamper event the calendar information has to be

rewritten to the backup registers.

3.5.3 Battery tamper timestamping

Events like battery removal or battery installation can also be timestamped and stored into

the emulated EEPROM. For this purpose, the V BAT pin (which is connected to the external

battery) can be connected to a general-purpose I/O. Configured in interrupt mode, this I/O

generates an interrupt on both the falling and rising edges of the V BAT signal so as to detect

battery removal and battery installation events. The exact times and dates of the events can

be stored into the memory.

14/28

AN2821Firmware description

15/28

4 Firmware description

The example firmware consists of a clock/calendar driver that includes all the functions

needed to realize a clock and calendar application on the medium-density STM32F10xxx RTC peripheral. Other features like RTC calibration, low-power mode demonstration, timestamping for tamper detection and battery removal/installation and, clock source

selection (between external crystal and 50/60 Hz) are also supported by the clock/calendar driver. The source code example is based on the STM32F10xxx firmware https://www.wendangku.net/doc/8b4069803.html,er include files:

clock_calendar.h : contains the prototypes of the basic functions used to implement the calendar function on the STM32F10xxx’s internal RTC. Also includes some macros for summer-time correction, system start default time and date.

●application_rtc.h : contains the RCC_Configuration , GPIO_Configuration and Systick_Config function prototypes.

●eeprom.h : contains the prototypes of the functions used to emulate EEPROM in the STM32F10xxx’s internal Flash memory, and for the timestamping application.●lcd.h : contains the prototypes of the functions used to drive the TFT LCD present on the STM3210B-EVAL board.

●fonts.h : includes various LCD font size definitions.

menu.h : contains the prototypes of the functions needed to implement the https://www.wendangku.net/doc/8b4069803.html,er source files:

●clock_calendar.c : contains the basic routines for calendar implementation and RTC calibration.

●application_rtc.c : contains the routines for system clock, peripheral clock, GPIO and Systick timer initialization.

eeprom.c : contains the functions used to emulate EEPROM in the STM32F10xxx’s internal Flash memory. The 1 Kbyte internal Flash memory page 124 (address 0x0801F000 – 0x0801F3FF) is used to store timestamping information. The page address and size are defined in the eeprom.h header file.

●lcd.c : contains the functions used to drive the display on the STM3210B-EVAL board.●

menu.c : contains the functions used to implement the menu. Various features can be accessed through the menu options displayed on the LCD screen.

Hardware description AN2821

16/28

5 Hardware description

This application can be fully demonstrated on the STM3210B-EVAL board. The main

hardware sections used are:

●TFT LCD

●LED’s (LD1, LD2, LD3, LD4)●Joystick ●Tamper switch ●Wakeup switch

PA1, PC7, PC8, PC13 pads

Figure 9.

STM3210B-EVAL board description

AN2821Running the demonstrator

17/28

6 Running the demonstrator

6.1 Clock/calendar

The home screen displays the clock, date and day on the LCD screen. The MENU provides

various options. When the application is run for the first time, some default settings are applied. The current system time and date can be set using various MENU options.Press joystick Sel to select, joystick right/left to move to the next/previous position and

joystick up/down to increment/decrement the value. If you want to exit the current menu, do not press any key for eight seconds, the display will automatically return to the home screen.

6.1.1 Setting the time

To set the system time, go to MENU->Time , and use joystick up/down to

increment/decrement the value, joystick right/left to move to the next/previous position and joystick Sel to set the time and return to the home screen. The clock will then start with the newly set time.

6.1.2 Setting the alarm

To set the alarm, go to MENU->Alarm , use joystick left/right, and up/down to enter the alarm

time. Then, press Sel. The system will now prompt you to set the alarm date. Enter the alarm date and press Sel. When the Alarm condition is reached, the LD1 LED (see Figure 9: STM3210B-EVAL board description ) lights up and ‘ALARM ’ appears on the LCD screen. After five seconds, LD1 turns off and the display returns to the home screen.

6.1.3 Setting the date

To set the system date, go to MENU->Date and enter the current date using the joystick.

After entering the date, press the joystick Sel button to set the current date and return to the home screen. The updated date is then displayed on the screen.

If a wrong date is entered, an error message is displayed on the LCD screen.

6.1.4 Summer time correction

The clock is set ahead by 1 hour at 2 am on the last Sunday of March and it is delayed by 1

hour on the last Sunday of October. Assumedly, the correction for March has already been done and the correction for October has to be done. This is a one-time assumption only, further corrections being automatically handled. The date and time have to be after the current system date and time, that is, for instance if the initial time and date were set to 12:00 hours, 1st April, 2008, you can verify the time correction for October 2008 and beyond, but not earlier.

6.2 Low-power demonstration

6.2.1 Stop mode

To enter the Stop mode, go to MENU->LowPower->STOP and press Sel. The LD4 LED

(see Figure 9: STM3210B-EVAL board description ) lights up and the MCU enters the Stop

Running the demonstrator AN2821

18/28mode. A confirmation message is displayed on the LCD screen. Press the joystick Sel button to exit the Stop mode. LD4 turns off and the display returns to the home screen.

6.2.2 Standby

mode

To enter the Standby mode, go to MENU->LowPower->STANDBY and press Sel. A

confirmation message is displayed on the screen. The Standby mode is exited by pressing

the Wakeup switch (see Figure9: STM3210B-EVAL board description).

The RTC alarm can also be used to auto-wake up the microcontroller from the Stop or

Standby mode. For that purpose, first configure the RTC Alarm and then place the

microcontroller in a low-power mode. When the alarm sets off, the microprocessor wakes up

from the low-power mode.

6.3 Timestamping

6.3.1 Tamper

events

To trigger a tamper event, press the B4 tamper switch on the STM3210B-EVAL board (see

Figure9: STM3210B-EVAL board description).

6.3.2 Battery removal and installation

The battery removal and installation conditions are simulated on the STM3210B-EVAL

board. The “battery in place” condition is simulated by placing a jumper between PC7 and

PC8 whereas the “battery removed” condition is simulated by removing this jumper. PC7

and PC8 are available on the STM3210B-EVAL board (see Figure9: STM3210B-EVAL

board description).

Viewing tamper events

To view the tamper log, go to MENU->Tamper->VIEW. If there is no tamper event stored,

the corresponding message is displayed on the LCD screen. A maximum of 64 events can

be stored. Each page shows up to 10 events. Subsequent pages can be displayed by

pressing the joystick SEL button. The tamper view screen is exited from the last page by

pressing the joystick SEL button once more.

Erasing the tamper log

The user can also erase the tamper log. To do so, go to MENU->Tamper->ERASE. This

erases the tamper log.

Since only a page (1 Kbyte) in Flash memory is used to store timestamps, a maximum of 64

timestamps can be stored. Whenever the 65th tamper occurs the memory page is erased.

The LCD screen cannot display more than 10 events at a time. The difference between

timestamps created by pressing the tamper switch and battery tamper timestamps is that

the former type of timestamps shows a “seconds” field, whereas the others show “BI” or

“BO” (to mean “battery in” and “battery out”, respectively).

AN2821Running the demonstrator

19/28

Note:

The TAMPER-RTC pin functionality is not available:

●when the user is navigating through the MENU options ●when user is viewing the tamper log ●during the calibration process

●when the microprocessor is in a low-power mode ●

when the main V DD supply is off

6.4 Clock calibration

To calibrate the external oscillator in manual mode, do as follows:

Go to MENU -> CALIBRATE-> MANUAL . A message appears on the LCD screen:“LSE/64 is available on the PC13/TAMPER-RTC pin. Measure the frequency and press

Sel to proceed”.

The frequency output on the TAMPER-RTC pin can be measured by using a high-precision frequency counter. The PC13 pad is available on the STM3210B-EVAL board. (Refer to Figure 9: STM3210B-EVAL board description .)

Follow the steps of the procedure illustrated in Figure 6: Calibration process to calculate the error in ppm. On the basis of this error, the calibration value can be inferred from the Calibration lookup table in Appendix A .

Once this is done, press Sel. Y ou will be prompted to enter the calibration value. To do so, use the joystick up/down and left/right buttons, then press Sel again. If the entered value is within the range (0-121), it is stored into the RTC clock/calibration register and the application returns to the home screen. Otherwise, the value is invalid. This

generates an error message and the application returns to the home screen without modifying the value in the RTC clock/calibration register.

To calibrate the external oscillator in auto-calibration mode, do as follows:

First, connect a wire link from PC13 (RTC clock output on the TAMPER-RTC pin) to PA1 (timer input). Both of these pads are available on the STM3210B-EVAL board. (Refer to Figure 9: STM3210B-EVAL board description .)

Select MENU->CALIBRATE->AUTO : the calibration value is automatically calculated and fed into the calibration register. A confirmation message will appear on the LCD screen.

After the calibration process, the application returns to normal clock operation.

Note:

For more details, please refer to Appendix B: Additional notes on calibration and clock accuracy on page 25.

6.5 Clock source selection

By default, on startup the clock runs at the external crystal oscillator frequency of

32.768kHz. This frequency is provided by the clock source of the STM3210B-EVAL board. An external clock source can also be used to provide the time base for the RTC. Figure 10 shows how to realize the RTC time base circuit.

Running the demonstrator AN2821

20/28

In this application note, the clock source selection is demonstrated by emulating an external 50/60Hz sinewave (as can be derived from the utility mains). The external 50/60Hz can be derived from a function generator and applied to the oscillator input using a connector probe (see Figure11: Clock source selection).

Caution:Make sure that the amplitude of the applied clock signal is less than 3 V peak-to-peak and that the corresponding DC offset is about half the full-scale value.

Note:Since the time base selection is a one-time setting, the clock is initialized every time the time base is changed.

6.5.1 50

Hz

time base selection

To select an external 50 Hz clock source:

●Apply the external clock to the oscillator input. The clock will seem to stop running.

●Go to MENU->50Hz and press the joystick Sel button. This sets the prescaler

accordingly and the clock restarts from 09:24:00.

To return to the 32.768 kHz clock source selection:

●Go to MENU->32768Hz and press the joystick Sel button

●Remove the external clock source. The clock restarts from 09:24:00.

6.5.2 60

Hz

time base selection

To select an external 60 Hz source:

●Apply the external clock to the oscillator input

●Go to MENU->60Hz and press the joystick Sel button. this sets the prescaler

accordingly and the clock restarts from 09:24:00.

To return to the 32.768 kHz clock source selection:

●Go to MENU->32768Hz and press the joystick Sel button

●Remove the external clock source. The clock restarts from 09:24:00.

RTC

RTC实时时钟 什么是RTC实时时钟 在一个嵌入式系统中,通常采用RTC 来提供可靠的系统时间,包括时分秒和年月日等;而且要求在系统处于关机状态下它也能够正常工作(通常采用后备电池供电),它的外围也不需要太多的辅助电路,典型的就是只需要一个高精度的32.768KHz 晶体和电阻电容等。 S3C2410实时时钟的基本特性 实时时钟(RTC)单元可以通过备用电池供电,因此,即使系统电源关闭,它也可以继续工作。RTC 可以通过STRB/LDRB 指令将8 位BCD 码数据送至CPU。这些BCD 数据包括秒,分,时,日期,星期,月和年。RTC 单元通过一个外部的32.768KHz晶振提供时钟。RTC具有定时报警的功能。RTC 控制器功能说明: ●时钟数据采用BCD 编码 ●能够对闰年的年月日进行自动处理 ●具有告警功能,当系统处于关机状态时,能产生告警中断; ●具有独立的电源输入 ●提供毫秒级时钟中断,该中断可用于作为嵌入式操作系统的内核时钟

RTC实时时钟的结构框图 RTC模块构成 ●闰年产生器 这个模块可以根据BCDDATA,BCDMON,以及BCDYEAR的数据决定每个月的最后日期是28,29,30 还是31。一个8位的计数器只能显示两个BCD码,因此它不能判断00 年究竟是不是闰年。例如它不能够判断1900 年和2000 的差别。。为了解决这个问题,S3C2410内的RTC 模块中有一个固定的逻辑,用来支持2000 年为闰年。请注意虽然2000 年是闰年,但1900 年不是闰年。因此,S3C2410 中00 代表2000 年,而不是1900 年。 ●读/写寄存器 要求置高RTCON 寄存器的0 位来表示读和写RTC 模块中的寄存器。。为了显示秒,分,小时,日期,月和年,CPU 会从BCDSEC,BCDMIN,BCDHOUR,BCDDAY,BCDDATE,BCDMON,和BCDYEAR 寄存器读取数据。但是由于多个寄存器的读取,可能产生1 秒钟的偏离。例如,如果用户读取寄存器BCDYEAR 到BCDMIN,假设结果为1959 年,12 月,31 日,23 点,59 分。在用户读取BCDSEC 寄存器时,但如果结果是0,那么很有可能年,月,日,时,分已经变成了1960 年1 月1 日0 时0 分了。解决的方法是,当读取到的BCDSEC 等于0 时,用户应该在读取一次BCDYEAR到BCDSEC 的值。 ●备用电池 RTC可被备用电池驱动,备用电池通过RTCVDD引脚向RTC提供电压。当系统掉电时,RTC与CPU 之间的接口被阻塞,备用电池仅仅驱动振荡电路以及BCD计数器,这样可减少能量损耗。

RTC实时时钟

#include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include sbit SCK=P3^6; //时钟 sbit SDA=P3^4; //数据 sbit RST = P3^5;// DS1302复位 sbit LS138A=P2^2; sbit LS138B=P2^3; sbit LS138C=P2^4; bit ReadRTC_Flag;//定义读DS1302标志 unsigned char l_tmpdate[7]={0,0,12,15,5,3,8};//秒分时日月周年08-05-15 12:00:00 unsigned char l_tmpdisplay[8]; code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年最低位读写位 code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //共阴数码管0-9 '-' '熄灭‘表 /******************************************************************/ /* 函数声明*/ /******************************************************************/ void Write_Ds1302_byte(unsigned char temp); void Write_Ds1302( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302 ( unsigned char address ); void Read_RTC(void);//read RTC void Set_RTC(void); //set RTC void InitTIMER0(void);//inital timer0 /******************************************************************/ /* 主函数*/ /******************************************************************/ void main(void) { InitTIMER0(); //初始化定时器0 Set_RTC(); //写入时钟值,如果使用备用电池时候,不需要没每次上电写入,此程序应该屏蔽

基于STM32-RTC实时时钟

1 课程设计内容 本文将利用ALIENTEK 2.8寸TFTLCD模块来显示日期时间,实现一个简单的时钟。 2 STM32芯片简介 2006年ARM公司推出了基于ARMv7架构的Cortex系列的标准体系结构,以满足各种技术的不同性能要求,包含A、R、M三个分工明确的系列[1]。其中,A系列面向复杂的尖端应用程序,用于运行开放式的复杂操作系统;R系列适合实时系统;M系列则专门针对低成本的微控制领域。Cortex-M3是首款基于ARMv7-M体系结构的32位标准处理器,具有低功耗、少门数、短中断延迟、低调试成本等众多优点。它是专门为在微控制系统、汽车车身系统、工业控制系统和无线网络等对功耗和成本敏感的嵌入式应用领域实现高系统性能而设计的,它大大简化了编程的复杂性,集高性能、低功耗、低成本于一体[2]。半导体制造厂商意法半导体ST公司是ARM公司Cortex-M3内核开发项目一个主要合作方,2007年6月11日ST公司率先推出了基于Cortex-M3内核的STM32系列MCU。本章将简要介绍STM32系列处理器的分类、内部结构及特点,并对本设计中重点应用的通用定时器做进一步分析。 2.1 STM32 RTC时钟简介 STM32 的实时时钟(RTC)是一个独立的定时器。STM32 的 RTC 模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。修改计数器的值可以重新设置系统当前的时间和日期。 RTC 模块和时钟配置系统(RCC_BDCR 寄存器)是在后备区域,即在系统复位或从待机模式唤醒后 RTC 的设置和时间维持不变。但是在系统复位后,会自动禁止访问后备寄存器和 RTC,以防止对后备区域(BKP)的意外写操作。所以在要设置时间之前,先要取消备份区域(BKP)写保护。 RTC 的简化框图,如图 20.1.1 所示:

汇编语言实现实时时钟显示

汇编语言实现实时时钟显示 data segment msg db 'Current time is: ' hours db ?,? db ':' minutes db ?,? db ':' seconds db ?,?,0 oldint1c dd ? ;旧的入口参数 position dw 0 ;时间显示位置 color db 07h data ends code segment ;代码段 assume cs:code,ds:code start: mov ax,data mov ds,ax jmp run newint1c: push ax push bx push cx push dx push si push di push es mov ax,data mov ds,ax mov ax,0200h ;获取系统时间,ch→hours cl→minutes DH→seconds,均为BCD码格式 int 1ah ;时钟服务 mov al,ch lea si,hours ;时 call BCD mov al,cl lea si,minutes ;分

call BCD mov al,dh lea si,seconds ;秒 call BCD mov ax,0b800h ;显存地址,采用直接写显存的方法输出字符 mov es,ax ;es:di 指向显存地址 lea si,position ;设置时间显示位置 mov di,[si] lea si,msg disp: mov al,[si] inc si or al,al jz exit cld stosb inc di jmp disp exit: pop es pop di pop si pop dx pop cx pop bx pop ax jmp ds:oldint1c ;执行旧的中断服务程序 BCD proc push ax ;用于将BCD码转换为ASCII码入口al-BCD码 mov ah,al ;出口 shr ah,1 shr ah,1 shr ah,1 shr ah,1 and al,0fh add ax,'00' ;加上0的ASCII码 xchg ah,al mov [si],ax pop ax ret

RTC实时时钟芯片

RTC实时时钟芯片 RTC实时时钟芯片是一种计时器,可以由硬件集成电路来完成,也可以由单片机加程序来完成。实时时钟可以对秒、分、时、星期、日、月和年进行准确计时,具有闰年补偿功能,能够计时到2100年。

消费类电子(机顶盒、VCR),手持式装置(GPS、POS终端),医疗设备,办公设备,电信(路由器、交换机、服务器),电器设备,汽车,消费类电子,嵌入式时标,工业,电表。 DS3231集成了温度补偿晶体振荡器(TCXO)和晶体,电池备份输入用于支持连续计时,可编程方波输出,低电平有效复位输出。关键参数: 工作温度商业级:0°C至+70°C,具有2ppm精度; 工业级:40°C至+85°C,具有3.5pmm精度。 DS3231M是业内首款内置MEMS、带温度补偿的RTC,允许器件用于强烈震动的场合,不会由于晶体失效而导致产品故障。 DS3232相比较于DS3231将32kHz输出驱动器更改为推挽输出,省去一个外部上拉电阻,节省空间,够加快时钟的边沿速度,降低器件功耗。电池切换时,可通过32kHz位选择使能/禁止32kHz输出。DS3232的32kHz输出在关闭状态下驱动至低电平,DS3231的32kHz输出在关闭状态下为高阻输出。DS3232内部可通过2个CRATE位控制温度转换速率,这些位用于控制器件的采样率。采样率决定了对温度传感器进行数字转换的频率,以及补偿振荡器的时间间隔。降低采样率则降低了温度传感器的工作频率,从而降低整体功耗。此外,DS3232具有236字节的SRAM。

压检测功能和振荡停止检测功能,内置定时器可以产生周期性的定时中断信号,警报器用于定时报警,可设定天、日期、小时、分钟。工作电压范围:1.70V-5.5V。计时保持电压:1.15V-5.5V。此外,采用IIC接口,支持低功耗模式。 RX6110 频率输出功能:能选择输出频率,有32.768kHz, 1024Hz, 1Hz。 接口类型:IIC总线接口和SPI总线接口。 自动电源切换功能:当VDD低于1.6V失效时,内部电源自动切换到VBAT. 内置128位的RAM。 定时器功能:当事件出现时,定时器可以自动记录到TF-bit,并能通过/IRQ1 或/IRQ2引脚输出。 报警功能:当事件出现时,定时器可以自动记录到AF-bit,并能通过/IRQ1引脚输出。 工作电压范围:1.6V-5.5V。 计时保持电压:1.1V-5.5V。

RTC (实时时钟) 晶振设计指南

RTC (实时时钟) 晶振设计指南(适用于FM31系列,FM3808,FM30C256) 总体概述 FM31系列,FM3808,FM30C256集成了处理器外围器件,它集成了FRAM非易失性存储器和实时时钟于一体。实时时钟在VDD掉电以后自动切换到后备电源。在使用后备电源的情况下,实时时钟耗电量很少以便其可以长期工作。 本应用笔记提醒系统设计者在使用实时时钟时应注意的问题。 振荡器和晶体 任何实时时钟的核心都是晶振,它为分频计数器提供精确的与低功耗的时基信号,它可以用于产生秒、分、时、日等信息。为了确保时钟长期的准确性,晶振必须工作正常,不能受到干扰。 Figure 1. Crystal Hookup to RTC 除了晶体之外,所有必须的元件都被集成在器件之内。如果有额外的诸如电容和电阻等元件被连接到X1和X2引脚,晶振将不能正常工作。这种情况下,直流工作点将发生偏移,晶振频率也会偏移,甚至在上电时,晶振不能正常起振。具有10pF电容和10M阻抗的被动示波器探针也会影响晶振正常工作。 所有的32.768KHZ晶体都有等效电容。市场上最为普遍的32KHZ晶体有两种类型:6pF和12.5pF。在操作时,晶体必须符合推荐的容性特性。那就是说,X1/X2引脚的容性负载必须为6pF。所有的FRAM 实时时钟都设计使用6pF类型的晶体。 Figure 2. Simplified Oscillator Circuit 以上简化的晶振示意图显示了穿孔晶振与芯片内的C1和C2的连接。芯片内的这两个电容值为 12pF,它们和晶体一起工作。因此CLOAD值为 C1*C2/(C1+C2)或6pF.。两个电阻每个为1千欧,它可以调整相位以提高晶振的工作稳定性。  所有带有实时时钟的Ramtron外围器件都选择6pF晶体以在使用后备电源时实现最低功耗。一个12pF的晶体晶振的功耗是6pF晶体晶振的两倍。值得注意的是:晶振内的150nA电流源为电路提供一个低值的直流偏置电流。晶振可能由于噪音和X1/X2引脚上额外负载的影响而引起工作异常。 晶振频率测量 晶振32.768KHZ频率不能被直接监控。不能在X1和X2引脚上增加电容,也不可以用探头直接接触。以下推荐几种检测晶振频率的方法: 1、上电后将OSCEN位设为0。 2、在实时时钟控制寄存器中,(在FM30C256和FM3808中为Flages寄存器),将CAL位设置为1。这一操作将CAL:引脚(FM3808中的INT)变为512Hz的监控器。512Hz是晶振频率的64分频。 3、可使用频率计数器或其他精确频率测量仪器测量晶振频率(可使用数码示波器观察十个周期以上,而不是只观察一个周期)。你希望读到512Hz这一精确频率值,但只要测量值在511.975-512.025Hz之间,我们就可以认为晶振工作正常。CAL(4:0)寄存器位能够使实时时钟误差率小于2ppm。0.025Hz晶振误差大约引起50ppm 的时钟误差。.

stm32 实时时钟rtc配置

u8 RTC_Init(void) { //启用PWR和BKP的时钟(from APB1) RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //后备域解锁 PWR_BackupAccessCmd(ENABLE); //备份寄存器模块复位 BKP_DeInit(); //外部32.768K其哟偶那个 //内部低速rc震荡器 RCC_LSICmd(ENABLE); /* Wait till LSE is ready */ /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); //RTC开启 RCC_RTCCLKCmd(ENABLE); //开启后需要等待APB1时钟与RTC时钟同步,才能读写寄存器 RTC_WaitForSynchro(); //读写寄存器前,要确定上一个操作已经结束 RTC_WaitForLastTask(); //设置RTC分频器,使RTC时钟为1Hz //RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) RTC_SetPrescaler(32767); //等待寄存器写入完成 RTC_WaitForLastTask(); //使能秒中断 RTC_ITConfig(RTC_IT_SEC, ENABLE); //等待写入完成 RTC_WaitForLastTask(); return 0;

}

单片机实验报告 实时时钟

单 片 机 实 验 报 告 姓名:姓名:学号:学号:

一、实验要求: 1. 设计一个实时时钟,四个八段数码管显示格式为:XX.XX(小时/分钟,24小时计时法);使用一个LED用来显示秒的状态,显示规则为:以1Hz频率闪烁,既亮灭一次为一秒钟,500毫秒亮、500毫秒灭。 2. 实时时钟可以通过3x4键盘设置初始值。数字键用于输入数值,sfb0键为设置键,sfb1键为开关键。 3. 设置初始值的流程:先按下sfb0键,四个数码管显示内容变为全“0”,并以1HZ频率开始闪烁并等待键盘输入小时、分钟数值(其中小时2位数,分钟2位数),输入完毕后,实时时钟开始以新输入的时间值开始计时。 4. 开关键的使用方法:在计时模式下按sfb1键一次,时钟停止计时,时间数值停留在按键那刻;在停止计时模式下,按sfb1键一次时钟开始继续计时。 5. 定时闹铃功能(加分功能,可选做):按sgp0_key键,进入闹铃值设置模式,四个数码管显示内容变为全“0”,并以1HZ频率开始闪烁并等待键盘输入小时、分钟数值(其中小时2位数,分钟2位数),此时计时仍然运行,输入完毕后,显示内容恢复为计时值。当时钟计时到达闹铃值,驱动蜂鸣器鸣响8次。 6. 增加通过RS232接口,更改时钟当前时间的功能。 二、实验程序说明: 对实验按键和存储位置的说明语句: 1.创建一个新工程,在该工程的器件编辑器(Device Editor)中选择定时器模块,然后将其按要求放置,如图所示。

图定时器模块放置图 2.配置全局资源。单击参数内容方框里的下拉箭头,选择合适的参数值,便可以更改工程中默认的全局资源。此实验配置的全局资源如图所示。 图全局资源配置 3.按图配置Timer8定时器模块的参数。

STM32实时时钟RTC按键修改时间

User文件夹下main.c #include "sys.h" #include "usart.h" #include "delay.h" #include "led.h" #include "key.h" #include "exti.h" #include "wdg.h" #include "timerx.h" #include "adc.h" #include "rtc.h" #include "12864.h" #include "ov7670.h" #include "usmart.h" #include "enc28j60.h" #include "uip.h" #include "uip_arp.h" #include "tapdev.h" #include "timer.h" #include "math.h" #include "string.h" #include "syn.h" //Mini STM32开发板扩展实验21 //ENC28J60网络模块实验 //正点原子@ALIENTEK //技术论坛:https://www.wendangku.net/doc/8b4069803.html, //广州市星翼电子科技有限公司 void uip_polling(void); void Display_Time(void); void received_date(u8 *str); u16 Process_date(u8 q,u8 b,u8 s,u8 g); #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) u8 t,Addres_1=10,Addres_2=1,Addres_3=168,Addres_4=192; int main(void) { Stm32_Clock_Init(9); //系统时钟设置 //usart_init(72,9600); //串口初始化为9600 USART3_Init(36,9600); USART2_Init(36,9600); delay_init(72); //延时初始化 LED_Init(); //初始化与LED连接的硬件接口LCD12864_InitPort(); //初始化LCD LCD12864_Init();

RTC实时时钟方案

实时时钟的缩写是RTC(Real_TimeClock)。RTC是集成电路,通常称为时钟芯片。我们这里除了要提供精确的实时时间,而且需要在主电源掉电时,还可以工作,需要外加电池供电。 常用的时钟芯片分为并行接口和串行接口两大类,并行时钟芯片数据传送速率较快,但连线多,不利于缩小产品体积,且占用较多的CPU端口资源。串行时钟芯片只需要占用CPU 的2-3条I/O口线,可大大减小产品体积线接口。 DS1302是由美国DALLAS公司推出的具有涓流充电能力的低功耗实时时钟芯片。DS1302可以对年、月、日、周、时、分、秒进行计时,且具有闰年补偿等多种功能,工作电压为2.0-5.5V。这种电路接口简单、价格低廉、使用方便,被广泛采用。 DS1302的引脚排列,其中Vcc2为主电源,VCC1为后备电源。在主电源关闭的情况下,也能保持时钟的连续运行。DS1302由Vcc1或Vcc2两者中的较大者供电。当Vcc2大于Vcc1+0.2V时,Vcc2给DS1302供电。当Vcc2小于Vcc1时,DS1302由Vcc1供电。X1和X2是振荡源,外接32.768kHz晶振。RST是复位/片选线,通过把RST输入驱动置高电平来启动所有的数据传送。RST输入有两种功能:首先,RST接通控制逻辑,允许地址/命令序列送入移位寄存器;其次,RST提供终止单字节或多字节数据传送的方法。当RST为高电平时,所有的数据传送被初始化,允许对DS1302进行操作。如果在传送过程中RST置为低电平,则会终止此次数据传送,I/O引脚变为高阻态。上电运行时,在Vcc>2.0V之前,RST必须保持低电平。只有在SCLK为低电平时,才能将RST置为高电平。I/O为串行数据输入输出端(双向),SCLK为时钟输入端。下图为DS1302的引脚功能图: 下面给出仿真图:

rtc实时时钟

《RTC实时时钟的开发》 1、建立工程 运行QUARTU SⅡ9.0(以下简称Q2),建立工程,File→New Project Wizard,如下图 点击New Project Wizard后弹出指定工程名的对话框,根据需要可进行如下填写 点击Next,出现添加工程文件的对话框

EP2C5Q208C8. 硬件选择完毕后,点击Next,出现以下对话框,这里不进行设置

最后得到一个新工程总结,对话框中的信息即为以上的设置总结 点击Finish。到此为止,工程已经建立完成,接下来需要建立一个Block Diagram/Schematic File,点击Fil e New,如下图所示

点击OK,工程中出现了一个Block1.bdf文件 构建NIO SⅡ软核 接下来,我们进入了构建软核环节,点击Tools SOPC builder或者下图红圈标注的图标 点击以后,SOPC Builder运行,界面如下图所示

将系统的名称命名为core,目标硬件描述语言选择Verilog,然后点击OK, clk_0为时钟名称,50.0为时钟值,单位为MHz,我们可以对它进行修改,用鼠标左键双击50.0,将其改为100.0,这是我们的软核时钟就是100MHz,接下来我们要建立NIO SⅡProcessor。 构建CPU模块 用鼠标左键双左侧边框中的NIO SⅡProcessor 点击后出现如下对话框,选择速度最快功能最多的NIO SⅡ/f。 点击Next,将红圈处选为None,也就是关闭Data Cache。

连续点击Next,直到出现下图为止 这里设置调试级别,级别越高,功能越多,占用的资源也就越多,我们选择级别1,点击Finish,结束NIO SⅡProcessor的建立。 建立SDRAM模块 接下来建立SDRAM控制器

RCT实时时钟详细介绍_华清远见

RCT实时时钟详细介绍 在说道正文之前,我们先要对RCT实时时钟坐一个简单的描述。实时时钟(Real-Time Clock)是PC主板上的晶振及相关电路组成的时钟电路的生成脉冲,RTC经过8254电路的变频产生一个频率较低一点的OS(系统)时钟TSC,系统时钟每一个cpu周期加一,每次系统时钟在系统初起时通过RTC初始化。8254本身工作也需要有自己的驱动时钟(PIT)。RCT实时时钟详细介绍请往下看。 1.1 RTC介绍 在一个嵌入式系统中,通常采用RTC 来提供可靠的系统时间,包括时分秒和年月日等,而且要求在系统处于关机状态下它也能够正常工作(通常采用后备电池供电)。它的外围也不需要太多的辅助电路,典型的就是只需要一个高精度的 32.768kHz晶体和电阻电容等,如图10-8所示。 图10-8 RTC外接电路 1.2 RTC控制器 实时时钟(RTC)单元可以通过备用电池供电,因此,即使系统电源关闭,它也可以继续工作。RTC 可以通过STRB/LDRB 指令将8 位BCD 码数据送至CPU。这些BCD 数据包括秒、分、时、日期、星期、月和年。RTC 单元通过一个外部的32.768kHz晶振提供时钟。RTC具有定时报警的功能,如图10-9所示。RTC 控制器功能说明:

图10-9 RTC控制器 时钟数据采用BCD 编码。 能够对闰年的年月日进行自动处理。 具有告警功能,当系统处于关机状态时,能产生告警中断。 具有独立的电源输入。 提供毫秒级时钟中断,该中断可用于作为嵌入式操作系统的内核时钟。 1.3 RTC控制器寄存器详解 如表10-9所示为相关寄存器描述。 表10-9 RTC控制寄存器

rtc实时时钟代码

/****************************************************************************** * * 标题: 试验数码管显示时钟* * * * 通过本例程了解DS1302时钟芯片的基本原理和使用,理解并掌握DS1302时钟芯片 * * 驱动程序的编写以及实现数字字符在数码管中的显示。 * * 注意:JP1302跳线冒要短接。 * * 请学员认真消化本例程,懂DS1302在C语言中的操作 * ******************************************************************************* */ #include //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include sbit SCK=P3^6; //时钟 sbit SDA=P3^4; //数据 sbit RST = P3^5;// DS1302复位 sbit LS138A=P2^2; sbit LS138B=P2^3; sbit LS138C=P2^4; bit ReadRTC_Flag;//定义读DS1302标志 unsigned char l_tmpdate[7]={0,0,12,15,5,3,8};//秒分时日月周年08-05-15 12:00:00 unsigned char l_tmpdisplay[8]; code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年最低位读写位 code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //共阴数码管0-9 '-' '熄灭‘表 /******************************************************************/ /* 函数声明*/ /******************************************************************/

实时时钟实验

实时时钟实验 一.实验目的 了解实时时钟的硬件控制原理及设计方法。 掌握 S3C2410X 处理器的RTC 模块程序设计方法。 二.实验设备 硬件:Embest EduKit-IV 平台,ULINK2 仿真器套件,PC 机; 软件:μVision IDE for ARM 集成开发环境,Windows 98/2000/NT/XP。 三.实验内容 学习和掌握Embest ARM 教学实验平台中RTC 模块的使用,编写应用程序,修改时钟日期及时间的设置,以及使用EMBEST ARM 教学系统的串口,在超级终端显示当前系统时间。 四.实验原理 1. 实时时钟(RTC) 实时时钟(RTC)器件是一种能提供日历/时钟、数据存储等功能的专用集成电路,常用作各种计算机系统的时钟信号源和参数设置存储电路。RTC 具有计时准确、耗电低和体积小等特点,特别是在各种嵌入式系统中用于记录事件发生的时间和相关信息,如通信工程、电力自动化、工业控制等自动化程度高的领域的无人值守环境。随着集成电路技术的不断发展,RTC 器件的新品也不断推出,这些新品不仅具有准确的RTC,还有大容量的存储器、温度传感器和A/D 数据采集通道等,已成为集RTC、数据采集和存储于一体的综合功能器件,特别适用于以微控制器为核心的嵌入式系统。 RTC 器件与微控制器之间的接口大都采用连线简单的串行接口,诸如I2C、SPI、MICROWIRE 和CAN 等串行总线接口。这些串口由2~3 根线连接,分为同步和异步。 2. S3C2410X 实时时钟(RTC)单元 S3C2410X 实时时钟(RTC)单元是处理器集成的片内外设。由开发板上的后备电池供电,可以在系统电源关闭的情况下运行。RTC 发送8 位BCD 码数据到CPU。传送的数据包括秒、分、小时、星期、日期、月份和年份。RTC 单元时钟源由外部32.768KHz 晶振提供,可以实现闹钟(报警)功能。 S3C2410X 实时时钟(RTC)单元特性: BCD 数据:秒、分、小时、星期、日期、月份和年份 闹钟(报警)功能:产生定时中断或激活系统 自动计算闰年 无 2000 年问题 独立的电源输入 支持毫秒级时间片中断,为 RTOS 提供时间基准 读/写寄存器 访问 RTC 模块的寄存器,首先要设RTCCON 的bit0 为1。CPU 通过读取RTC 模块中寄存器BCDSEC、BCDMIN、BCDHOUR、BCDDAY、BCDDATE、BCDMON 和 BCDYEAR 的值,得到当前的相应时间值。然而,由于多个寄存器依次读出,所以有可能产生错误。比如:用户依次读取年(1989)、月(12)、日(31)、时(23)、分(59)、秒(59)。当秒数为1 到59 时,没有任何问题,但是,当秒数为0 时,当前时间和日期就变成了1990 年1 月1 日0 时0 分。这种

相关文档