first pass at demo rtc code for the kl25z

Dependencies:   KL25Z_RTC mbed

Fork of FRDM_RTC by clemente di caprio

Committer:
ftagius
Date:
Fri Jun 26 17:53:08 2015 +0000
Revision:
1:48b423c65bcb
Parent:
0:92bedc4e1536
first pass at demo rtc code for the kl25z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
clemente 0:92bedc4e1536 1 #include "mbed.h"
clemente 0:92bedc4e1536 2 #include "KL25Z_RTC.h"
clemente 0:92bedc4e1536 3
clemente 0:92bedc4e1536 4 DigitalOut myled(LED1);
clemente 0:92bedc4e1536 5 Serial pc(USBTX, USBRX);
clemente 0:92bedc4e1536 6
ftagius 1:48b423c65bcb 7 // Init the RTC module.
ftagius 1:48b423c65bcb 8 KL25Z_RTC rtc(NULL);
clemente 0:92bedc4e1536 9
ftagius 1:48b423c65bcb 10 // Hardware warning!!!!!
ftagius 1:48b423c65bcb 11 // Hardware warning!!!!!
ftagius 1:48b423c65bcb 12 // Hardware warning!!!!!
ftagius 1:48b423c65bcb 13 // Hardware warning!!!!!
ftagius 1:48b423c65bcb 14 // 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
ftagius 1:48b423c65bcb 15 // be removed from the board: https://developer.mbed.org/questions/5706/Which-one-is-R24/
ftagius 1:48b423c65bcb 16 // These changes allow for the application of an internal 32 khz reference signal on the RTC clock in pin (PTC1). Without both of
ftagius 1:48b423c65bcb 17 // these hardware mods, the real time clock will not increment
ftagius 1:48b423c65bcb 18
ftagius 1:48b423c65bcb 19 void set_CLKOUT32k(void){
ftagius 1:48b423c65bcb 20 MCG->C1 |= MCG_C1_IRCLKEN_MASK; // Enable the internal reference clock. MCGIRCLK is active.
ftagius 1:48b423c65bcb 21 MCG->C2 &= ~(MCG_C2_IRCS_MASK); // Select the slow internal reference clock source.
ftagius 1:48b423c65bcb 22 SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
ftagius 1:48b423c65bcb 23 //SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0x2); // Select 32 KHz clock source as RTC_CLKIN
ftagius 1:48b423c65bcb 24 SIM->SOPT2 |= SIM_SOPT2_CLKOUTSEL(0x4); // Set PTC3 as CLKOUT pin for MCGIRCLK
ftagius 1:48b423c65bcb 25 SIM->SCGC5|=SIM_SCGC5_PORTC_MASK; //Enable Clock to Port C
ftagius 1:48b423c65bcb 26 PORTC->PCR[3] |= (PORT_PCR_MUX(0x5)); // Select the MCGIRCLK clock to output on the CLKOUT pin.
ftagius 1:48b423c65bcb 27 }
ftagius 1:48b423c65bcb 28
ftagius 1:48b423c65bcb 29 void SetDateTime
ftagius 1:48b423c65bcb 30 (int year = 2015
ftagius 1:48b423c65bcb 31 ,int mon = 5
ftagius 1:48b423c65bcb 32 ,int day = 26
ftagius 1:48b423c65bcb 33 ,int hour = 10
ftagius 1:48b423c65bcb 34 ,int min = 0
ftagius 1:48b423c65bcb 35 ,int sec = 0
ftagius 1:48b423c65bcb 36 )
ftagius 1:48b423c65bcb 37 {
ftagius 1:48b423c65bcb 38 struct tm Clock;
ftagius 1:48b423c65bcb 39 Clock.tm_year = year - 1900;
ftagius 1:48b423c65bcb 40 Clock.tm_mon = mon;
ftagius 1:48b423c65bcb 41 Clock.tm_mday = day;
ftagius 1:48b423c65bcb 42 Clock.tm_hour = hour;
ftagius 1:48b423c65bcb 43 Clock.tm_min = min;
ftagius 1:48b423c65bcb 44 Clock.tm_sec = sec;
ftagius 1:48b423c65bcb 45 time_t epoch = mktime(&Clock);
ftagius 1:48b423c65bcb 46 if (epoch == (time_t) -1) {
ftagius 1:48b423c65bcb 47 error("Error in clock setting\r\n");
ftagius 1:48b423c65bcb 48 }
ftagius 1:48b423c65bcb 49 set_time(epoch);
ftagius 1:48b423c65bcb 50 }
clemente 0:92bedc4e1536 51
ftagius 1:48b423c65bcb 52 void ShowDateTime()
ftagius 1:48b423c65bcb 53 {
ftagius 1:48b423c65bcb 54 char str[32];
ftagius 1:48b423c65bcb 55 time_t seconds = time(NULL);
ftagius 1:48b423c65bcb 56 struct tm *tminfo = localtime(&seconds);
ftagius 1:48b423c65bcb 57
ftagius 1:48b423c65bcb 58 strftime(str, 32, "%F,%T", tminfo);
ftagius 1:48b423c65bcb 59 printf("RTC: %s\r\n", str);
ftagius 1:48b423c65bcb 60 }
ftagius 1:48b423c65bcb 61
clemente 0:92bedc4e1536 62 int main() {
clemente 0:92bedc4e1536 63
ftagius 1:48b423c65bcb 64 pc.baud( 9600);
ftagius 1:48b423c65bcb 65 set_CLKOUT32k();
ftagius 1:48b423c65bcb 66 rtc.RTC_Start();
ftagius 1:48b423c65bcb 67
ftagius 1:48b423c65bcb 68 SetDateTime();
clemente 0:92bedc4e1536 69 while(1)
clemente 0:92bedc4e1536 70 {
ftagius 1:48b423c65bcb 71
ftagius 1:48b423c65bcb 72 ShowDateTime();
ftagius 1:48b423c65bcb 73 myled=0;
ftagius 1:48b423c65bcb 74 wait_ms(200);
ftagius 1:48b423c65bcb 75 myled=1;
ftagius 1:48b423c65bcb 76 wait_ms(800);
ftagius 1:48b423c65bcb 77
clemente 0:92bedc4e1536 78 }
clemente 0:92bedc4e1536 79 }
clemente 0:92bedc4e1536 80
ftagius 1:48b423c65bcb 81