This Program is for MAPLE board with OLED and GPS/RTC module. OLED module(OB) : 128 x 128 pixels, 4K color GPS/RTC module(GB) : UP501 These module can buy Marutyu (http://www.marutsu.co.jp)
gpsrtc.cpp@0:58e40a872950, 2011-06-05 (annotated)
- Committer:
- y_notsu
- Date:
- Sun Jun 05 15:10:48 2011 +0000
- Revision:
- 0:58e40a872950
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y_notsu | 0:58e40a872950 | 1 | //========================================================= |
y_notsu | 0:58e40a872950 | 2 | // LPC1114 Project |
y_notsu | 0:58e40a872950 | 3 | //========================================================= |
y_notsu | 0:58e40a872950 | 4 | // File Name : gpsrtc.c |
y_notsu | 0:58e40a872950 | 5 | // Function : GPS&RTC Control |
y_notsu | 0:58e40a872950 | 6 | //--------------------------------------------------------- |
y_notsu | 0:58e40a872950 | 7 | // Rev.01 2010.08.29 Munetomo Maruyama |
y_notsu | 0:58e40a872950 | 8 | //--------------------------------------------------------- |
y_notsu | 0:58e40a872950 | 9 | // Copyright (C) 2010-2011 Munetomo Maruyama |
y_notsu | 0:58e40a872950 | 10 | //========================================================= |
y_notsu | 0:58e40a872950 | 11 | // ---- License Information ------------------------------- |
y_notsu | 0:58e40a872950 | 12 | // Anyone can FREELY use this code fully or partially |
y_notsu | 0:58e40a872950 | 13 | // under conditions shown below. |
y_notsu | 0:58e40a872950 | 14 | // 1. You may use this code only for individual purpose, |
y_notsu | 0:58e40a872950 | 15 | // and educational purpose. |
y_notsu | 0:58e40a872950 | 16 | // Do not use this code for business even if partially. |
y_notsu | 0:58e40a872950 | 17 | // 2. You should use this code under the GNU GPL. |
y_notsu | 0:58e40a872950 | 18 | // 3. You should remain this header text in your codes |
y_notsu | 0:58e40a872950 | 19 | // including Copyright credit and License Information. |
y_notsu | 0:58e40a872950 | 20 | // 4. Your codes should inherit this license information. |
y_notsu | 0:58e40a872950 | 21 | //========================================================= |
y_notsu | 0:58e40a872950 | 22 | // ---- Patent Notice ------------------------------------- |
y_notsu | 0:58e40a872950 | 23 | // I have not cared whether this system (hw + sw) causes |
y_notsu | 0:58e40a872950 | 24 | // infringement on the patent, copyright, trademark, |
y_notsu | 0:58e40a872950 | 25 | // or trade secret rights of others. You have all |
y_notsu | 0:58e40a872950 | 26 | // responsibilities for determining if your designs |
y_notsu | 0:58e40a872950 | 27 | // and products infringe on the intellectual property |
y_notsu | 0:58e40a872950 | 28 | // rights of others, when you use technical information |
y_notsu | 0:58e40a872950 | 29 | // included in this system for your business. |
y_notsu | 0:58e40a872950 | 30 | //========================================================= |
y_notsu | 0:58e40a872950 | 31 | // ---- Disclaimers --------------------------------------- |
y_notsu | 0:58e40a872950 | 32 | // The function and reliability of this system are not |
y_notsu | 0:58e40a872950 | 33 | // guaranteed. They may cause any damages to loss of |
y_notsu | 0:58e40a872950 | 34 | // properties, data, money, profits, life, or business. |
y_notsu | 0:58e40a872950 | 35 | // By adopting this system even partially, you assume |
y_notsu | 0:58e40a872950 | 36 | // all responsibility for its use. |
y_notsu | 0:58e40a872950 | 37 | //========================================================= |
y_notsu | 0:58e40a872950 | 38 | |
y_notsu | 0:58e40a872950 | 39 | //#ifdef __USE_CMSIS |
y_notsu | 0:58e40a872950 | 40 | //#include "LPC11xx.h" |
y_notsu | 0:58e40a872950 | 41 | //#endif |
y_notsu | 0:58e40a872950 | 42 | |
y_notsu | 0:58e40a872950 | 43 | //#include "gpio.h" |
y_notsu | 0:58e40a872950 | 44 | #include "gpsrtc.h" |
y_notsu | 0:58e40a872950 | 45 | //#include "i2c.h" |
y_notsu | 0:58e40a872950 | 46 | //#include "systick.h |
y_notsu | 0:58e40a872950 | 47 | #include "type.h" |
y_notsu | 0:58e40a872950 | 48 | //#include "uart.h" |
y_notsu | 0:58e40a872950 | 49 | //#include "utility.h" |
y_notsu | 0:58e40a872950 | 50 | #include "mbed.h" |
y_notsu | 0:58e40a872950 | 51 | |
y_notsu | 0:58e40a872950 | 52 | Serial device(p9,p10); //tx,rx |
y_notsu | 0:58e40a872950 | 53 | I2C i2c(p28,p27); //sda.scl |
y_notsu | 0:58e40a872950 | 54 | |
y_notsu | 0:58e40a872950 | 55 | //====================== |
y_notsu | 0:58e40a872950 | 56 | // Time Zone Difference |
y_notsu | 0:58e40a872950 | 57 | //====================== |
y_notsu | 0:58e40a872950 | 58 | #define TZD (+9) // Japan |
y_notsu | 0:58e40a872950 | 59 | |
y_notsu | 0:58e40a872950 | 60 | //==================== |
y_notsu | 0:58e40a872950 | 61 | // RTC Device Address |
y_notsu | 0:58e40a872950 | 62 | //==================== |
y_notsu | 0:58e40a872950 | 63 | #define RTC_DEV_ADDR 0xa2 |
y_notsu | 0:58e40a872950 | 64 | #define RTC_WADDR 0xa2 |
y_notsu | 0:58e40a872950 | 65 | #define RTC_RADDR 0xa3 |
y_notsu | 0:58e40a872950 | 66 | |
y_notsu | 0:58e40a872950 | 67 | //======================= |
y_notsu | 0:58e40a872950 | 68 | // RTC Register Address |
y_notsu | 0:58e40a872950 | 69 | //======================= |
y_notsu | 0:58e40a872950 | 70 | #define RTC_CONTROL1 0x00 |
y_notsu | 0:58e40a872950 | 71 | #define RTC_CONTROL2 0x01 |
y_notsu | 0:58e40a872950 | 72 | #define RTC_SECONDS 0x02 |
y_notsu | 0:58e40a872950 | 73 | #define RTC_MINUTES 0x03 |
y_notsu | 0:58e40a872950 | 74 | #define RTC_HOURS 0x04 |
y_notsu | 0:58e40a872950 | 75 | #define RTC_DAYS 0x05 |
y_notsu | 0:58e40a872950 | 76 | #define RTC_WEEKDAYS 0x06 |
y_notsu | 0:58e40a872950 | 77 | #define RTC_C_MONTHS 0x07 |
y_notsu | 0:58e40a872950 | 78 | #define RTC_YEARS 0x08 |
y_notsu | 0:58e40a872950 | 79 | #define RTC_MINUTE_ALARM 0x09 |
y_notsu | 0:58e40a872950 | 80 | #define RTC_HOUR_ALARM 0x0a |
y_notsu | 0:58e40a872950 | 81 | #define RTC_DAY_ALARM 0x0b |
y_notsu | 0:58e40a872950 | 82 | #define RTC_WEEKDAY_ALARM 0x0c |
y_notsu | 0:58e40a872950 | 83 | #define RTC_CLKOUT_FREQ 0x0d |
y_notsu | 0:58e40a872950 | 84 | #define RTC_TIMER_CONTROL 0x0e |
y_notsu | 0:58e40a872950 | 85 | #define RTC_TIMER 0x0f |
y_notsu | 0:58e40a872950 | 86 | |
y_notsu | 0:58e40a872950 | 87 | //============ |
y_notsu | 0:58e40a872950 | 88 | // Globals |
y_notsu | 0:58e40a872950 | 89 | //============ |
y_notsu | 0:58e40a872950 | 90 | extern volatile uint32_t I2CCount; |
y_notsu | 0:58e40a872950 | 91 | //extern volatile uint8_t I2CMasterBuffer[BUFSIZE]; |
y_notsu | 0:58e40a872950 | 92 | //extern volatile uint8_t I2CSlaveBuffer[BUFSIZE]; |
y_notsu | 0:58e40a872950 | 93 | extern volatile uint32_t I2CMasterState; |
y_notsu | 0:58e40a872950 | 94 | extern volatile uint32_t I2CReadLength, I2CWriteLength; |
y_notsu | 0:58e40a872950 | 95 | // |
y_notsu | 0:58e40a872950 | 96 | extern volatile uint32_t UARTCount; |
y_notsu | 0:58e40a872950 | 97 | |
y_notsu | 0:58e40a872950 | 98 | //======================== |
y_notsu | 0:58e40a872950 | 99 | // Get Number from GPS |
y_notsu | 0:58e40a872950 | 100 | //======================== |
y_notsu | 0:58e40a872950 | 101 | uint8_t* Get_Number_from_GPS(uint8_t *pStr, |
y_notsu | 0:58e40a872950 | 102 | int32_t *pInteger, int32_t *pIntrnd, int32_t *pDecimal, uint32_t *pDeclen) |
y_notsu | 0:58e40a872950 | 103 | { |
y_notsu | 0:58e40a872950 | 104 | uint8_t ch; |
y_notsu | 0:58e40a872950 | 105 | int32_t found_decimal; |
y_notsu | 0:58e40a872950 | 106 | int32_t pol; |
y_notsu | 0:58e40a872950 | 107 | int32_t decimal_1st; |
y_notsu | 0:58e40a872950 | 108 | |
y_notsu | 0:58e40a872950 | 109 | found_decimal = 0; |
y_notsu | 0:58e40a872950 | 110 | *pInteger = 0; |
y_notsu | 0:58e40a872950 | 111 | *pDecimal = 0; |
y_notsu | 0:58e40a872950 | 112 | *pDeclen = 0; |
y_notsu | 0:58e40a872950 | 113 | pol = 1; |
y_notsu | 0:58e40a872950 | 114 | while ((ch = *pStr++) != ',') |
y_notsu | 0:58e40a872950 | 115 | { |
y_notsu | 0:58e40a872950 | 116 | if (ch == '.') |
y_notsu | 0:58e40a872950 | 117 | { |
y_notsu | 0:58e40a872950 | 118 | found_decimal = 1; |
y_notsu | 0:58e40a872950 | 119 | } |
y_notsu | 0:58e40a872950 | 120 | else if (ch == '-') |
y_notsu | 0:58e40a872950 | 121 | { |
y_notsu | 0:58e40a872950 | 122 | pol = -1; |
y_notsu | 0:58e40a872950 | 123 | } |
y_notsu | 0:58e40a872950 | 124 | else |
y_notsu | 0:58e40a872950 | 125 | { |
y_notsu | 0:58e40a872950 | 126 | if (found_decimal == 0) |
y_notsu | 0:58e40a872950 | 127 | { |
y_notsu | 0:58e40a872950 | 128 | *pInteger = (*pInteger) * 10 + (ch - '0'); |
y_notsu | 0:58e40a872950 | 129 | } |
y_notsu | 0:58e40a872950 | 130 | else |
y_notsu | 0:58e40a872950 | 131 | { |
y_notsu | 0:58e40a872950 | 132 | *pDecimal = (*pDecimal) * 10 + (ch - '0'); |
y_notsu | 0:58e40a872950 | 133 | *pDeclen = *pDeclen + 1; |
y_notsu | 0:58e40a872950 | 134 | } |
y_notsu | 0:58e40a872950 | 135 | } |
y_notsu | 0:58e40a872950 | 136 | } |
y_notsu | 0:58e40a872950 | 137 | decimal_1st = (*pDeclen > 0)? *pDecimal / power(10, *pDeclen - 1) : 0; |
y_notsu | 0:58e40a872950 | 138 | *pIntrnd = (decimal_1st < 5)? *pInteger : *pInteger + 1; |
y_notsu | 0:58e40a872950 | 139 | // |
y_notsu | 0:58e40a872950 | 140 | *pInteger = *pInteger * pol; |
y_notsu | 0:58e40a872950 | 141 | *pIntrnd = *pIntrnd * pol; |
y_notsu | 0:58e40a872950 | 142 | *pDecimal = *pDecimal * pol; |
y_notsu | 0:58e40a872950 | 143 | return pStr; |
y_notsu | 0:58e40a872950 | 144 | } |
y_notsu | 0:58e40a872950 | 145 | |
y_notsu | 0:58e40a872950 | 146 | //=========================== |
y_notsu | 0:58e40a872950 | 147 | // Get_GPGGA Data from GPS |
y_notsu | 0:58e40a872950 | 148 | //=========================== |
y_notsu | 0:58e40a872950 | 149 | void Get_GPGGA_Data(sGPSRTC *pG) |
y_notsu | 0:58e40a872950 | 150 | { |
y_notsu | 0:58e40a872950 | 151 | uint8_t ch; |
y_notsu | 0:58e40a872950 | 152 | uint8_t str[256]; |
y_notsu | 0:58e40a872950 | 153 | uint8_t *pStr; |
y_notsu | 0:58e40a872950 | 154 | uint32_t quit = 0; |
y_notsu | 0:58e40a872950 | 155 | int32_t integer; |
y_notsu | 0:58e40a872950 | 156 | int32_t intrnd; |
y_notsu | 0:58e40a872950 | 157 | int32_t decimal; |
y_notsu | 0:58e40a872950 | 158 | uint32_t declen; |
y_notsu | 0:58e40a872950 | 159 | device.baud(9600); |
y_notsu | 0:58e40a872950 | 160 | |
y_notsu | 0:58e40a872950 | 161 | //------------------------- |
y_notsu | 0:58e40a872950 | 162 | // Get String after $GPGGA |
y_notsu | 0:58e40a872950 | 163 | //------------------------- |
y_notsu | 0:58e40a872950 | 164 | while(quit == 0) |
y_notsu | 0:58e40a872950 | 165 | { |
y_notsu | 0:58e40a872950 | 166 | //-------------------------- |
y_notsu | 0:58e40a872950 | 167 | // Retry from 1st String |
y_notsu | 0:58e40a872950 | 168 | //-------------------------- |
y_notsu | 0:58e40a872950 | 169 | while(quit == 0) |
y_notsu | 0:58e40a872950 | 170 | { |
y_notsu | 0:58e40a872950 | 171 | //---------------- |
y_notsu | 0:58e40a872950 | 172 | // Check "$GPGGA," |
y_notsu | 0:58e40a872950 | 173 | //---------------- |
y_notsu | 0:58e40a872950 | 174 | if (device.getc() != '$') break; |
y_notsu | 0:58e40a872950 | 175 | if (device.getc() != 'G') break; |
y_notsu | 0:58e40a872950 | 176 | if (device.getc() != 'P') break; |
y_notsu | 0:58e40a872950 | 177 | if (device.getc() != 'G') break; |
y_notsu | 0:58e40a872950 | 178 | if (device.getc() != 'G') break; |
y_notsu | 0:58e40a872950 | 179 | if (device.getc() != 'A') break; |
y_notsu | 0:58e40a872950 | 180 | if (device.getc() != ',') break; |
y_notsu | 0:58e40a872950 | 181 | //----------------- |
y_notsu | 0:58e40a872950 | 182 | // Get String |
y_notsu | 0:58e40a872950 | 183 | //----------------- |
y_notsu | 0:58e40a872950 | 184 | pStr = str; |
y_notsu | 0:58e40a872950 | 185 | while ((ch = device.getc()) != '\r') // LF |
y_notsu | 0:58e40a872950 | 186 | { |
y_notsu | 0:58e40a872950 | 187 | *pStr++ = ch; |
y_notsu | 0:58e40a872950 | 188 | } |
y_notsu | 0:58e40a872950 | 189 | quit = 1; |
y_notsu | 0:58e40a872950 | 190 | } |
y_notsu | 0:58e40a872950 | 191 | } |
y_notsu | 0:58e40a872950 | 192 | pStr = str; |
y_notsu | 0:58e40a872950 | 193 | //------------- |
y_notsu | 0:58e40a872950 | 194 | // UTC |
y_notsu | 0:58e40a872950 | 195 | //------------- |
y_notsu | 0:58e40a872950 | 196 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 197 | pG->bGPS_UTC_hour = (uint8_t) (integer / 10000); |
y_notsu | 0:58e40a872950 | 198 | pG->bGPS_UTC_min = (uint8_t) ((integer % 10000) / 100); |
y_notsu | 0:58e40a872950 | 199 | pG->bGPS_UTC_sec = (uint8_t) (integer % 100); |
y_notsu | 0:58e40a872950 | 200 | //--------------- |
y_notsu | 0:58e40a872950 | 201 | // Latitude |
y_notsu | 0:58e40a872950 | 202 | //--------------- |
y_notsu | 0:58e40a872950 | 203 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 204 | pG->bGPS_LAT_deg = (uint8_t) (integer / 100); |
y_notsu | 0:58e40a872950 | 205 | pG->bGPS_LAT_min = (uint8_t) (integer % 100); |
y_notsu | 0:58e40a872950 | 206 | pG->bGPS_LAT_sec = (uint8_t) ((60 * decimal) / power(10, declen)); |
y_notsu | 0:58e40a872950 | 207 | pG->cGPS_LAT = (*pStr != ',')? *pStr++ : ' '; |
y_notsu | 0:58e40a872950 | 208 | ch = *pStr++; // ',' |
y_notsu | 0:58e40a872950 | 209 | //--------------- |
y_notsu | 0:58e40a872950 | 210 | // Longitude |
y_notsu | 0:58e40a872950 | 211 | //--------------- |
y_notsu | 0:58e40a872950 | 212 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 213 | pG->bGPS_LNG_deg = (uint8_t) (integer / 100); |
y_notsu | 0:58e40a872950 | 214 | pG->bGPS_LNG_min = (uint8_t) (integer % 100); |
y_notsu | 0:58e40a872950 | 215 | pG->bGPS_LNG_sec = (uint8_t) ((60 * decimal) / power(10, declen)); |
y_notsu | 0:58e40a872950 | 216 | pG->cGPS_LNG = (*pStr != ',')? *pStr++ : ' '; |
y_notsu | 0:58e40a872950 | 217 | ch = *pStr++; // ',' |
y_notsu | 0:58e40a872950 | 218 | //-------------- |
y_notsu | 0:58e40a872950 | 219 | // GPS Quality |
y_notsu | 0:58e40a872950 | 220 | //-------------- |
y_notsu | 0:58e40a872950 | 221 | pG->cGPS_Quality = *pStr++; |
y_notsu | 0:58e40a872950 | 222 | ch = *pStr++; // ',' |
y_notsu | 0:58e40a872950 | 223 | //----------------- |
y_notsu | 0:58e40a872950 | 224 | // Satellite Count |
y_notsu | 0:58e40a872950 | 225 | //----------------- |
y_notsu | 0:58e40a872950 | 226 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 227 | pG->bGPS_Sat = (uint8_t) integer; |
y_notsu | 0:58e40a872950 | 228 | //----------------- |
y_notsu | 0:58e40a872950 | 229 | // HDOP |
y_notsu | 0:58e40a872950 | 230 | //----------------- |
y_notsu | 0:58e40a872950 | 231 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 232 | pG->bGPS_HDOP_I = (uint8_t) integer; |
y_notsu | 0:58e40a872950 | 233 | pG->bGPS_HDOP_D = (uint8_t) ((decimal + power(10, declen) / 2) / power(10, declen)); |
y_notsu | 0:58e40a872950 | 234 | //------------------------- |
y_notsu | 0:58e40a872950 | 235 | // Altitude above Sea Level |
y_notsu | 0:58e40a872950 | 236 | //------------------------- |
y_notsu | 0:58e40a872950 | 237 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 238 | pG->wGPS_ASL_m = intrnd; |
y_notsu | 0:58e40a872950 | 239 | ch = *pStr++; // 'M' |
y_notsu | 0:58e40a872950 | 240 | ch = *pStr++; // ',' |
y_notsu | 0:58e40a872950 | 241 | //------------------------- |
y_notsu | 0:58e40a872950 | 242 | // Geoid Separation |
y_notsu | 0:58e40a872950 | 243 | //------------------------- |
y_notsu | 0:58e40a872950 | 244 | pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); |
y_notsu | 0:58e40a872950 | 245 | pG->wGPS_GEO_m = intrnd; |
y_notsu | 0:58e40a872950 | 246 | ch = *pStr++; // 'M' |
y_notsu | 0:58e40a872950 | 247 | ch = *pStr++; // ',' |
y_notsu | 0:58e40a872950 | 248 | } |
y_notsu | 0:58e40a872950 | 249 | |
y_notsu | 0:58e40a872950 | 250 | //================== |
y_notsu | 0:58e40a872950 | 251 | // Initialize RTC |
y_notsu | 0:58e40a872950 | 252 | //================== |
y_notsu | 0:58e40a872950 | 253 | void Init_RTC(uint32_t do_adj, uint8_t year, uint8_t month, uint8_t day, |
y_notsu | 0:58e40a872950 | 254 | uint8_t week, uint8_t hour, uint8_t minute, uint8_t second) |
y_notsu | 0:58e40a872950 | 255 | { |
y_notsu | 0:58e40a872950 | 256 | |
y_notsu | 0:58e40a872950 | 257 | wait(1); // wait 1000ms |
y_notsu | 0:58e40a872950 | 258 | // |
y_notsu | 0:58e40a872950 | 259 | RTC_Write_Reg(RTC_CONTROL1, 0x20); // STOP |
y_notsu | 0:58e40a872950 | 260 | RTC_Write_Reg(RTC_CONTROL2, 0x00); |
y_notsu | 0:58e40a872950 | 261 | // |
y_notsu | 0:58e40a872950 | 262 | RTC_Write_Reg(RTC_HOURS, BCD_INT(hour)); |
y_notsu | 0:58e40a872950 | 263 | RTC_Write_Reg(RTC_MINUTES, BCD_INT(minute)); |
y_notsu | 0:58e40a872950 | 264 | RTC_Write_Reg(RTC_SECONDS, BCD_INT(second)); |
y_notsu | 0:58e40a872950 | 265 | // |
y_notsu | 0:58e40a872950 | 266 | RTC_Write_Reg(RTC_YEARS, BCD_INT(year)); |
y_notsu | 0:58e40a872950 | 267 | RTC_Write_Reg(RTC_C_MONTHS, BCD_INT(month)); |
y_notsu | 0:58e40a872950 | 268 | RTC_Write_Reg(RTC_DAYS, BCD_INT(day)); |
y_notsu | 0:58e40a872950 | 269 | RTC_Write_Reg(RTC_WEEKDAYS, BCD_INT(week)); |
y_notsu | 0:58e40a872950 | 270 | // |
y_notsu | 0:58e40a872950 | 271 | RTC_Write_Reg(RTC_MINUTE_ALARM, 0x00); |
y_notsu | 0:58e40a872950 | 272 | RTC_Write_Reg(RTC_HOUR_ALARM, 0x00); |
y_notsu | 0:58e40a872950 | 273 | RTC_Write_Reg(RTC_DAY_ALARM, 0x00); |
y_notsu | 0:58e40a872950 | 274 | RTC_Write_Reg(RTC_WEEKDAY_ALARM, 0x00); |
y_notsu | 0:58e40a872950 | 275 | // |
y_notsu | 0:58e40a872950 | 276 | RTC_Write_Reg(RTC_CLKOUT_FREQ, 0x00); |
y_notsu | 0:58e40a872950 | 277 | RTC_Write_Reg(RTC_TIMER_CONTROL,0x00); |
y_notsu | 0:58e40a872950 | 278 | RTC_Write_Reg(RTC_TIMER, 0x00); |
y_notsu | 0:58e40a872950 | 279 | // |
y_notsu | 0:58e40a872950 | 280 | RTC_Write_Reg(RTC_CONTROL1, 0x00); // START |
y_notsu | 0:58e40a872950 | 281 | } |
y_notsu | 0:58e40a872950 | 282 | |
y_notsu | 0:58e40a872950 | 283 | //==================== |
y_notsu | 0:58e40a872950 | 284 | // Get RTC Data |
y_notsu | 0:58e40a872950 | 285 | //==================== |
y_notsu | 0:58e40a872950 | 286 | void Get_RTC_Data(sGPSRTC *psGPSRTC) |
y_notsu | 0:58e40a872950 | 287 | { |
y_notsu | 0:58e40a872950 | 288 | psGPSRTC->bRTC_year = INT_BCD(RTC_Read_Reg(RTC_YEARS)); |
y_notsu | 0:58e40a872950 | 289 | psGPSRTC->bRTC_mon = INT_BCD(RTC_Read_Reg(RTC_C_MONTHS) & 0x1f); |
y_notsu | 0:58e40a872950 | 290 | psGPSRTC->bRTC_day = INT_BCD(RTC_Read_Reg(RTC_DAYS) & 0x3f); |
y_notsu | 0:58e40a872950 | 291 | psGPSRTC->bRTC_week = RTC_Read_Reg(RTC_WEEKDAYS) & 0x07; |
y_notsu | 0:58e40a872950 | 292 | psGPSRTC->bRTC_hour = INT_BCD(RTC_Read_Reg(RTC_HOURS) & 0x3f); |
y_notsu | 0:58e40a872950 | 293 | psGPSRTC->bRTC_min = INT_BCD(RTC_Read_Reg(RTC_MINUTES) & 0x7f); |
y_notsu | 0:58e40a872950 | 294 | psGPSRTC->bRTC_sec = INT_BCD(RTC_Read_Reg(RTC_SECONDS) & 0x7f); |
y_notsu | 0:58e40a872950 | 295 | } |
y_notsu | 0:58e40a872950 | 296 | |
y_notsu | 0:58e40a872950 | 297 | //=================== |
y_notsu | 0:58e40a872950 | 298 | // Get Week String |
y_notsu | 0:58e40a872950 | 299 | //=================== |
y_notsu | 0:58e40a872950 | 300 | uint8_t *Get_Week_String(uint32_t week) |
y_notsu | 0:58e40a872950 | 301 | { |
y_notsu | 0:58e40a872950 | 302 | static const char *WEEK[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; |
y_notsu | 0:58e40a872950 | 303 | |
y_notsu | 0:58e40a872950 | 304 | return (uint8_t*) WEEK[week]; |
y_notsu | 0:58e40a872950 | 305 | } |
y_notsu | 0:58e40a872950 | 306 | |
y_notsu | 0:58e40a872950 | 307 | //===================== |
y_notsu | 0:58e40a872950 | 308 | // RTC Write Register |
y_notsu | 0:58e40a872950 | 309 | //===================== |
y_notsu | 0:58e40a872950 | 310 | void RTC_Write_Reg(uint32_t addr, uint32_t data) |
y_notsu | 0:58e40a872950 | 311 | { |
y_notsu | 0:58e40a872950 | 312 | char cmd[2]; |
y_notsu | 0:58e40a872950 | 313 | cmd[0]=addr; |
y_notsu | 0:58e40a872950 | 314 | cmd[1]=data; |
y_notsu | 0:58e40a872950 | 315 | const int addrw=0xA2; |
y_notsu | 0:58e40a872950 | 316 | i2c.write(addrw,cmd,2); |
y_notsu | 0:58e40a872950 | 317 | } |
y_notsu | 0:58e40a872950 | 318 | |
y_notsu | 0:58e40a872950 | 319 | //==================== |
y_notsu | 0:58e40a872950 | 320 | // RTC Read Register |
y_notsu | 0:58e40a872950 | 321 | //==================== |
y_notsu | 0:58e40a872950 | 322 | uint32_t RTC_Read_Reg(uint32_t addr) |
y_notsu | 0:58e40a872950 | 323 | { |
y_notsu | 0:58e40a872950 | 324 | char read_buf[2]; |
y_notsu | 0:58e40a872950 | 325 | const int addrw=0xa2; |
y_notsu | 0:58e40a872950 | 326 | const int addrr=0xa3; |
y_notsu | 0:58e40a872950 | 327 | char write_cmd[1]; |
y_notsu | 0:58e40a872950 | 328 | write_cmd[0]=char(addr); |
y_notsu | 0:58e40a872950 | 329 | i2c.write(addrw,write_cmd,1); |
y_notsu | 0:58e40a872950 | 330 | wait(0.01); |
y_notsu | 0:58e40a872950 | 331 | i2c.read(addrr,read_buf,1); |
y_notsu | 0:58e40a872950 | 332 | // |
y_notsu | 0:58e40a872950 | 333 | return read_buf[0]; |
y_notsu | 0:58e40a872950 | 334 | } |
y_notsu | 0:58e40a872950 | 335 | |
y_notsu | 0:58e40a872950 | 336 | //===================== |
y_notsu | 0:58e40a872950 | 337 | // BCD from Integer |
y_notsu | 0:58e40a872950 | 338 | //===================== |
y_notsu | 0:58e40a872950 | 339 | uint8_t BCD_INT(uint8_t num) |
y_notsu | 0:58e40a872950 | 340 | { |
y_notsu | 0:58e40a872950 | 341 | return ((num / 10) << 4) + (num % 10); |
y_notsu | 0:58e40a872950 | 342 | } |
y_notsu | 0:58e40a872950 | 343 | |
y_notsu | 0:58e40a872950 | 344 | //======================== |
y_notsu | 0:58e40a872950 | 345 | // Calculate x^n |
y_notsu | 0:58e40a872950 | 346 | //======================== |
y_notsu | 0:58e40a872950 | 347 | int32_t power(int32_t x, int32_t n) |
y_notsu | 0:58e40a872950 | 348 | { |
y_notsu | 0:58e40a872950 | 349 | uint32_t i; |
y_notsu | 0:58e40a872950 | 350 | int32_t y; |
y_notsu | 0:58e40a872950 | 351 | |
y_notsu | 0:58e40a872950 | 352 | y = 1; |
y_notsu | 0:58e40a872950 | 353 | for (i = 0; i < n; i++) |
y_notsu | 0:58e40a872950 | 354 | { |
y_notsu | 0:58e40a872950 | 355 | y = y * x; |
y_notsu | 0:58e40a872950 | 356 | } |
y_notsu | 0:58e40a872950 | 357 | return y; |
y_notsu | 0:58e40a872950 | 358 | } |
y_notsu | 0:58e40a872950 | 359 | |
y_notsu | 0:58e40a872950 | 360 | //===================== |
y_notsu | 0:58e40a872950 | 361 | // Integer from BCD |
y_notsu | 0:58e40a872950 | 362 | //===================== |
y_notsu | 0:58e40a872950 | 363 | uint8_t INT_BCD(uint8_t bcd) |
y_notsu | 0:58e40a872950 | 364 | { |
y_notsu | 0:58e40a872950 | 365 | return (((bcd >> 4) * 10) + (bcd & 0x0f)); |
y_notsu | 0:58e40a872950 | 366 | } |
y_notsu | 0:58e40a872950 | 367 | |
y_notsu | 0:58e40a872950 | 368 | //========================================================= |
y_notsu | 0:58e40a872950 | 369 | // End of Program |
y_notsu | 0:58e40a872950 | 370 | //========================================================= |