This post was last edited by RCSN on 2018-8-29 23:50 Since the project of this competition needs to use RTC. Therefore, the RTC of GD32 is very easy to use for this peripheral under the drum set, and the library function is also convenient to use. Record this RTC to help future forum friends avoid pitfalls. When using RTC, some codes also refer to the official code. With the help of the reference manual, you can deepen your understanding of this peripheral. The first step is how to initialize RTC ①RTC needs to be powered on all the time to maintain time, so a power management unit is needed, that is, when the power supply VDD is turned off, the power switch can switch the power of the battery backup domain to the Vbat pin, which is the button battery pin input. The following is the official description
To use this battery backup domain, you need to enable the power management unit clock and enable the write enable of this backup domain. As for why you need to enable the write enable of this backup domain, its application will be explained below.
After enabling the above operations, let's look at the RTC part. RTC needs a clock source to maintain. In GD32F350, there are three independent clock sources to choose from, the internal crystal oscillator 40K, the external high-speed crystal oscillator 32-divided clock, and the external low-speed crystal oscillator. There is no external crystal oscillator on the board, so here we still use the internal crystal oscillator 40K, which is IRC40K. After that, it is necessary to determine whether the selected clock source is ready. If it is ready, configure it to the RTC as the clock source, configure some required pre-scaling coefficients, enable the RTC peripheral clock, and wait for the clock to be ready to complete the RTC initialization work. If the previous clock source is not ready, an error is returned. The initialization function I wrote here returns parameters. If the initialization is successful, it returns SUCCESS, and if it is an error, it returns ERROR. You can make some judgments during initialization, such as printing a log to notify the user. The following is a screenshot of the code
All the information is available on the official website. Go to the official website and download it. There is actually a lot of information. I uploaded the attachment.
Details
Published on 2018-8-30 00:13
The second step is how to configure the date and time of the RTC. Before configuring, first understand the RTC module of GD32F350. The official introduction is as follows: 372528 Its advantage is that it provides the sub-second function, but it should be noted that except for the sub-second which is binary, the other time and date are in the form of BCD code. No matter reading or writing, they must be read and written in this form, so when using, appropriate conversion is required. It provides three interrupt sources, enriching some application requirements, such as the use of alarm clocks. These official examples can be referred to. More importantly, there are 5 32-bit general backup registers, totaling 20 bytes. This allows data to be saved in some applications. For example, when I configure the RTC for the first time, I write a flag to one of the backup registers. Then, when resetting, the value of the backup register can be determined by power-on initialization to avoid reconfiguring the register. When configuring during initialization, when the host computer issues configuration instructions after power-off and power-on again, it is necessary to operate the corresponding registers, such as the time register, etc. However, since there are a lot of registers, register operations are indeed a bit troublesome for those who are not familiar with them. The official also provides a simple function interface to encapsulate it. You only need to execute some encapsulated structures to assign values, and then pass parameters to complete the operation. In terms of reading time, a related encapsulation interface is also made. The function prototype is void rtc_current_time_get(rtc_parameter_struct* rtc_initpara_struct).
This structure is rtc_parameter_struct, and the function interface is ErrStatus rtc_init(rtc_parameter_struct* rtc_initpara_struct). The structure prototype is as follows: The comments are relatively clear, which reduces the time for beginners to get started. It should be noted that some of them need to be passed in BCD codes.
So when I am configuring here, I use the serial port to interact. Enter the year, month, day and other information in sequence. If the registration is successful, the current time will be displayed on the serial port, and the flag bit will be written into the backup register 0. Here I only use one of the five backup registers, and of course I will use it again in subsequent applications. The following is the code part[code]//RTC configuration interface function void RtcSetup(void) { uint32_t tmp_ye = 0xFF, tmp_month = 0xFF, tmp_date = 0xFF; uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF; rtc_initpara.rtc_factor_asyn = prescaler_a; rtc_initpara.rtc_factor_syn = prescaler_s; rtc_initpara.rtc_year = 0x18; rtc_initpara.rtc_day_of_week = RTC_SATURDAY; rtc_initpara.rtc_month = RTC_AUG; rtc_initpara.rtc_date = 0x29; rtc_initpara.rtc_display_format = RTC_24HOUR; rtc_initpara.rtc_am_pm = RTC_AM; printf("=======RTC time configuration========\n\r"); printf("Please enter year:\n\r"); while (tmp_ye == 0xFF) { tmp_ye = usart_input_threshold(99); //The hour input cannot be greater than 99 rtc_initpara.rtc_year = tmp_ye; } printf(" %0.2x\n\r", tmp_ye); //Display year printf("Please enter month:\n\r"); while (tmp_month == 0xFF) { tmp_month = usart_input_threshold(12); //The month input cannot be greater than 12 rtc_initpara.rtc_month = tmp_month; } printf(" %0.2x\n\r", tmp_month); //Display month printf("Please enter the day:\n\r"); while (tmp_date == 0xFF) { tmp_date = usart_input_threshold(31); //The day input cannot be greater than 31 rtc_initpara.rtc_date = tmp_date; } printf(" %0.2x\n\r", tmp_date); //Display day printf("Please enter the hour:\n\r"); while (tmp_hh == 0xFF) { tmp_hh = usart_input_threshold(23); //The hour input cannot be greater than 23 rtc_initpara.rtc_hour = tmp_hh; } printf(" %0.2x\n\r", tmp_hh); //Display hour printf("Please enter the minute:\n\r"); while (tmp_mm == 0xFF){ tmp_mm = usart_input_threshold(59); //The minute input cannot be greater than 59 rtc_initpara.rtc_minute = tmp_mm; } printf(" %0.2x\n\r", tmp_mm); printf("Please enter seconds:\n\r"); while (tmp_ss == 0xFF){ tmp_ss = usart_input_threshold(59); //The second input cannot be greater than 59 rtc_initpara.rtc_second = tmp_ss; } printf(" %0.2x\n\r", tmp_ss); if(ERROR == rtc_init(&rtc_initpara)) //RTC clock configuration is wrong { printf("\n\r** RTC time configuration failed! **\n\r"); } else //Correct { printf("\n\r** RTC time configuration success! **\n\r"); rtc_show_time(); RTC_BKP0 = BKP_VALUE; } rtc_show_time();//Display time}
All the information is available on the official website, go to the official website and download it. There is actually a lot of information. I will upload the attachment.
RCSN posted on 2018-8-30 00:13 All the information is available on the official website, go to the official website and download it. There is actually a lot of information. I uploaded the attachment.
Thank you, I downloaded the English version It seems too difficult
The third step is how to get the current time. The official also provides the same interface function void rtc_current_time_get(rtc_parameter_struct* rtc_initpara_struct). However, when applying it, it is necessary to distinguish and convert between BCD code and decimal. Here I simply display it.
During initialization, if the backup register 0 is not written to the flag bit, it will be configured, otherwise it does not need to be configured again. Here you can also judge why the reset is caused based on the status register of the RCU. This state can be used to determine the cause of the reset.
The next step is to get the peripherals --- serial port. Use DMA for sending and receive + idle interrupt for receiving. In addition, GD32F350 has a receive FIFO, which can be played with.