test
Dependencies: FatFileSystem TextLCD mbed
gpsrtc.cpp
00001 //========================================================= 00002 // LPC1114 Project 00003 //========================================================= 00004 // File Name : gpsrtc.c 00005 // Function : GPS&RTC Control 00006 //--------------------------------------------------------- 00007 // Rev.01 2010.08.29 Munetomo Maruyama 00008 //--------------------------------------------------------- 00009 // Copyright (C) 2010-2011 Munetomo Maruyama 00010 //========================================================= 00011 // ---- License Information ------------------------------- 00012 // Anyone can FREELY use this code fully or partially 00013 // under conditions shown below. 00014 // 1. You may use this code only for individual purpose, 00015 // and educational purpose. 00016 // Do not use this code for business even if partially. 00017 // 2. You should use this code under the GNU GPL. 00018 // 3. You should remain this header text in your codes 00019 // including Copyright credit and License Information. 00020 // 4. Your codes should inherit this license information. 00021 //========================================================= 00022 // ---- Patent Notice ------------------------------------- 00023 // I have not cared whether this system (hw + sw) causes 00024 // infringement on the patent, copyright, trademark, 00025 // or trade secret rights of others. You have all 00026 // responsibilities for determining if your designs 00027 // and products infringe on the intellectual property 00028 // rights of others, when you use technical information 00029 // included in this system for your business. 00030 //========================================================= 00031 // ---- Disclaimers --------------------------------------- 00032 // The function and reliability of this system are not 00033 // guaranteed. They may cause any damages to loss of 00034 // properties, data, money, profits, life, or business. 00035 // By adopting this system even partially, you assume 00036 // all responsibility for its use. 00037 //========================================================= 00038 00039 //#ifdef __USE_CMSIS 00040 //#include "LPC11xx.h" 00041 //#endif 00042 00043 //#include "gpio.h" 00044 #include "gpsrtc.h" 00045 //#include "i2c.h" 00046 //#include "systick.h 00047 #include "type.h" 00048 //#include "uart.h" 00049 //#include "utility.h" 00050 #include "mbed.h" 00051 00052 Serial device(p9,p10); //tx,rx 00053 I2C i2c(p28,p27); //sda.scl 00054 00055 //====================== 00056 // Time Zone Difference 00057 //====================== 00058 #define TZD (+9) // Japan 00059 00060 //==================== 00061 // RTC Device Address 00062 //==================== 00063 #define RTC_DEV_ADDR 0xa2 00064 #define RTC_WADDR 0xa2 00065 #define RTC_RADDR 0xa3 00066 00067 //======================= 00068 // RTC Register Address 00069 //======================= 00070 #define RTC_CONTROL1 0x00 00071 #define RTC_CONTROL2 0x01 00072 #define RTC_SECONDS 0x02 00073 #define RTC_MINUTES 0x03 00074 #define RTC_HOURS 0x04 00075 #define RTC_DAYS 0x05 00076 #define RTC_WEEKDAYS 0x06 00077 #define RTC_C_MONTHS 0x07 00078 #define RTC_YEARS 0x08 00079 #define RTC_MINUTE_ALARM 0x09 00080 #define RTC_HOUR_ALARM 0x0a 00081 #define RTC_DAY_ALARM 0x0b 00082 #define RTC_WEEKDAY_ALARM 0x0c 00083 #define RTC_CLKOUT_FREQ 0x0d 00084 #define RTC_TIMER_CONTROL 0x0e 00085 #define RTC_TIMER 0x0f 00086 00087 //============ 00088 // Globals 00089 //============ 00090 extern volatile unsigned int I2CCount; 00091 //extern volatile uint8_t I2CMasterBuffer[BUFSIZE]; 00092 //extern volatile uint8_t I2CSlaveBuffer[BUFSIZE]; 00093 extern volatile unsigned int I2CMasterState; 00094 extern volatile unsigned int I2CReadLength, I2CWriteLength; 00095 // 00096 extern volatile unsigned int UARTCount; 00097 00098 //======================== 00099 // Get Number from GPS 00100 //======================== 00101 unsigned char* Get_Number_from_GPS(unsigned char *pStr, 00102 signed int *pInteger, signed int *pIntrnd, signed int *pDecimal, unsigned int *pDeclen) 00103 { 00104 unsigned char ch; 00105 signed int found_decimal; 00106 signed int pol; 00107 signed int decimal_1st; 00108 00109 found_decimal = 0; 00110 *pInteger = 0; 00111 *pDecimal = 0; 00112 *pDeclen = 0; 00113 pol = 1; 00114 while ((ch = *pStr++) != ',') 00115 { 00116 if (ch == '.') 00117 { 00118 found_decimal = 1; 00119 } 00120 else if (ch == '-') 00121 { 00122 pol = -1; 00123 } 00124 else 00125 { 00126 if (found_decimal == 0) 00127 { 00128 *pInteger = (*pInteger) * 10 + (ch - '0'); 00129 } 00130 else 00131 { 00132 *pDecimal = (*pDecimal) * 10 + (ch - '0'); 00133 *pDeclen = *pDeclen + 1; 00134 } 00135 } 00136 } 00137 decimal_1st = (*pDeclen > 0)? *pDecimal / power(10, *pDeclen - 1) : 0; 00138 *pIntrnd = (decimal_1st < 5)? *pInteger : *pInteger + 1; 00139 // 00140 *pInteger = *pInteger * pol; 00141 *pIntrnd = *pIntrnd * pol; 00142 *pDecimal = *pDecimal * pol; 00143 return pStr; 00144 } 00145 00146 //=========================== 00147 // Get_GPGGA Data from GPS 00148 //=========================== 00149 void Get_GPGGA_Data(sGPSRTC *pG) 00150 { 00151 unsigned char ch; 00152 unsigned char str[256]; 00153 unsigned char *pStr; 00154 unsigned int quit = 0; 00155 signed int integer; 00156 signed int intrnd; 00157 signed int decimal; 00158 unsigned int declen; 00159 //device.baud(4800); //for SparkFun GPS-08936 00160 device.baud(9600); //for MARY-GB 00161 //unsigned int ti=0; 00162 00163 //------------------------- 00164 // Get String after $GPGGA 00165 //------------------------- 00166 while(quit == 0) 00167 { 00168 //-------------------------- 00169 // Retry from 1st String 00170 //-------------------------- 00171 while(quit == 0) 00172 { 00173 //---------------- 00174 // Check "$GPGGA," 00175 //---------------- 00176 if (device.getc() != '$') break; 00177 if (device.getc() != 'G') break; 00178 if (device.getc() != 'P') break; 00179 if (device.getc() != 'G') break; 00180 if (device.getc() != 'G') break; 00181 if (device.getc() != 'A') break; 00182 if (device.getc() != ',') break; 00183 //----------------- 00184 // Get String 00185 //----------------- 00186 pStr = str; 00187 while ((ch = device.getc()) != '\r') // LF 00188 { 00189 *pStr++ = ch; 00190 //ti++; 00191 //if(ti==10000) goto L1; 00192 } 00193 quit = 1; 00194 } 00195 } 00196 L1: 00197 pStr = str; 00198 //------------- 00199 // UTC 00200 //------------- 00201 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00202 pG->bGPS_UTC_hour = (unsigned char) (integer / 10000); 00203 pG->bGPS_UTC_min = (unsigned char) ((integer % 10000) / 100); 00204 pG->bGPS_UTC_sec = (unsigned char) (integer % 100); 00205 //--------------- 00206 // Latitude 00207 //--------------- 00208 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00209 pG->bGPS_LAT_deg = (unsigned char) (integer / 100); 00210 pG->bGPS_LAT_min = (unsigned char) (integer % 100); 00211 pG->bGPS_LAT_sec = (unsigned char) ((60 * decimal) / power(10, declen)); 00212 pG->cGPS_LAT = (*pStr != ',')? *pStr++ : ' '; 00213 ch = *pStr++; // ',' 00214 //--------------- 00215 // Longitude 00216 //--------------- 00217 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00218 pG->bGPS_LNG_deg = (unsigned char) (integer / 100); 00219 pG->bGPS_LNG_min = (unsigned char) (integer % 100); 00220 pG->bGPS_LNG_sec = (unsigned char) ((60 * decimal) / power(10, declen)); 00221 pG->cGPS_LNG = (*pStr != ',')? *pStr++ : ' '; 00222 ch = *pStr++; // ',' 00223 //-------------- 00224 // GPS Quality 00225 //-------------- 00226 pG->cGPS_Quality = *pStr++; 00227 ch = *pStr++; // ',' 00228 //----------------- 00229 // Satellite Count 00230 //----------------- 00231 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00232 pG->bGPS_Sat = (unsigned char) integer; 00233 //----------------- 00234 // HDOP 00235 //----------------- 00236 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00237 pG->bGPS_HDOP_I = (unsigned char) integer; 00238 pG->bGPS_HDOP_D = (unsigned char) ((decimal + power(10, declen) / 2) / power(10, declen)); 00239 //------------------------- 00240 // Altitude above Sea Level 00241 //------------------------- 00242 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00243 pG->wGPS_ASL_m = intrnd; 00244 ch = *pStr++; // 'M' 00245 ch = *pStr++; // ',' 00246 //------------------------- 00247 // Geoid Separation 00248 //------------------------- 00249 pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen); 00250 pG->wGPS_GEO_m = intrnd; 00251 ch = *pStr++; // 'M' 00252 ch = *pStr++; // ',' 00253 } 00254 00255 //================== 00256 // Initialize RTC 00257 //================== 00258 void Init_RTC(unsigned int do_adj, unsigned char year, unsigned char month, unsigned char day, 00259 unsigned char week, unsigned char hour, unsigned char minute, unsigned char second) 00260 { 00261 00262 wait(1); // wait 1000ms 00263 // 00264 RTC_Write_Reg(RTC_CONTROL1, 0x20); // STOP 00265 RTC_Write_Reg(RTC_CONTROL2, 0x00); 00266 // 00267 RTC_Write_Reg(RTC_HOURS, BCD_INT(hour)); 00268 RTC_Write_Reg(RTC_MINUTES, BCD_INT(minute)); 00269 RTC_Write_Reg(RTC_SECONDS, BCD_INT(second)); 00270 // 00271 RTC_Write_Reg(RTC_YEARS, BCD_INT(year)); 00272 RTC_Write_Reg(RTC_C_MONTHS, BCD_INT(month)); 00273 RTC_Write_Reg(RTC_DAYS, BCD_INT(day)); 00274 RTC_Write_Reg(RTC_WEEKDAYS, BCD_INT(week)); 00275 // 00276 RTC_Write_Reg(RTC_MINUTE_ALARM, 0x00); 00277 RTC_Write_Reg(RTC_HOUR_ALARM, 0x00); 00278 RTC_Write_Reg(RTC_DAY_ALARM, 0x00); 00279 RTC_Write_Reg(RTC_WEEKDAY_ALARM, 0x00); 00280 // 00281 RTC_Write_Reg(RTC_CLKOUT_FREQ, 0x00); 00282 RTC_Write_Reg(RTC_TIMER_CONTROL,0x00); 00283 RTC_Write_Reg(RTC_TIMER, 0x00); 00284 // 00285 RTC_Write_Reg(RTC_CONTROL1, 0x00); // START 00286 } 00287 00288 //==================== 00289 // Get RTC Data 00290 //==================== 00291 void Get_RTC_Data(sGPSRTC *psGPSRTC) 00292 { 00293 psGPSRTC->bRTC_year = INT_BCD(RTC_Read_Reg(RTC_YEARS)); 00294 psGPSRTC->bRTC_mon = INT_BCD(RTC_Read_Reg(RTC_C_MONTHS) & 0x1f); 00295 psGPSRTC->bRTC_day = INT_BCD(RTC_Read_Reg(RTC_DAYS) & 0x3f); 00296 psGPSRTC->bRTC_week = RTC_Read_Reg(RTC_WEEKDAYS) & 0x07; 00297 psGPSRTC->bRTC_hour = INT_BCD(RTC_Read_Reg(RTC_HOURS) & 0x3f); 00298 psGPSRTC->bRTC_min = INT_BCD(RTC_Read_Reg(RTC_MINUTES) & 0x7f); 00299 psGPSRTC->bRTC_sec = INT_BCD(RTC_Read_Reg(RTC_SECONDS) & 0x7f); 00300 } 00301 00302 //=================== 00303 // Get Week String 00304 //=================== 00305 unsigned char *Get_Week_String(unsigned int week) 00306 { 00307 static const char *WEEK[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}; 00308 00309 return (unsigned char*) WEEK[week]; 00310 } 00311 00312 //===================== 00313 // RTC Write Register 00314 //===================== 00315 void RTC_Write_Reg(unsigned int addr, unsigned int data) 00316 { 00317 char cmd[2]; 00318 cmd[0]=addr; 00319 cmd[1]=data; 00320 const int addrw=0xA2; 00321 i2c.write(addrw,cmd,2); 00322 } 00323 00324 //==================== 00325 // RTC Read Register 00326 //==================== 00327 unsigned int RTC_Read_Reg(unsigned int addr) 00328 { 00329 char read_buf[2]; 00330 const int addrw=0xa2; 00331 const int addrr=0xa3; 00332 char write_cmd[1]; 00333 write_cmd[0]=char(addr); 00334 i2c.write(addrw,write_cmd,1); 00335 wait(0.01); 00336 i2c.read(addrr,read_buf,1); 00337 // 00338 return read_buf[0]; 00339 } 00340 00341 //===================== 00342 // BCD from Integer 00343 //===================== 00344 unsigned char BCD_INT(unsigned char num) 00345 { 00346 return ((num / 10) << 4) + (num % 10); 00347 } 00348 00349 //======================== 00350 // Calculate x^n 00351 //======================== 00352 signed int power(signed int x, signed int n) 00353 { 00354 unsigned int i; 00355 signed int y; 00356 00357 y = 1; 00358 for (i = 0; i < n; i++) 00359 { 00360 y = y * x; 00361 } 00362 return y; 00363 } 00364 00365 //===================== 00366 // Integer from BCD 00367 //===================== 00368 unsigned char INT_BCD(unsigned char bcd) 00369 { 00370 return (((bcd >> 4) * 10) + (bcd & 0x0f)); 00371 } 00372 00373 //========================================================= 00374 // End of Program 00375 //=========================================================
Generated on Wed Jul 13 2022 12:25:58 by 1.7.2