2417 views|0 replies

36

Posts

0

Resources
The OP
 

[RISC-V MCU CH32V103 Review] ---Advancing Wiki---RTC [Copy link]

 

【 RISC-V MCU CH32V103 Review】 ---Advancing Wiki--- RTC

The Advancement of Victory

February 1 , 2021

Preface:

This is the ninth article of Weizi 's review of ch32v103 on EEWORLD . This article mainly explains the basic knowledge of RTC in the manual and walks through the sample code.

First of all, RTC is also a kind of timer in the final analysis. System tick timer, watchdog timer, RTC are all timers. If it is a timer, there is an input clock source. If there is input, there is comparison and output. This also generates: second events, alarm events and overflow events. My master said that the control of MCU engineers is actually to output the appropriate level at the right time. The timer is a condition to judge whether the time is appropriate.

  1. Manual Reading

The official PDF manual is very clear, but I will still show you how to use it. Trust me, I will definitely lead you astray.

1 : Look at the structure diagram first:

2 : Look at the RTC clock source:

" RTC clock ( RTCCLK ) can be provided by HSE/128 , LSE or LSI clock by setting the RTCSEL[1:0] bit in the RCC_BDCTLR register . Before modifying this bit, make sure that the power control register ( PWR_CR) should be set to DBP (bit 8 ) in PWR_CTLR . This bit can only be reset by resetting the backup area. LSE as RTC clock : Since LSE is in the backup domain and powered by VBAT , as long as VBAT maintains power supply, RTC continues to work even if VDD power is cut off . LSI as RTC clock: If VDD power is cut off, RTC automatic wake-up cannot be guaranteed. HSE/128 as RTC clock: If VDD power is cut off or the internal voltage regulator is turned off ( power supply of 1.8V domain is cut off ) , the RTC state is uncertain. "


Conclusion: Clock source: HSE/128 , LSE , LSI ; but if you want the device to still work when VDD is powered off, use LSE .

3 : What else can you do?

First, let me talk about the conclusion: it can be interrupted and woken up. Events include: seconds, overflow, alarm, etc., which can all wake up from standby . Wake-up is when the alarm or the rising edge of the WKU pin exits from standby .

To go a little further: Does the WKUP pin belong to the RTC module? I don't think so, but it's not good to treat it as an orphan.

4 : Check registers

RTC functions are simple, and you can know everything with just a glance. I'd like to say a few words: the official libraries are very complete and easy to use. Generally, you can modify your own code by looking at the official examples. Many people don't look at the registers (when time is tight, many people don't read the manual), but I think it's best to spend two minutes to go through the registers. It doesn't take long, so you can understand it.

  1. Walking code

With the foundation of manual reading, when we look at the code, it is easy to understand why the programmer wrote it this way. The program is very simple, RTC initialization and RTC interrupt.

Main function

int main ( void )

{

RTC_Init();

while (1){

Delay_Ms(1000);

Print time.

}

}

The interrupt handling function reads the time every time it is interrupted

void RTC_IRQHandler ( void )

{

if (RTC_GetITStatus(RTC_IT_SEC) != RESET ) /* Seconds interrupt */

RTC_Get();

if (RTC_GetITStatus(RTC_IT_ALR)!= RESET ){

RTC_ClearITPendingBit(RTC_IT_ALR);

RTC_Get();

}

RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW);

RTC_WaitForLastTask();

}

RTC_Init :

I won't copy the code here. Just know that writing a specific value to a specific location in the backup register is enough to prove it. In a similar way, you can determine whether the microcontroller is reset or powered off.

Below is the function list of the RTC library. With the knowledge of registers, you can understand them by just looking at the function body.

  1. Compile and observe the results

  1. Summarize

RTC is a very easy-to-use internal peripheral. It is also very simple to use. Generally, RTC plays an important role in low power consumption. It is a good way to use it for low power consumption delay. This example code can be directly used in your own code. Microcontrollers are becoming simpler and simpler.

This post is from Domestic Chip Exchange
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list