STM32L476RG has one Real Time Clock (RTC) unit on-board. RTC has two sets of alarms (ALARM-A, ALARM-B). In the lab session of RTC, we worked on ALARM-A to generate a 1Hz square wave on PA5.
Questions for Task 3
Copyright By PowCoder代写 加微信 powcoder
a) Assume the asynchronous prescaler of RTC is equal to 0x3F. Using ALARM-A, design a project to generate a 1 Hz square wave on PA6. Provide the source code in C.
Hint: you might use polling, or an interrupt scheme in your code.
b) Explain why your project outputs a square-wave of 1 Hz.
Because the frequency requirement of square wave (PA5 on/off) is 1Hz and the period is 1s. That is to say, the LED of pin PA5 turns on for 0.5s and then turns off for 0.5s, so it is set in the code to trigger an interrupt RTC_Alarm_IRQHandler every 0.5s to switch PA5. Clock frequency f should be 2Hz.
According to [STM32L476 Reference Manual;No comparison on sub seconds for Alarm A. ]. We can set the data in MASKSS to 0.
In this way, the alarm time is the shortest. When the second unit increases, the alarm is activated, so if the calendar clock is set to 0.5 Hz, the alarm is activated every 0.5 seconds (switch PA5)
Next, configure the prescaler register to obtain the required output clock frequency F.
F=f_RTC/((Asynch_Prescaler+1)×(Synch_Prescaler+1) )=1 f_RTC=32768
If we want F=2Hz, then
(Asynch_Prescaler+1)×(Synch_Prescaler+1)=16384
Because it has been assumed that the asynchronous prescaler of RTC is equal to 0x3f
Which means:
Asynch_Prescaler+1=64
Synch_Prescaler+1=16384/64=256
256-1=255=0xFF.
The code of prescaler part is as follows:
RTC->PRER |= (0xFFUL<