FRDM-KL46Z board sLCD demo code using RTC clock.

Dependencies:   SLCD mbed

Fork of FRDM-KL46Z LCD rtc Demo by Paul Staron

Committer:
star297
Date:
Mon Jan 27 21:58:45 2014 +0000
Revision:
1:34f0bfc62803
Parent:
0:4f67859595b2
Child:
2:1c12897871e5
colon/dot function fixed in library, changed code to suit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:4f67859595b2 1 #include "mbed.h"
star297 0:4f67859595b2 2 #include "SLCD.h"
star297 0:4f67859595b2 3
star297 1:34f0bfc62803 4 time_t seconds = time(NULL); // needed to start rtc on reset to maintain reasonable time if hard reset
star297 0:4f67859595b2 5
star297 0:4f67859595b2 6 SLCD slcd;
star297 0:4f67859595b2 7 Timer scroll;
star297 1:34f0bfc62803 8 DigitalOut led1(LED1);
star297 1:34f0bfc62803 9 DigitalOut led2(LED2);
star297 0:4f67859595b2 10 InterruptIn setmin (SW1);
star297 0:4f67859595b2 11 InterruptIn sethour (SW3);
star297 0:4f67859595b2 12
star297 0:4f67859595b2 13 struct tm t;
star297 0:4f67859595b2 14
star297 1:34f0bfc62803 15 int i,j,k,lastscroll,display_timer,minute,hour,colon,dp;
star297 1:34f0bfc62803 16 char message[60];
star297 0:4f67859595b2 17 void scroll_message();
star297 0:4f67859595b2 18 char buffer[32];
star297 0:4f67859595b2 19
star297 0:4f67859595b2 20 void setminIRQ();
star297 0:4f67859595b2 21 void sethourIRQ();
star297 0:4f67859595b2 22
star297 0:4f67859595b2 23
star297 0:4f67859595b2 24 main()
star297 1:34f0bfc62803 25 {
star297 1:34f0bfc62803 26 slcd.All_Segments(1);
star297 1:34f0bfc62803 27 wait(2);
star297 1:34f0bfc62803 28 slcd.All_Segments(0);
star297 1:34f0bfc62803 29 wait(1);
star297 1:34f0bfc62803 30
star297 1:34f0bfc62803 31 led1 = 1;led2 = 1;
star297 1:34f0bfc62803 32
star297 1:34f0bfc62803 33 sprintf(message, " rtc clock s3 sets the hours s1 sets the minutes");
star297 0:4f67859595b2 34
star297 1:34f0bfc62803 35 // scrolling message
star297 1:34f0bfc62803 36 scroll.start();
star297 1:34f0bfc62803 37 while (i<58) {
star297 1:34f0bfc62803 38
star297 1:34f0bfc62803 39 while (i<58) {
star297 1:34f0bfc62803 40 scroll_message();
star297 1:34f0bfc62803 41 }
star297 1:34f0bfc62803 42 }
star297 1:34f0bfc62803 43 wait(1);
star297 1:34f0bfc62803 44
star297 1:34f0bfc62803 45 setmin.rise(setminIRQ); // start set Minutes IRQ
star297 1:34f0bfc62803 46 sethour.rise(sethourIRQ); // start set Hours IRQ
star297 0:4f67859595b2 47
star297 1:34f0bfc62803 48 // rtc clock function
star297 1:34f0bfc62803 49 while(1) {
star297 1:34f0bfc62803 50
star297 1:34f0bfc62803 51 time_t seconds = time(NULL);
star297 1:34f0bfc62803 52
star297 1:34f0bfc62803 53 if(display_timer>6) {
star297 1:34f0bfc62803 54 strftime(buffer, 4, "%H%M", localtime(&seconds));// display Hours,Minutes for 2 seconds
star297 1:34f0bfc62803 55 slcd.Colon(1);led2=0;
star297 1:34f0bfc62803 56 slcd.DP2(0);led1=1;
star297 1:34f0bfc62803 57 } else {
star297 1:34f0bfc62803 58 strftime(buffer, 4, "%M%S", localtime(&seconds));// display Minutes,Seconds for 8 seconds
star297 1:34f0bfc62803 59 slcd.Colon(0);led2=1;
star297 1:34f0bfc62803 60 slcd.DP2(1);led1=0;
star297 0:4f67859595b2 61 }
star297 1:34f0bfc62803 62 slcd.printf(buffer);
star297 1:34f0bfc62803 63 wait(.5);
star297 1:34f0bfc62803 64 slcd.DP2(0);led1=1;
star297 1:34f0bfc62803 65 display_timer++;
star297 1:34f0bfc62803 66 if (display_timer>9)display_timer=0;
star297 1:34f0bfc62803 67 wait(.5);
star297 0:4f67859595b2 68 }
star297 1:34f0bfc62803 69 }
star297 0:4f67859595b2 70
star297 0:4f67859595b2 71 void scroll_message()
star297 0:4f67859595b2 72 {
star297 1:34f0bfc62803 73 if (scroll.read_ms() > lastscroll + 350) {
star297 0:4f67859595b2 74 scroll.reset();
star297 1:34f0bfc62803 75 if (i > 58) {
star297 0:4f67859595b2 76 i=0;
star297 0:4f67859595b2 77 }
star297 0:4f67859595b2 78 int j, k = i;
star297 0:4f67859595b2 79 for (j = 0; j < 4; j++) {
star297 0:4f67859595b2 80 if (message[k+j]) {
star297 0:4f67859595b2 81 slcd.putc(message[k+j]);
star297 0:4f67859595b2 82 } else {
star297 0:4f67859595b2 83 slcd.putc(' ');
star297 0:4f67859595b2 84 k--;
star297 0:4f67859595b2 85 }
star297 0:4f67859595b2 86 }
star297 0:4f67859595b2 87 i++;
star297 0:4f67859595b2 88 lastscroll=scroll.read_ms();
star297 0:4f67859595b2 89 }
star297 0:4f67859595b2 90 }
star297 0:4f67859595b2 91
star297 1:34f0bfc62803 92 void setminIRQ(void) // set Minutes ISR
star297 0:4f67859595b2 93 {
star297 1:34f0bfc62803 94 display_timer=7;
star297 0:4f67859595b2 95 time_t seconds = time(NULL);
star297 1:34f0bfc62803 96 char buffer[2];
star297 0:4f67859595b2 97 strftime(buffer, 2,"%H", localtime(&seconds));
star297 1:34f0bfc62803 98 hour = atoi(buffer); // get Hour integer
star297 0:4f67859595b2 99 strftime(buffer, 2,"%M", localtime(&seconds));
star297 1:34f0bfc62803 100 minute = atoi(buffer); // get Minutes integer
star297 0:4f67859595b2 101 minute++;
star297 1:34f0bfc62803 102 if(minute>59) minute=0;
star297 1:34f0bfc62803 103 t.tm_sec = 0; // Seconds reset to zero
star297 1:34f0bfc62803 104 t.tm_min = minute;
star297 1:34f0bfc62803 105 t.tm_hour = hour;
star297 1:34f0bfc62803 106 t.tm_mday = 1;
star297 1:34f0bfc62803 107 t.tm_mon = 2;
star297 1:34f0bfc62803 108 t.tm_year = 114;
star297 0:4f67859595b2 109 set_time (mktime(&t));
star297 1:34f0bfc62803 110
star297 0:4f67859595b2 111 }
star297 0:4f67859595b2 112
star297 1:34f0bfc62803 113 void sethourIRQ(void) // set Hours ISR
star297 0:4f67859595b2 114 {
star297 1:34f0bfc62803 115 display_timer=7;
star297 0:4f67859595b2 116 time_t seconds = time(NULL);
star297 0:4f67859595b2 117 char buffer[2];
star297 1:34f0bfc62803 118 strftime(buffer, 2,"%H", localtime(&seconds));
star297 1:34f0bfc62803 119 hour = atoi(buffer); // get Hour integer
star297 0:4f67859595b2 120 strftime(buffer, 2,"%M", localtime(&seconds));
star297 1:34f0bfc62803 121 minute = atoi(buffer); // get Minutes integer
star297 0:4f67859595b2 122 hour++;
star297 1:34f0bfc62803 123 if(hour>23) hour=0;
star297 1:34f0bfc62803 124 t.tm_sec = 0; // Seconds reset to zero
star297 1:34f0bfc62803 125 t.tm_min = minute;
star297 1:34f0bfc62803 126 t.tm_hour = hour;
star297 1:34f0bfc62803 127 t.tm_mday = 1;
star297 1:34f0bfc62803 128 t.tm_mon = 2;
star297 1:34f0bfc62803 129 t.tm_year = 114;
star297 0:4f67859595b2 130 set_time (mktime(&t));
star297 1:34f0bfc62803 131
star297 0:4f67859595b2 132 }
star297 0:4f67859595b2 133
star297 0:4f67859595b2 134
star297 1:34f0bfc62803 135
star297 1:34f0bfc62803 136