Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Maple.cpp@1:aefa1992ce0f, 2011-10-10 (annotated)
- Committer:
- uehara00
- Date:
- Mon Oct 10 11:45:51 2011 +0000
- Revision:
- 1:aefa1992ce0f
- Parent:
- 0:0be38b583cf7
- Child:
- 3:eec13a411e94
Date/time display to LCD, adjust by buttons, alarm, timer, clock-output control. Just an example of RTC funtions, not for a practical use.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| uehara00 | 1:aefa1992ce0f | 1 | //copyright 2011 Uehara Yoshiyuki |
| uehara00 | 1:aefa1992ce0f | 2 | //==================================================================== |
| uehara00 | 1:aefa1992ce0f | 3 | //The author provide the programs without any guarantees or warranty. |
| uehara00 | 1:aefa1992ce0f | 4 | //The author is not responsible for any damage or losses of any kind |
| uehara00 | 1:aefa1992ce0f | 5 | //caused by using or misusing of the programs. |
| uehara00 | 1:aefa1992ce0f | 6 | //The author is under no obligation to provide support, service, |
| uehara00 | 1:aefa1992ce0f | 7 | //corrections, or upgrades to the programs. |
| uehara00 | 1:aefa1992ce0f | 8 | //==================================================================== |
| uehara00 | 0:0be38b583cf7 | 9 | // MAPLE board[MARM01-BASE] |
| uehara00 | 0:0be38b583cf7 | 10 | // common functions |
| uehara00 | 0:0be38b583cf7 | 11 | #include "Maple_RTC.h" |
| uehara00 | 0:0be38b583cf7 | 12 | #include "Maple_I2C.h" |
| uehara00 | 0:0be38b583cf7 | 13 | #include "Maple_LCD.h" |
| uehara00 | 0:0be38b583cf7 | 14 | #include "Maple.h" |
| uehara00 | 0:0be38b583cf7 | 15 | #include "mbed.h" |
| uehara00 | 0:0be38b583cf7 | 16 | |
| uehara00 | 1:aefa1992ce0f | 17 | // string copy |
| uehara00 | 1:aefa1992ce0f | 18 | char* copy_string(char destination_string[], int destination_position, int copy_length, const char source_string[]) { |
| uehara00 | 1:aefa1992ce0f | 19 | for(int i = 0; i < copy_length; ++i) { |
| uehara00 | 1:aefa1992ce0f | 20 | destination_string[destination_position + i] = source_string[i]; |
| uehara00 | 1:aefa1992ce0f | 21 | } |
| uehara00 | 1:aefa1992ce0f | 22 | return destination_string; |
| uehara00 | 0:0be38b583cf7 | 23 | } |
| uehara00 | 0:0be38b583cf7 | 24 | |
| uehara00 | 1:aefa1992ce0f | 25 | // integer(4 bits) to hexadecimal character(1 digit) |
| uehara00 | 1:aefa1992ce0f | 26 | char int_to_hex1(int i) { |
| uehara00 | 1:aefa1992ce0f | 27 | const char hex[] = "0123456789abcdef"; |
| uehara00 | 1:aefa1992ce0f | 28 | |
| uehara00 | 1:aefa1992ce0f | 29 | return hex[i & 0x0f]; |
| uehara00 | 0:0be38b583cf7 | 30 | } |
| uehara00 | 0:0be38b583cf7 | 31 | |
| uehara00 | 1:aefa1992ce0f | 32 | // integer(1 byte) to hexadecimal character(2 digits) |
| uehara00 | 1:aefa1992ce0f | 33 | char* int_to_hex2(int i, char h[]) { |
| uehara00 | 1:aefa1992ce0f | 34 | h[0] = int_to_hex1(i >> 4); |
| uehara00 | 1:aefa1992ce0f | 35 | h[1] = int_to_hex1(i); |
| uehara00 | 1:aefa1992ce0f | 36 | h[2] = '\0'; |
| uehara00 | 1:aefa1992ce0f | 37 | return h; |
| uehara00 | 1:aefa1992ce0f | 38 | } |
| uehara00 | 1:aefa1992ce0f | 39 | |
| uehara00 | 1:aefa1992ce0f | 40 | // BCD(1 byte) to integer |
| uehara00 | 0:0be38b583cf7 | 41 | int bcd_to_int(char b) { |
| uehara00 | 0:0be38b583cf7 | 42 | return ((b >> 4) * 10) + (b & 0x0f); |
| uehara00 | 0:0be38b583cf7 | 43 | } |
| uehara00 | 0:0be38b583cf7 | 44 | |
| uehara00 | 1:aefa1992ce0f | 45 | // integer to BCD(1 byte) |
| uehara00 | 0:0be38b583cf7 | 46 | char int_to_bcd(int i) { |
| uehara00 | 0:0be38b583cf7 | 47 | return ((i / 10) << 4) + (i % 10); |
| uehara00 | 0:0be38b583cf7 | 48 | } |
| uehara00 | 0:0be38b583cf7 | 49 | |
| uehara00 | 1:aefa1992ce0f | 50 | // decrement/increment BCD |
| uehara00 | 1:aefa1992ce0f | 51 | // flag 0:decrement, 1:increment |
| uehara00 | 1:aefa1992ce0f | 52 | // min, max |
| uehara00 | 0:0be38b583cf7 | 53 | // 0x00, 0x99 .. 00-99: year 0x00 |
| uehara00 | 0:0be38b583cf7 | 54 | // 0x01, 0x12 .. 01-12: month |
| uehara00 | 0:0be38b583cf7 | 55 | // 0x01, max .. 01-max: day |
| uehara00 | 0:0be38b583cf7 | 56 | // 0x00, 0x23 .. 00-23: hour |
| uehara00 | 0:0be38b583cf7 | 57 | // 0x00, 0x59 .. 00-59: minute, second |
| uehara00 | 1:aefa1992ce0f | 58 | char xxcrement_bcd(int flag, char bcd_data, char bcd_min, char bcd_max) { |
| uehara00 | 1:aefa1992ce0f | 59 | if(flag == 0) { |
| uehara00 | 1:aefa1992ce0f | 60 | if(bcd_data < (bcd_min + 0x01)) { |
| uehara00 | 1:aefa1992ce0f | 61 | bcd_data = bcd_max; |
| uehara00 | 1:aefa1992ce0f | 62 | } |
| uehara00 | 1:aefa1992ce0f | 63 | else if((bcd_data & 0x0f) == 0x00) { |
| uehara00 | 1:aefa1992ce0f | 64 | bcd_data -= 0x07; // - 0x10 + 0x0a - 0x01; |
| uehara00 | 1:aefa1992ce0f | 65 | } |
| uehara00 | 1:aefa1992ce0f | 66 | else { |
| uehara00 | 1:aefa1992ce0f | 67 | bcd_data -= 0x01; |
| uehara00 | 1:aefa1992ce0f | 68 | } |
| uehara00 | 1:aefa1992ce0f | 69 | return bcd_data; |
| uehara00 | 0:0be38b583cf7 | 70 | } |
| uehara00 | 0:0be38b583cf7 | 71 | else { |
| uehara00 | 1:aefa1992ce0f | 72 | if((bcd_data & 0x0f) == 0x09) { |
| uehara00 | 1:aefa1992ce0f | 73 | bcd_data += 0x07; // + 0x10 - 0x0a + 0x01 |
| uehara00 | 1:aefa1992ce0f | 74 | } |
| uehara00 | 1:aefa1992ce0f | 75 | else { |
| uehara00 | 1:aefa1992ce0f | 76 | bcd_data += 0x01; |
| uehara00 | 1:aefa1992ce0f | 77 | } |
| uehara00 | 1:aefa1992ce0f | 78 | if(bcd_data > bcd_max) { |
| uehara00 | 1:aefa1992ce0f | 79 | bcd_data = bcd_min; |
| uehara00 | 1:aefa1992ce0f | 80 | } |
| uehara00 | 1:aefa1992ce0f | 81 | return bcd_data; |
| uehara00 | 0:0be38b583cf7 | 82 | } |
| uehara00 | 0:0be38b583cf7 | 83 | } |
| uehara00 | 0:0be38b583cf7 | 84 | |
| uehara00 | 1:aefa1992ce0f | 85 | // weekday to string(3 characters) |
| uehara00 | 1:aefa1992ce0f | 86 | // 0(SUN) .. 6(SAT) |
| uehara00 | 1:aefa1992ce0f | 87 | const char* weekday_to_string(int w, const char* s) { |
| uehara00 | 1:aefa1992ce0f | 88 | const char* weekday_string[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; |
| uehara00 | 0:0be38b583cf7 | 89 | |
| uehara00 | 1:aefa1992ce0f | 90 | s = weekday_string[w]; |
| uehara00 | 1:aefa1992ce0f | 91 | return s; |
| uehara00 | 0:0be38b583cf7 | 92 | } |
| uehara00 | 0:0be38b583cf7 | 93 | |
| uehara00 | 0:0be38b583cf7 | 94 | // year in 2cyy format |
| uehara00 | 1:aefa1992ce0f | 95 | // bcd_century .. 0x00/0x01 |
| uehara00 | 0:0be38b583cf7 | 96 | int bcd_to_year(char century, char bcd_year) { |
| uehara00 | 0:0be38b583cf7 | 97 | return 2000 + (century * 100) + bcd_to_int(bcd_year); |
| uehara00 | 0:0be38b583cf7 | 98 | } |
| uehara00 | 0:0be38b583cf7 | 99 | |
| uehara00 | 0:0be38b583cf7 | 100 | // check leap year |
| uehara00 | 0:0be38b583cf7 | 101 | // false: no, true:yes |
| uehara00 | 0:0be38b583cf7 | 102 | bool leap_year(int year) { |
| uehara00 | 0:0be38b583cf7 | 103 | return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); |
| uehara00 | 0:0be38b583cf7 | 104 | } |
| uehara00 | 0:0be38b583cf7 | 105 | |
| uehara00 | 0:0be38b583cf7 | 106 | // days in a month |
| uehara00 | 0:0be38b583cf7 | 107 | int days_in_month(int year, int month) { |
| uehara00 | 0:0be38b583cf7 | 108 | const int days_leap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| uehara00 | 0:0be38b583cf7 | 109 | const int days_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| uehara00 | 0:0be38b583cf7 | 110 | |
| uehara00 | 1:aefa1992ce0f | 111 | if(leap_year(year)) { |
| uehara00 | 0:0be38b583cf7 | 112 | return days_leap[month]; |
| uehara00 | 0:0be38b583cf7 | 113 | } |
| uehara00 | 0:0be38b583cf7 | 114 | else { |
| uehara00 | 0:0be38b583cf7 | 115 | return days_common[month]; |
| uehara00 | 0:0be38b583cf7 | 116 | } |
| uehara00 | 0:0be38b583cf7 | 117 | } |
| uehara00 | 0:0be38b583cf7 | 118 | |
| uehara00 | 1:aefa1992ce0f | 119 | // days(BCD in a month in BCD |
| uehara00 | 1:aefa1992ce0f | 120 | char bcd_days_in_month(char bcd_century, char bcd_year, char bcd_month) { |
| uehara00 | 1:aefa1992ce0f | 121 | return int_to_bcd(days_in_month(bcd_to_year(bcd_century, bcd_year), bcd_to_int(bcd_month))); |
| uehara00 | 0:0be38b583cf7 | 122 | } |
| uehara00 | 0:0be38b583cf7 | 123 | |
| uehara00 | 0:0be38b583cf7 | 124 | // Zeller's congruence for Calendario gregoriano |
| uehara00 | 0:0be38b583cf7 | 125 | // 0:SUN .. 6:SAT |
| uehara00 | 0:0be38b583cf7 | 126 | int bcd_date_to_weekday(char bcd_century, char bcd_year, char bcd_month, char bcd_day) { |
| uehara00 | 0:0be38b583cf7 | 127 | int century, year, month, day; |
| uehara00 | 0:0be38b583cf7 | 128 | |
| uehara00 | 0:0be38b583cf7 | 129 | century = 20 + bcd_century; |
| uehara00 | 1:aefa1992ce0f | 130 | year = bcd_to_int(bcd_year); |
| uehara00 | 1:aefa1992ce0f | 131 | month = bcd_to_int(bcd_month); |
| uehara00 | 1:aefa1992ce0f | 132 | day = bcd_to_int(bcd_day); |
| uehara00 | 0:0be38b583cf7 | 133 | return (day + (((month + 1) * 26 ) / 10) + year + year / 4 + century / 4 - century * 2 + 6) % 7; |
| uehara00 | 0:0be38b583cf7 | 134 | } |
| uehara00 | 0:0be38b583cf7 | 135 | |
| uehara00 | 1:aefa1992ce0f | 136 | // test |
| uehara00 | 1:aefa1992ce0f | 137 | // RTC read and raw print to LCD |
| uehara00 | 0:0be38b583cf7 | 138 | void RTC_to_LCD_raw() { |
| uehara00 | 0:0be38b583cf7 | 139 | char d[17]; |
| uehara00 | 0:0be38b583cf7 | 140 | |
| uehara00 | 0:0be38b583cf7 | 141 | i2c_RTC_read(RTC_REG_CONTROL1, d, 16); |
| uehara00 | 0:0be38b583cf7 | 142 | LCD_locate(0, 0); |
| uehara00 | 0:0be38b583cf7 | 143 | for(int i = 0; i < 8; ++i) { |
| uehara00 | 0:0be38b583cf7 | 144 | LCD_print_hex(d[i]); |
| uehara00 | 0:0be38b583cf7 | 145 | } |
| uehara00 | 0:0be38b583cf7 | 146 | LCD_locate(1, 0); |
| uehara00 | 0:0be38b583cf7 | 147 | for(int i = 8; i < 16; ++i) { |
| uehara00 | 0:0be38b583cf7 | 148 | LCD_print_hex(d[i]); |
| uehara00 | 0:0be38b583cf7 | 149 | } |
| uehara00 | 0:0be38b583cf7 | 150 | } |
| uehara00 | 0:0be38b583cf7 | 151 | |
| uehara00 | 1:aefa1992ce0f | 152 | // test |
| uehara00 | 1:aefa1992ce0f | 153 | // LCD display 64 characters in 1st/2nd row and shift |
| uehara00 | 0:0be38b583cf7 | 154 | // |
| uehara00 | 0:0be38b583cf7 | 155 | void LCD_display_all_char_shift() { |
| uehara00 | 0:0be38b583cf7 | 156 | LCD_display_32char_shift(0x00); |
| uehara00 | 0:0be38b583cf7 | 157 | LCD_display_32char_shift(0x40); |
| uehara00 | 0:0be38b583cf7 | 158 | LCD_display_32char_shift(0x80); |
| uehara00 | 0:0be38b583cf7 | 159 | LCD_display_32char_shift(0xC0); |
| uehara00 | 0:0be38b583cf7 | 160 | } |
| uehara00 | 0:0be38b583cf7 | 161 | |
| uehara00 | 0:0be38b583cf7 | 162 | static void LCD_display_32char_shift(char base) { |
| uehara00 | 1:aefa1992ce0f | 163 | LCD_clear_display(); // select 1st row and clear display |
| uehara00 | 1:aefa1992ce0f | 164 | for(int i = 0; i < 32; ++i) { |
| uehara00 | 1:aefa1992ce0f | 165 | LCD_print_char(base + i); // write 32 characters |
| uehara00 | 0:0be38b583cf7 | 166 | } |
| uehara00 | 1:aefa1992ce0f | 167 | LCD_locate(1, 0); // select 2nd row |
| uehara00 | 1:aefa1992ce0f | 168 | for(int i = 32; i < 64; ++i) { |
| uehara00 | 1:aefa1992ce0f | 169 | LCD_print_char(base + i); // write 32 characters |
| uehara00 | 0:0be38b583cf7 | 170 | } |
| uehara00 | 1:aefa1992ce0f | 171 | for(int i = 0; i < 24; ++i) { |
| uehara00 | 1:aefa1992ce0f | 172 | LCD_cursor_or_display_shift(1, 0); // shift right 24 times |
| uehara00 | 0:0be38b583cf7 | 173 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 174 | } |
| uehara00 | 1:aefa1992ce0f | 175 | for(int i = 0; i < 32; ++i) { |
| uehara00 | 1:aefa1992ce0f | 176 | LCD_cursor_or_display_shift(1, 1); // shift left 32 times |
| uehara00 | 0:0be38b583cf7 | 177 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 178 | } |
| uehara00 | 1:aefa1992ce0f | 179 | for(int i = 0; i < 8; ++i) { |
| uehara00 | 1:aefa1992ce0f | 180 | LCD_cursor_or_display_shift(1, 0); // shift right 8 times |
| uehara00 | 0:0be38b583cf7 | 181 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 182 | } |
| uehara00 | 0:0be38b583cf7 | 183 | } |
| uehara00 | 0:0be38b583cf7 | 184 | |
| uehara00 | 1:aefa1992ce0f | 185 | // test |
| uehara00 | 1:aefa1992ce0f | 186 | // LCD display 16x2 characters with 1st and 2nd row |
| uehara00 | 0:0be38b583cf7 | 187 | void LCD_display_all_char() { |
| uehara00 | 0:0be38b583cf7 | 188 | LCD_display_32char(0x00); |
| uehara00 | 0:0be38b583cf7 | 189 | LCD_display_32char(0x20); |
| uehara00 | 0:0be38b583cf7 | 190 | LCD_display_32char(0x40); |
| uehara00 | 0:0be38b583cf7 | 191 | LCD_display_32char(0x60); |
| uehara00 | 0:0be38b583cf7 | 192 | LCD_display_32char(0x80); |
| uehara00 | 0:0be38b583cf7 | 193 | LCD_display_32char(0xa0); |
| uehara00 | 0:0be38b583cf7 | 194 | LCD_display_32char(0xc0); |
| uehara00 | 0:0be38b583cf7 | 195 | LCD_display_32char(0xe0); |
| uehara00 | 0:0be38b583cf7 | 196 | } |
| uehara00 | 0:0be38b583cf7 | 197 | |
| uehara00 | 0:0be38b583cf7 | 198 | static void LCD_display_32char(char base) { |
| uehara00 | 1:aefa1992ce0f | 199 | LCD_clear_display(); // select 1st row and clear display |
| uehara00 | 1:aefa1992ce0f | 200 | for(int i = 0; i < 16; ++i) { |
| uehara00 | 1:aefa1992ce0f | 201 | LCD_print_char(base + i); // write 1st-half 16 characters |
| uehara00 | 0:0be38b583cf7 | 202 | } |
| uehara00 | 1:aefa1992ce0f | 203 | LCD_locate(1, 0); // select 2nd row |
| uehara00 | 1:aefa1992ce0f | 204 | for(int i = 16; i < 32; ++i) { |
| uehara00 | 1:aefa1992ce0f | 205 | LCD_print_char(base + i); // write 2nd-half 16 characters |
| uehara00 | 0:0be38b583cf7 | 206 | } |
| uehara00 | 0:0be38b583cf7 | 207 | wait_ms(3000); |
| uehara00 | 0:0be38b583cf7 | 208 | } |