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@0:0be38b583cf7, 2011-09-25 (annotated)
- Committer:
- uehara00
- Date:
- Sun Sep 25 11:37:21 2011 +0000
- Revision:
- 0:0be38b583cf7
- Child:
- 1:aefa1992ce0f
Preliminary. Alarm functions are not yet implimented.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| uehara00 | 0:0be38b583cf7 | 1 | // MAPLE board[MARM01-BASE] |
| uehara00 | 0:0be38b583cf7 | 2 | // common functions |
| uehara00 | 0:0be38b583cf7 | 3 | // |
| uehara00 | 0:0be38b583cf7 | 4 | #include "Maple_RTC.h" |
| uehara00 | 0:0be38b583cf7 | 5 | #include "Maple_I2C.h" |
| uehara00 | 0:0be38b583cf7 | 6 | #include "Maple_LCD.h" |
| uehara00 | 0:0be38b583cf7 | 7 | #include "Maple.h" |
| uehara00 | 0:0be38b583cf7 | 8 | #include "mbed.h" |
| uehara00 | 0:0be38b583cf7 | 9 | |
| uehara00 | 0:0be38b583cf7 | 10 | // char to hexadecimal (4 bits to 1 hexadecimal digit) |
| uehara00 | 0:0be38b583cf7 | 11 | char char_to_hex1(int c) { |
| uehara00 | 0:0be38b583cf7 | 12 | const char hex_code[] = "0123456789abcdef"; |
| uehara00 | 0:0be38b583cf7 | 13 | |
| uehara00 | 0:0be38b583cf7 | 14 | return hex_code[c]; |
| uehara00 | 0:0be38b583cf7 | 15 | } |
| uehara00 | 0:0be38b583cf7 | 16 | |
| uehara00 | 0:0be38b583cf7 | 17 | // char to hexadecimal (1 byte, 2 hexadecimal digits) |
| uehara00 | 0:0be38b583cf7 | 18 | void char_to_hex(int c, char h[]) { |
| uehara00 | 0:0be38b583cf7 | 19 | h[0] = char_to_hex1(c >> 4); |
| uehara00 | 0:0be38b583cf7 | 20 | h[1] = char_to_hex1(c & 0x0f); |
| uehara00 | 0:0be38b583cf7 | 21 | h[2] = '\0'; |
| uehara00 | 0:0be38b583cf7 | 22 | } |
| uehara00 | 0:0be38b583cf7 | 23 | |
| uehara00 | 0:0be38b583cf7 | 24 | // BCD to integer |
| uehara00 | 0:0be38b583cf7 | 25 | int bcd_to_int(char b) { |
| uehara00 | 0:0be38b583cf7 | 26 | return ((b >> 4) * 10) + (b & 0x0f); |
| uehara00 | 0:0be38b583cf7 | 27 | } |
| uehara00 | 0:0be38b583cf7 | 28 | |
| uehara00 | 0:0be38b583cf7 | 29 | // integer to BCD |
| uehara00 | 0:0be38b583cf7 | 30 | char int_to_bcd(int i) { |
| uehara00 | 0:0be38b583cf7 | 31 | return ((i / 10) << 4) + (i % 10); |
| uehara00 | 0:0be38b583cf7 | 32 | } |
| uehara00 | 0:0be38b583cf7 | 33 | |
| uehara00 | 0:0be38b583cf7 | 34 | // increment BCD |
| uehara00 | 0:0be38b583cf7 | 35 | // (min, max+1) |
| uehara00 | 0:0be38b583cf7 | 36 | // 0x00, 0x99 .. 00-99: year 0x00 |
| uehara00 | 0:0be38b583cf7 | 37 | // 0x01, 0x12 .. 01-12: month |
| uehara00 | 0:0be38b583cf7 | 38 | // 0x01, max .. 01-max: day |
| uehara00 | 0:0be38b583cf7 | 39 | // 0x00, 0x23 .. 00-23: hour |
| uehara00 | 0:0be38b583cf7 | 40 | // 0x00, 0x59 .. 00-59: minute, second |
| uehara00 | 0:0be38b583cf7 | 41 | char increment_bcd(char bcd_data, char bcd_min, char bcd_max) { |
| uehara00 | 0:0be38b583cf7 | 42 | if((bcd_data & 0x0f) == 0x09) { |
| uehara00 | 0:0be38b583cf7 | 43 | bcd_data += 0x07; // + 0x10 - 0x0a + 0x01 |
| uehara00 | 0:0be38b583cf7 | 44 | } |
| uehara00 | 0:0be38b583cf7 | 45 | else { |
| uehara00 | 0:0be38b583cf7 | 46 | bcd_data += 0x01; |
| uehara00 | 0:0be38b583cf7 | 47 | } |
| uehara00 | 0:0be38b583cf7 | 48 | if(bcd_data > bcd_max) { |
| uehara00 | 0:0be38b583cf7 | 49 | bcd_data = bcd_min; |
| uehara00 | 0:0be38b583cf7 | 50 | } |
| uehara00 | 0:0be38b583cf7 | 51 | return bcd_data; |
| uehara00 | 0:0be38b583cf7 | 52 | } |
| uehara00 | 0:0be38b583cf7 | 53 | |
| uehara00 | 0:0be38b583cf7 | 54 | // decrement bcd |
| uehara00 | 0:0be38b583cf7 | 55 | // (min, max+1) |
| uehara00 | 0:0be38b583cf7 | 56 | // 0x00, 0x99 .. 00-99: year 0x00 |
| uehara00 | 0:0be38b583cf7 | 57 | // 0x01, 0x12 .. 01-12: month |
| uehara00 | 0:0be38b583cf7 | 58 | // 0x01, max .. 01-max: day |
| uehara00 | 0:0be38b583cf7 | 59 | // 0x00, 0x23 .. 00-23: hour |
| uehara00 | 0:0be38b583cf7 | 60 | // 0x00, 0x59 .. 00-59: minute, second |
| uehara00 | 0:0be38b583cf7 | 61 | char decrement_bcd(char bcd_data, char bcd_min, char bcd_max) { |
| uehara00 | 0:0be38b583cf7 | 62 | if(bcd_data < (bcd_min + 0x01)) { |
| uehara00 | 0:0be38b583cf7 | 63 | bcd_data = bcd_max; |
| uehara00 | 0:0be38b583cf7 | 64 | } |
| uehara00 | 0:0be38b583cf7 | 65 | else if((bcd_data & 0x0f) == 0x00) { |
| uehara00 | 0:0be38b583cf7 | 66 | bcd_data -= 0x07; // - 0x10 + 0x0a - 0x01; |
| uehara00 | 0:0be38b583cf7 | 67 | } |
| uehara00 | 0:0be38b583cf7 | 68 | else { |
| uehara00 | 0:0be38b583cf7 | 69 | bcd_data -= 0x01; |
| uehara00 | 0:0be38b583cf7 | 70 | } |
| uehara00 | 0:0be38b583cf7 | 71 | return bcd_data; |
| uehara00 | 0:0be38b583cf7 | 72 | } |
| uehara00 | 0:0be38b583cf7 | 73 | |
| uehara00 | 0:0be38b583cf7 | 74 | // weekday string(2 characters) |
| uehara00 | 0:0be38b583cf7 | 75 | // weekday: 0(SU) .. 6(SA) |
| uehara00 | 0:0be38b583cf7 | 76 | void int_to_weekday2(int weekday, char s[]) { |
| uehara00 | 0:0be38b583cf7 | 77 | const char weekday_string2[7][3] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; |
| uehara00 | 0:0be38b583cf7 | 78 | |
| uehara00 | 0:0be38b583cf7 | 79 | s[0] = weekday_string2[weekday][0]; |
| uehara00 | 0:0be38b583cf7 | 80 | s[1] = weekday_string2[weekday][1]; |
| uehara00 | 0:0be38b583cf7 | 81 | s[2] = '\0'; |
| uehara00 | 0:0be38b583cf7 | 82 | } |
| uehara00 | 0:0be38b583cf7 | 83 | |
| uehara00 | 0:0be38b583cf7 | 84 | // weekday string(3 characters) |
| uehara00 | 0:0be38b583cf7 | 85 | // weekday: 0(SUN) .. 6(SAT) |
| uehara00 | 0:0be38b583cf7 | 86 | void int_to_weekday3(int weekday, char s[]) { |
| uehara00 | 0:0be38b583cf7 | 87 | const char weekday_string3[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; |
| uehara00 | 0:0be38b583cf7 | 88 | |
| uehara00 | 0:0be38b583cf7 | 89 | s[0] = weekday_string3[weekday][0]; |
| uehara00 | 0:0be38b583cf7 | 90 | s[1] = weekday_string3[weekday][1]; |
| uehara00 | 0:0be38b583cf7 | 91 | s[2] = weekday_string3[weekday][2]; |
| uehara00 | 0:0be38b583cf7 | 92 | s[3] = '\0'; |
| uehara00 | 0:0be38b583cf7 | 93 | } |
| uehara00 | 0:0be38b583cf7 | 94 | |
| uehara00 | 0:0be38b583cf7 | 95 | // year in 2cyy format |
| uehara00 | 0:0be38b583cf7 | 96 | // c: century .. 0x00/0x01 |
| uehara00 | 0:0be38b583cf7 | 97 | int bcd_to_year(char century, char bcd_year) { |
| uehara00 | 0:0be38b583cf7 | 98 | return 2000 + (century * 100) + bcd_to_int(bcd_year); |
| uehara00 | 0:0be38b583cf7 | 99 | } |
| uehara00 | 0:0be38b583cf7 | 100 | |
| uehara00 | 0:0be38b583cf7 | 101 | // check leap year |
| uehara00 | 0:0be38b583cf7 | 102 | // false: no, true:yes |
| uehara00 | 0:0be38b583cf7 | 103 | bool leap_year(int year) { |
| uehara00 | 0:0be38b583cf7 | 104 | return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); |
| uehara00 | 0:0be38b583cf7 | 105 | } |
| uehara00 | 0:0be38b583cf7 | 106 | |
| uehara00 | 0:0be38b583cf7 | 107 | // days in a month |
| uehara00 | 0:0be38b583cf7 | 108 | int days_in_month(int year, int month) { |
| uehara00 | 0:0be38b583cf7 | 109 | const int days_leap[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| uehara00 | 0:0be38b583cf7 | 110 | const int days_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| uehara00 | 0:0be38b583cf7 | 111 | |
| uehara00 | 0:0be38b583cf7 | 112 | if (leap_year(year)) { |
| uehara00 | 0:0be38b583cf7 | 113 | return days_leap[month]; |
| uehara00 | 0:0be38b583cf7 | 114 | } |
| uehara00 | 0:0be38b583cf7 | 115 | else { |
| uehara00 | 0:0be38b583cf7 | 116 | return days_common[month]; |
| uehara00 | 0:0be38b583cf7 | 117 | } |
| uehara00 | 0:0be38b583cf7 | 118 | } |
| uehara00 | 0:0be38b583cf7 | 119 | |
| uehara00 | 0:0be38b583cf7 | 120 | // (days in a month) + 1 in BCD |
| uehara00 | 0:0be38b583cf7 | 121 | char bcd_days_in_month(char century, char bcd_year, char bcd_month) { |
| uehara00 | 0:0be38b583cf7 | 122 | return int_to_bcd(days_in_month(bcd_to_year(century, bcd_year), bcd_to_int(bcd_month))); |
| uehara00 | 0:0be38b583cf7 | 123 | } |
| uehara00 | 0:0be38b583cf7 | 124 | |
| uehara00 | 0:0be38b583cf7 | 125 | // Zeller's congruence for Calendario gregoriano |
| uehara00 | 0:0be38b583cf7 | 126 | // 0:SUN .. 6:SAT |
| uehara00 | 0:0be38b583cf7 | 127 | int bcd_date_to_weekday(char bcd_century, char bcd_year, char bcd_month, char bcd_day) { |
| uehara00 | 0:0be38b583cf7 | 128 | int century, year, month, day; |
| uehara00 | 0:0be38b583cf7 | 129 | |
| uehara00 | 0:0be38b583cf7 | 130 | century = 20 + bcd_century; |
| uehara00 | 0:0be38b583cf7 | 131 | year = bcd_to_int(bcd_year); |
| uehara00 | 0:0be38b583cf7 | 132 | month = bcd_to_int(bcd_month); |
| uehara00 | 0:0be38b583cf7 | 133 | day = bcd_to_int(bcd_day); |
| uehara00 | 0:0be38b583cf7 | 134 | return (day + (((month + 1) * 26 ) / 10) + year + year / 4 + century / 4 - century * 2 + 6) % 7; |
| uehara00 | 0:0be38b583cf7 | 135 | } |
| uehara00 | 0:0be38b583cf7 | 136 | |
| uehara00 | 0:0be38b583cf7 | 137 | // test test test test test test test test |
| uehara00 | 0:0be38b583cf7 | 138 | // test: RTC read and raw print to LCD |
| uehara00 | 0:0be38b583cf7 | 139 | void RTC_to_LCD_raw() { |
| uehara00 | 0:0be38b583cf7 | 140 | char d[17]; |
| uehara00 | 0:0be38b583cf7 | 141 | |
| uehara00 | 0:0be38b583cf7 | 142 | i2c_RTC_read(RTC_REG_CONTROL1, d, 16); |
| uehara00 | 0:0be38b583cf7 | 143 | LCD_locate(0, 0); |
| uehara00 | 0:0be38b583cf7 | 144 | for(int i = 0; i < 8; ++i) { |
| uehara00 | 0:0be38b583cf7 | 145 | LCD_print_hex(d[i]); |
| uehara00 | 0:0be38b583cf7 | 146 | } |
| uehara00 | 0:0be38b583cf7 | 147 | LCD_locate(1, 0); |
| uehara00 | 0:0be38b583cf7 | 148 | for(int i = 8; i < 16; ++i) { |
| uehara00 | 0:0be38b583cf7 | 149 | LCD_print_hex(d[i]); |
| uehara00 | 0:0be38b583cf7 | 150 | } |
| uehara00 | 0:0be38b583cf7 | 151 | } |
| uehara00 | 0:0be38b583cf7 | 152 | |
| uehara00 | 0:0be38b583cf7 | 153 | // test 2 of LCD |
| uehara00 | 0:0be38b583cf7 | 154 | // display 64 characters in 1st/2nd row and shift |
| uehara00 | 0:0be38b583cf7 | 155 | // |
| uehara00 | 0:0be38b583cf7 | 156 | void LCD_display_all_char_shift() { |
| uehara00 | 0:0be38b583cf7 | 157 | LCD_display_32char_shift(0x00); |
| uehara00 | 0:0be38b583cf7 | 158 | LCD_display_32char_shift(0x40); |
| uehara00 | 0:0be38b583cf7 | 159 | LCD_display_32char_shift(0x80); |
| uehara00 | 0:0be38b583cf7 | 160 | LCD_display_32char_shift(0xC0); |
| uehara00 | 0:0be38b583cf7 | 161 | } |
| uehara00 | 0:0be38b583cf7 | 162 | |
| uehara00 | 0:0be38b583cf7 | 163 | static void LCD_display_32char_shift(char base) { |
| uehara00 | 0:0be38b583cf7 | 164 | int i; |
| uehara00 | 0:0be38b583cf7 | 165 | |
| uehara00 | 0:0be38b583cf7 | 166 | LCD_clear_display(); // select 1st row and clear display |
| uehara00 | 0:0be38b583cf7 | 167 | for(i = 0; i < 32; ++i) { |
| uehara00 | 0:0be38b583cf7 | 168 | LCD_print_char(base + i); // write 32 characters |
| uehara00 | 0:0be38b583cf7 | 169 | } |
| uehara00 | 0:0be38b583cf7 | 170 | LCD_locate(1, 0); // select 2nd row |
| uehara00 | 0:0be38b583cf7 | 171 | for(i = 32; i < 64; ++i) { |
| uehara00 | 0:0be38b583cf7 | 172 | LCD_print_char(base + i); // write 32 characters |
| uehara00 | 0:0be38b583cf7 | 173 | } |
| uehara00 | 0:0be38b583cf7 | 174 | for(i = 0; i < 24; ++i) { |
| uehara00 | 0:0be38b583cf7 | 175 | LCD_cursor_or_display_shift(1, 0); // shift right 24 times |
| uehara00 | 0:0be38b583cf7 | 176 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 177 | } |
| uehara00 | 0:0be38b583cf7 | 178 | for(i = 0; i < 32; ++i) { |
| uehara00 | 0:0be38b583cf7 | 179 | LCD_cursor_or_display_shift(1, 1); // shift left 32 times |
| uehara00 | 0:0be38b583cf7 | 180 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 181 | } |
| uehara00 | 0:0be38b583cf7 | 182 | for(i = 0; i < 8; ++i) { |
| uehara00 | 0:0be38b583cf7 | 183 | LCD_cursor_or_display_shift(1, 0); // shift right 8 times |
| uehara00 | 0:0be38b583cf7 | 184 | wait_ms(1000); |
| uehara00 | 0:0be38b583cf7 | 185 | } |
| uehara00 | 0:0be38b583cf7 | 186 | } |
| uehara00 | 0:0be38b583cf7 | 187 | |
| uehara00 | 0:0be38b583cf7 | 188 | // test 1 of LCD |
| uehara00 | 0:0be38b583cf7 | 189 | // display 16x2 characters with 1st and 2nd row |
| uehara00 | 0:0be38b583cf7 | 190 | void LCD_display_all_char() { |
| uehara00 | 0:0be38b583cf7 | 191 | LCD_display_32char(0x00); |
| uehara00 | 0:0be38b583cf7 | 192 | LCD_display_32char(0x20); |
| uehara00 | 0:0be38b583cf7 | 193 | LCD_display_32char(0x40); |
| uehara00 | 0:0be38b583cf7 | 194 | LCD_display_32char(0x60); |
| uehara00 | 0:0be38b583cf7 | 195 | LCD_display_32char(0x80); |
| uehara00 | 0:0be38b583cf7 | 196 | LCD_display_32char(0xa0); |
| uehara00 | 0:0be38b583cf7 | 197 | LCD_display_32char(0xc0); |
| uehara00 | 0:0be38b583cf7 | 198 | LCD_display_32char(0xe0); |
| uehara00 | 0:0be38b583cf7 | 199 | } |
| uehara00 | 0:0be38b583cf7 | 200 | |
| uehara00 | 0:0be38b583cf7 | 201 | static void LCD_display_32char(char base) { |
| uehara00 | 0:0be38b583cf7 | 202 | int i; |
| uehara00 | 0:0be38b583cf7 | 203 | |
| uehara00 | 0:0be38b583cf7 | 204 | LCD_clear_display(); // select 1st row and clear display |
| uehara00 | 0:0be38b583cf7 | 205 | for(i = 0; i < 16; ++i) { |
| uehara00 | 0:0be38b583cf7 | 206 | LCD_print_char(base + i); // write 1st-half 16 characters |
| uehara00 | 0:0be38b583cf7 | 207 | } |
| uehara00 | 0:0be38b583cf7 | 208 | LCD_locate(1, 0); // select 2nd row |
| uehara00 | 0:0be38b583cf7 | 209 | for(i = 16; i < 32; ++i) { |
| uehara00 | 0:0be38b583cf7 | 210 | LCD_print_char(base + i); // write 2nd-half 16 characters |
| uehara00 | 0:0be38b583cf7 | 211 | } |
| uehara00 | 0:0be38b583cf7 | 212 | wait_ms(3000); |
| uehara00 | 0:0be38b583cf7 | 213 | } |