9 years, 11 months ago.

KL25Z Timer

Hello Mbed Members,

I'm having issues with the Time Example (http://mbed.org/handbook/Time). I was able to compile it and have it outputting to the Terminal. The problem is the time doesn't output the changes as time elapsed. It only outputs the time that the example initially sets in.

I tried connecting PTC1 to PTC3 but with no luck. I do not need to keep track of time if the board powers down. I just need it to store the time and output the Hours, Minutes, Seconds that has elapse since the board has been powered on.

Here's an example of the output:

Output Example

Time as seconds since January 1, 1970 = 1256729737
Time as a basic string = Wed Oct 28 11:35:37 2009
Time as a custom formatted string = 11:35 AM
Time as seconds since January 1, 1970 = 1256729737
Time as a basic string = Wed Oct 28 11:35:37 2009
Time as a custom formatted string = 11:35 AM

Any Help is appreciated, Thanks!

3 Answers

8 years, 8 months ago.

If you want to use stand alone, accurate or reliable RTC function you will need to clock the MCU with a 32KHz crystal. Very simple to do, take a look here:


The SDA MCU RTC clock source is no longer required or used, also you will not loose RTC register information on a reset, only during power down. Deep sleep functions will also keep the RTC running in low power modes.

Using the RTC effectively on this platform is a no go unless this mode is employed.

I have been using this set up for some time now, the only down side is the USB will not function due to inaccuracy of the prescalers to operate the USB interface with a 32KHz clock source.

Ideally clock set up mode 3 needs to be added and the RTC source specified in the peripheralpins.c file needs to be conditionally activated if clock set up mode 3 is NOT used. That way only a simple change to the SRC system_mkl25z4.c needs to done to suit the users requirements.

I have had many direct questions regarding this so a small change here would be useful.

If anyone has any ideas how to do this ?

BTW if you are having firmware issues, roll back the firmware to an earlier version or even the original. Some boards appear to have problems depending on the operating system. For me, I have to use the original firmware when using windows7, this may not apply to very one.

9 years, 11 months ago.

Hello Andy Lau,

did you update the mbed lib? Update, go to the Revisions and switch to the newest one (81 revision). It's working on my KL25Z right now.


Can you provide more information? I updated my mbed to Revision 81 and was able to compile but when I try running it output still stays the same. I tried shorting PTC1 to PTC3 and without. Still having no luck.

posted by Andy Lau 04 Apr 2014
8 years, 8 months ago.


I spent the better part of the day trying to get the Time example to work on the KL25Z. After many failed attempts, I finally succeeded (output time incrementing).

To enable the rtc on the kl25z, a jumper must be placed between PTC1 (J10, pin 12) and PTC3 (J1, pin 3) and the resistor R24 must be removed from the board: https://developer.mbed.org/questions/5706/Which-one-is-R24/

These changes allow for the application of an internal 32 khz reference signal on the RTC clock in pin (PTC1). Without both of these hardware mods, the real time clock will not increment. Here is the routine to create the 32 khz clock. I called this code before setting the clock.

32 khz clock generation

void set_CLKOUT32k(void){ 
    MCG->C1 |= MCG_C1_IRCLKEN_MASK; // Enable the internal reference clock. MCGIRCLK is active.
    MCG->C2 &= ~(MCG_C2_IRCS_MASK); // Select the slow internal reference clock source.
    //SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0x2); // Select 32 KHz clock source as RTC_CLKIN
    SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(0x4); // Set PTC3 as CLKOUT pin for MCGIRCLK
    SIM->SCGC5|=SIM_SCGC5_PORTC_MASK;       //Enable Clock to Port C 
    PORTC->PCR[3] |= (PORT_PCR_MUX(0x5)); // Select the MCGIRCLK clock to output on the CLKOUT pin.

Update: I'm pretty sure I understand why the RTC did not run on my kl25z boards. My boards were purchased fairly recently (June of 2015). On these boards, the PEMicro OpenSDA firmware (http://www.pemicro.com/opensda/) was installed at the time of manufacture. Apparently the PEMicro OpenSDA firmware does not include the functions to generate the 32khz clock. Without that clock signal, the RTC won't run. I verified this by installing the "mbed controller" firmware (https://developer.mbed.org/media/uploads/sam_grove/20140530_k20dx128_kl25z_if_opensda.s19.zip). With the "mbed controller" firmware running, the Time example program ran correctly without the need for hardware modifications or the extra clock generation code I listed above. So if you need a functioning RTC on your kl25z, you either need to install the "mbed controller" firmware or when using the PEMicro OpenSDA firmware you need to make the hardware/software mods outlined above.

Without hardware mods the SDA chip outputs a 32kHz signal for the KL25 and that should work out of the box. After the firmware is updated to the (latest) mbed firmware.

posted by Erik - 26 Jun 2015

Can you clarify what you mean by the latest mbed firmware? I've imported the Time hello world example code, ran "update all", but I don't see a 32kHz signal on PTC3 or PTC1 and the RTC clock does not increment. Is there a version dependency on the boot loader version or on the Open SDA firmware version?

posted by Frank Agius 29 Jun 2015

It should be on PTC1. The SDA chip needs to be powered (So board powered via de SDA USB connector), and I think you need mbed firmware running on it. If you have that already you can try a newer version (https://developer.mbed.org/handbook/Firmware-FRDM-KL25Z). You can also try a much older version of the mbed lib, in case it was broken accidentally (I can check that later myself), but it would be weird if no one before found that to happen. (Try for example the rev 81 which Martin confirmed above to work)

posted by Erik - 01 Jul 2015