FRDM-KL46Z board sLCD demo code
Fork of FRDM-KL46Z LCD rtc Demo by
main.cpp@2:678c0be8c23d, 2014-09-27 (annotated)
- Committer:
- salemtang
- Date:
- Sat Sep 27 02:26:19 2014 +0000
- Revision:
- 2:678c0be8c23d
- Parent:
- 1:34f0bfc62803
- Child:
- 3:fb38dbc95b12
20140927_1126 modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
salemtang | 2:678c0be8c23d | 1 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 2 | //======== INCLUDES ===========================================// |
salemtang | 2:678c0be8c23d | 3 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
star297 | 0:4f67859595b2 | 4 | #include "mbed.h" |
star297 | 0:4f67859595b2 | 5 | #include "SLCD.h" |
star297 | 0:4f67859595b2 | 6 | |
salemtang | 2:678c0be8c23d | 7 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 8 | //======== DEFINES ============================================// |
salemtang | 2:678c0be8c23d | 9 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
star297 | 0:4f67859595b2 | 10 | |
salemtang | 2:678c0be8c23d | 11 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 12 | //======== PROTOTYPES =========================================// |
salemtang | 2:678c0be8c23d | 13 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
salemtang | 2:678c0be8c23d | 14 | void scroll_message (void); |
salemtang | 2:678c0be8c23d | 15 | void setMinuteIRQ (void); |
salemtang | 2:678c0be8c23d | 16 | void setHourIRQ (void); |
salemtang | 2:678c0be8c23d | 17 | |
salemtang | 2:678c0be8c23d | 18 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 19 | //======== VARIABLES ==========================================// |
salemtang | 2:678c0be8c23d | 20 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
salemtang | 2:678c0be8c23d | 21 | DigitalOut led1_DO(LED1); |
salemtang | 2:678c0be8c23d | 22 | DigitalOut led2_DO(LED2); |
salemtang | 2:678c0be8c23d | 23 | InterruptIn setMinute(SW1); |
salemtang | 2:678c0be8c23d | 24 | InterruptIn setHour(SW3); |
salemtang | 2:678c0be8c23d | 25 | time_t seconds = time(NULL); // needed to start rtc on reset to maintain reasonable time if hard reset |
star297 | 0:4f67859595b2 | 26 | SLCD slcd; |
star297 | 0:4f67859595b2 | 27 | Timer scroll; |
star297 | 0:4f67859595b2 | 28 | struct tm t; |
salemtang | 2:678c0be8c23d | 29 | int i, j, k, lastscroll, display_timer, minute, hour, colon, dp; |
star297 | 1:34f0bfc62803 | 30 | char message[60]; |
star297 | 0:4f67859595b2 | 31 | char buffer[32]; |
star297 | 0:4f67859595b2 | 32 | |
salemtang | 2:678c0be8c23d | 33 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 34 | //======== MAIN() =============================================// |
salemtang | 2:678c0be8c23d | 35 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
salemtang | 2:678c0be8c23d | 36 | int main () |
star297 | 1:34f0bfc62803 | 37 | { |
star297 | 1:34f0bfc62803 | 38 | slcd.All_Segments(1); |
star297 | 1:34f0bfc62803 | 39 | wait(2); |
star297 | 1:34f0bfc62803 | 40 | slcd.All_Segments(0); |
star297 | 1:34f0bfc62803 | 41 | wait(1); |
salemtang | 2:678c0be8c23d | 42 | |
salemtang | 2:678c0be8c23d | 43 | led1_DO = 1; |
salemtang | 2:678c0be8c23d | 44 | led2_DO = 1; |
salemtang | 2:678c0be8c23d | 45 | |
star297 | 1:34f0bfc62803 | 46 | sprintf(message, " rtc clock s3 sets the hours s1 sets the minutes"); |
salemtang | 2:678c0be8c23d | 47 | |
star297 | 1:34f0bfc62803 | 48 | // scrolling message |
star297 | 1:34f0bfc62803 | 49 | scroll.start(); |
salemtang | 2:678c0be8c23d | 50 | while (i < 58) { |
salemtang | 2:678c0be8c23d | 51 | while (i < 58) { |
star297 | 1:34f0bfc62803 | 52 | scroll_message(); |
star297 | 1:34f0bfc62803 | 53 | } |
star297 | 1:34f0bfc62803 | 54 | } |
star297 | 1:34f0bfc62803 | 55 | wait(1); |
star297 | 1:34f0bfc62803 | 56 | |
salemtang | 2:678c0be8c23d | 57 | setMinute.rise(setMinuteIRQ); // start set Minutes IRQ |
salemtang | 2:678c0be8c23d | 58 | setHour.rise(setHourIRQ); // start set Hours IRQ |
star297 | 0:4f67859595b2 | 59 | |
star297 | 1:34f0bfc62803 | 60 | // rtc clock function |
salemtang | 2:678c0be8c23d | 61 | while (1) { |
star297 | 1:34f0bfc62803 | 62 | time_t seconds = time(NULL); |
star297 | 1:34f0bfc62803 | 63 | |
salemtang | 2:678c0be8c23d | 64 | if (display_timer > 6) { |
salemtang | 2:678c0be8c23d | 65 | strftime(buffer, 4, "%H%M", localtime(&seconds)); // display Hours,Minutes for 2 seconds |
salemtang | 2:678c0be8c23d | 66 | slcd.Colon(1); |
salemtang | 2:678c0be8c23d | 67 | led2_DO = 0; |
salemtang | 2:678c0be8c23d | 68 | slcd.DP2(0); |
salemtang | 2:678c0be8c23d | 69 | led1_DO = 1; |
star297 | 1:34f0bfc62803 | 70 | } else { |
salemtang | 2:678c0be8c23d | 71 | strftime(buffer, 4, "%M%S", localtime(&seconds)); // display Minutes,Seconds for 8 seconds |
salemtang | 2:678c0be8c23d | 72 | slcd.Colon(0); |
salemtang | 2:678c0be8c23d | 73 | led2_DO = 1; |
salemtang | 2:678c0be8c23d | 74 | slcd.DP2(1); |
salemtang | 2:678c0be8c23d | 75 | led1_DO = 0; |
star297 | 0:4f67859595b2 | 76 | } |
star297 | 1:34f0bfc62803 | 77 | slcd.printf(buffer); |
salemtang | 2:678c0be8c23d | 78 | wait(0.5); |
salemtang | 2:678c0be8c23d | 79 | slcd.DP2(0); |
salemtang | 2:678c0be8c23d | 80 | led1_DO = 1; |
star297 | 1:34f0bfc62803 | 81 | display_timer++; |
salemtang | 2:678c0be8c23d | 82 | if (display_timer > 9) display_timer = 0; |
salemtang | 2:678c0be8c23d | 83 | wait(0.5); |
star297 | 0:4f67859595b2 | 84 | } |
star297 | 1:34f0bfc62803 | 85 | } |
star297 | 0:4f67859595b2 | 86 | |
salemtang | 2:678c0be8c23d | 87 | //_____________________________________________________________// |
salemtang | 2:678c0be8c23d | 88 | //======== FUNC() =============================================// |
salemtang | 2:678c0be8c23d | 89 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
salemtang | 2:678c0be8c23d | 90 | // message control |
salemtang | 2:678c0be8c23d | 91 | void scroll_message (void) |
star297 | 0:4f67859595b2 | 92 | { |
star297 | 1:34f0bfc62803 | 93 | if (scroll.read_ms() > lastscroll + 350) { |
star297 | 0:4f67859595b2 | 94 | scroll.reset(); |
star297 | 1:34f0bfc62803 | 95 | if (i > 58) { |
star297 | 0:4f67859595b2 | 96 | i=0; |
star297 | 0:4f67859595b2 | 97 | } |
star297 | 0:4f67859595b2 | 98 | int j, k = i; |
star297 | 0:4f67859595b2 | 99 | for (j = 0; j < 4; j++) { |
star297 | 0:4f67859595b2 | 100 | if (message[k+j]) { |
star297 | 0:4f67859595b2 | 101 | slcd.putc(message[k+j]); |
star297 | 0:4f67859595b2 | 102 | } else { |
star297 | 0:4f67859595b2 | 103 | slcd.putc(' '); |
star297 | 0:4f67859595b2 | 104 | k--; |
star297 | 0:4f67859595b2 | 105 | } |
star297 | 0:4f67859595b2 | 106 | } |
star297 | 0:4f67859595b2 | 107 | i++; |
salemtang | 2:678c0be8c23d | 108 | lastscroll = scroll.read_ms(); |
star297 | 0:4f67859595b2 | 109 | } |
star297 | 0:4f67859595b2 | 110 | } |
star297 | 0:4f67859595b2 | 111 | |
salemtang | 2:678c0be8c23d | 112 | // set Minutes ISR |
salemtang | 2:678c0be8c23d | 113 | void setMinuteIRQ (void) |
star297 | 0:4f67859595b2 | 114 | { |
salemtang | 2:678c0be8c23d | 115 | display_timer = 7; |
star297 | 0:4f67859595b2 | 116 | time_t seconds = time(NULL); |
star297 | 1:34f0bfc62803 | 117 | char buffer[2]; |
star297 | 0:4f67859595b2 | 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)); |
salemtang | 2:678c0be8c23d | 121 | minute = atoi(buffer); // get Minutes integer |
star297 | 0:4f67859595b2 | 122 | minute++; |
salemtang | 2:678c0be8c23d | 123 | if (minute > 59) minute = 0; |
salemtang | 2:678c0be8c23d | 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; |
salemtang | 2:678c0be8c23d | 130 | set_time(mktime(&t)); |
star297 | 0:4f67859595b2 | 131 | } |
star297 | 0:4f67859595b2 | 132 | |
salemtang | 2:678c0be8c23d | 133 | // set Hours ISR |
salemtang | 2:678c0be8c23d | 134 | void setHourIRQ (void) |
star297 | 0:4f67859595b2 | 135 | { |
salemtang | 2:678c0be8c23d | 136 | display_timer = 7; |
star297 | 0:4f67859595b2 | 137 | time_t seconds = time(NULL); |
star297 | 0:4f67859595b2 | 138 | char buffer[2]; |
star297 | 1:34f0bfc62803 | 139 | strftime(buffer, 2,"%H", localtime(&seconds)); |
star297 | 1:34f0bfc62803 | 140 | hour = atoi(buffer); // get Hour integer |
star297 | 0:4f67859595b2 | 141 | strftime(buffer, 2,"%M", localtime(&seconds)); |
salemtang | 2:678c0be8c23d | 142 | minute = atoi(buffer); // get Minutes integer |
star297 | 0:4f67859595b2 | 143 | hour++; |
salemtang | 2:678c0be8c23d | 144 | if (hour > 23) hour = 0; |
salemtang | 2:678c0be8c23d | 145 | t.tm_sec = 0; // Seconds reset to zero |
star297 | 1:34f0bfc62803 | 146 | t.tm_min = minute; |
star297 | 1:34f0bfc62803 | 147 | t.tm_hour = hour; |
star297 | 1:34f0bfc62803 | 148 | t.tm_mday = 1; |
star297 | 1:34f0bfc62803 | 149 | t.tm_mon = 2; |
star297 | 1:34f0bfc62803 | 150 | t.tm_year = 114; |
salemtang | 2:678c0be8c23d | 151 | set_time(mktime(&t)); |
star297 | 0:4f67859595b2 | 152 | } |