The Real Time Clock (RTC) module is the right tool when we want to keep tracking the current time for our applications.
For the Freedom Platform (KL25Z) the RTC module features include:
This document describes how to implement the module configuration. Also, how to modify the hardware in order feed a 32 KHz frequency to RTC module (it is just a simple wire link).
The RTC module needs a source clock of 32 KHz. This source is not wired on the board; hence we need to wire it. Do not be afraid of this, it is just a simple
wire between PTC3 and PTC1 and the good news are that these pins are external.
First of all we need to set the configurations above-mentioned in Component Inspector of CPU component.
With this procedure we have a frequency of 32 KHz on PTC3 and PTC1 configured as RTC clock-in source. The MCG mode configurations in this case is PEE mode: 96 MHz PLL clock, 48 MHz Core Clock and 24 MHz Bus clock.
For the RTC_LDD component the only important thing is to select the ERCKL32K as the Clock Source. The image below shows the RTC_LDD component configuration for this application.
After this you only need to Generate Processor Expert Code and write your application. The code of this example application can be found in the attachments of the post. The application prints every second the current time.
For a non-PEx application we need to do the same configurations above.
MCG_C1 |= MCG_C1_IRCLKEN_MASK;
MCG_C2 &= ~(MCG_C2_IRCS_MASK);
PORTC_PCR1 |= (PORT_PCR_MUX(0x1));
SIM_SOPT1 |= SIM_SOPT1_OSC32KSEL(0b10);
SIM_SOPT2 |= SIM_SOPT2_CLKOUTSEL(0b100);
PORTC_PCR3 |= (PORT_PCR_MUX(0x5));
And the RTC module configuration could be as follows (this is the basic configuration just with seconds interrupt):
SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
RTC_CR = RTC_CR_SWR_MASK;
RTC_CR &= ~RTC_CR_SWR_MASK;
if (RTC_SR & RTC_SR_TIF_MASK){
RTC_TSR = 0x00000000;
}
RTC_TCR = RTC_TCR_CIR(1) | RTC_TCR_TCR(0xFF);
enable_irq(INT_RTC_Seconds - 16);
RTC_IER |= RTC_IER_TSIE_MASK;
RTC_SR |= RTC_SR_TCE_MASK;
RTC_TSR = 0xFF;
After this configurations you can write your application, do not forget to add you Interrupt Service Routine to the vector table and implement an ISR code.
In the attachments you can find two zip files: PEx application and non-PEx application.
I hope this could be useful for you,
Adrián Sánchez Cano.
Original Attachment has been moved to: FRDM-KL25Z-RTC-TEST.zip
Original Attachment has been moved to: FRDM-KL25Z-PEx-RTC.zip
Could I use OSC32KCLK as RTC clock source?
HOW?Is it possible that OSC32KCLK output 32.768KHz? HOW?
it's possible to use only system oscillator without external connection?
thanks
A good tutorial.
I am using LPTMR (1ms) to generate 1 second interrupt as my RTC.
Hi Adrián
Why did you not use the internal OSC32KCLK as RTC source, rather than generating the clock on RTC_CLKOUT and looping it back to RTC_CLKIN?
Also, by using a 1s interrupt and priming RTC_TPR with (32768 - 1000) gives a 1s time base from the LPO 1kHz clock (may be not as accurate as the ERCLK32K socurce but can also be compensated).
Regards
Mark
RTC_TPR with (32768 - 1000) this base is not continued in LLs mode..... in LLs mode it is taking whole cycle from 0 to 32768 to over flow....so is there any way to solve this..??
H
See the following: µTasker KL RTC Support "Using LPO to achieve RTC functionality"
The RTC will run about 32x slower in LLD mode but it is possible to compensate by saving the count and prescale value before entering the LLS state and then compensating the time from the values after wakeup.
Regards
Mark
Hi Mark,
I can answer that question for you. Adrian didn't use OSC32KCLK as the RTC source because he wanted to run the Oscillator in a higher range mode to be able to use the PLL. If he had used OSC32KCLK then there would have had to be a 32 kHz crystal on the XTAL/EXTAL pins.
Regards,
Chris
How to configure timer1 and timer2 in frdm kl25z..??
Hello, I was using the code you uploaded but it didn't work properly, I added this line because you need to enable the oscillator.
This is what you have: RTC_CR = RTC_CR_SWR_MASK; |
RTC_CR &= ~RTC_CR_SWR_MASK;
This is what I added after the previous line :
RTC_CR |= RTC_CR_OSCE_MASK;
Now it works correctly.
if I made a mistake please let me know, Thanks.