This program is for mbed-Geiger counter system. It use OLED module, GPS module (with RTC), TextLCD, SD FileSystem and some Interrupt pin.

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpsrtc.cpp Source File

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(9600);
00160 
00161     //-------------------------
00162     // Get String after $GPGGA
00163     //-------------------------
00164     while(quit == 0)
00165     {
00166         //--------------------------
00167         // Retry from 1st String
00168         //--------------------------
00169         while(quit == 0)
00170         {
00171             //----------------
00172             // Check "$GPGGA,"
00173             //----------------
00174             if (device.getc() != '$') break;
00175             if (device.getc() != 'G') break;
00176             if (device.getc() != 'P') break;
00177             if (device.getc() != 'G') break;
00178             if (device.getc() != 'G') break;
00179             if (device.getc() != 'A') break;
00180             if (device.getc() != ',') break;
00181             //-----------------
00182             // Get String
00183             //-----------------
00184             pStr = str;
00185             while ((ch = device.getc()) != '\r') // LF
00186             {
00187                 *pStr++ = ch;
00188             }
00189             quit = 1;
00190         }
00191     }
00192     pStr = str;
00193     //-------------
00194     // UTC
00195     //-------------
00196     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00197     pG->bGPS_UTC_hour = (unsigned char) (integer / 10000);
00198     pG->bGPS_UTC_min  = (unsigned char) ((integer % 10000) / 100);
00199     pG->bGPS_UTC_sec  = (unsigned char) (integer % 100);
00200     //---------------
00201     // Latitude
00202     //---------------
00203     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00204     pG->bGPS_LAT_deg = (unsigned char) (integer / 100);
00205     pG->bGPS_LAT_min = (unsigned char) (integer % 100);
00206     pG->bGPS_LAT_sec = (unsigned char) ((60 * decimal) / power(10, declen));
00207     pG->cGPS_LAT = (*pStr != ',')?  *pStr++ : ' ';
00208     ch = *pStr++; // ','
00209     //---------------
00210     // Longitude
00211     //---------------
00212     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00213     pG->bGPS_LNG_deg = (unsigned char) (integer / 100);
00214     pG->bGPS_LNG_min = (unsigned char) (integer % 100);
00215     pG->bGPS_LNG_sec = (unsigned char) ((60 * decimal) / power(10, declen));
00216     pG->cGPS_LNG = (*pStr != ',')?  *pStr++ : ' ';
00217     ch = *pStr++; // ','
00218     //--------------
00219     // GPS Quality
00220     //--------------
00221     pG->cGPS_Quality = *pStr++;
00222     ch = *pStr++; // ','
00223     //-----------------
00224     // Satellite Count
00225     //-----------------
00226     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00227     pG->bGPS_Sat = (unsigned char) integer;
00228     //-----------------
00229     // HDOP
00230     //-----------------
00231     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00232     pG->bGPS_HDOP_I = (unsigned char) integer;
00233     pG->bGPS_HDOP_D = (unsigned char) ((decimal + power(10, declen) / 2) / power(10, declen));
00234     //-------------------------
00235     // Altitude above Sea Level
00236     //-------------------------
00237     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00238     pG->wGPS_ASL_m = intrnd;
00239     ch = *pStr++; // 'M'
00240     ch = *pStr++; // ','
00241     //-------------------------
00242     // Geoid Separation
00243     //-------------------------
00244     pStr = Get_Number_from_GPS(pStr, &integer, &intrnd, &decimal, &declen);
00245     pG->wGPS_GEO_m = intrnd;
00246     ch = *pStr++; // 'M'
00247     ch = *pStr++; // ','
00248 }
00249 
00250 //==================
00251 // Initialize RTC
00252 //==================
00253 void Init_RTC(unsigned int do_adj, unsigned char year, unsigned char month,  unsigned char day,
00254               unsigned char week,    unsigned char hour, unsigned char minute, unsigned char second)
00255 {
00256 
00257         wait(1); // wait 1000ms
00258         //
00259         RTC_Write_Reg(RTC_CONTROL1, 0x20); // STOP
00260         RTC_Write_Reg(RTC_CONTROL2, 0x00);
00261         //
00262         RTC_Write_Reg(RTC_HOURS,   BCD_INT(hour));
00263         RTC_Write_Reg(RTC_MINUTES, BCD_INT(minute));
00264         RTC_Write_Reg(RTC_SECONDS, BCD_INT(second));
00265         //
00266         RTC_Write_Reg(RTC_YEARS,    BCD_INT(year));
00267         RTC_Write_Reg(RTC_C_MONTHS, BCD_INT(month));
00268         RTC_Write_Reg(RTC_DAYS,     BCD_INT(day));
00269         RTC_Write_Reg(RTC_WEEKDAYS, BCD_INT(week));
00270         //
00271         RTC_Write_Reg(RTC_MINUTE_ALARM,  0x00);
00272         RTC_Write_Reg(RTC_HOUR_ALARM,    0x00);
00273         RTC_Write_Reg(RTC_DAY_ALARM,     0x00);
00274         RTC_Write_Reg(RTC_WEEKDAY_ALARM, 0x00);
00275         //
00276         RTC_Write_Reg(RTC_CLKOUT_FREQ,  0x00);
00277         RTC_Write_Reg(RTC_TIMER_CONTROL,0x00);
00278         RTC_Write_Reg(RTC_TIMER,        0x00);
00279         //
00280         RTC_Write_Reg(RTC_CONTROL1, 0x00); // START
00281 }
00282 
00283 //====================
00284 // Get RTC Data
00285 //====================
00286 void Get_RTC_Data(sGPSRTC *psGPSRTC)
00287 {
00288     psGPSRTC->bRTC_year = INT_BCD(RTC_Read_Reg(RTC_YEARS));
00289     psGPSRTC->bRTC_mon  = INT_BCD(RTC_Read_Reg(RTC_C_MONTHS) & 0x1f);
00290     psGPSRTC->bRTC_day  = INT_BCD(RTC_Read_Reg(RTC_DAYS) & 0x3f);
00291     psGPSRTC->bRTC_week = RTC_Read_Reg(RTC_WEEKDAYS) & 0x07;
00292     psGPSRTC->bRTC_hour = INT_BCD(RTC_Read_Reg(RTC_HOURS) & 0x3f);
00293     psGPSRTC->bRTC_min  = INT_BCD(RTC_Read_Reg(RTC_MINUTES) & 0x7f);
00294     psGPSRTC->bRTC_sec  = INT_BCD(RTC_Read_Reg(RTC_SECONDS) & 0x7f);
00295 }
00296 
00297 //===================
00298 // Get Week String
00299 //===================
00300 unsigned char *Get_Week_String(unsigned int week)
00301 {
00302     static const char *WEEK[] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
00303 
00304     return (unsigned char*) WEEK[week];
00305 }
00306 
00307 //=====================
00308 // RTC Write Register
00309 //=====================
00310 void RTC_Write_Reg(unsigned int addr, unsigned int data)
00311 {
00312    char cmd[2];
00313    cmd[0]=addr;
00314    cmd[1]=data;
00315    const int addrw=0xA2;
00316    i2c.write(addrw,cmd,2);
00317 }
00318 
00319 //====================
00320 // RTC Read Register
00321 //====================
00322 unsigned int RTC_Read_Reg(unsigned int addr)
00323 {
00324     char read_buf[2];
00325     const int addrw=0xa2;
00326     const int addrr=0xa3;
00327     char write_cmd[1];
00328     write_cmd[0]=char(addr);
00329     i2c.write(addrw,write_cmd,1);
00330     wait(0.01);
00331     i2c.read(addrr,read_buf,1);
00332     //
00333     return read_buf[0];
00334 }
00335 
00336 //=====================
00337 // BCD from Integer
00338 //=====================
00339 unsigned char BCD_INT(unsigned char num)
00340 {
00341     return ((num / 10) << 4) + (num % 10);
00342 }
00343 
00344 //========================
00345 // Calculate x^n
00346 //========================
00347 signed int power(signed int x, signed int n)
00348 {
00349     unsigned int i;
00350     signed int  y;
00351 
00352     y = 1;
00353     for (i = 0; i < n; i++)
00354     {
00355         y = y * x;
00356     }
00357     return y;
00358 }
00359 
00360 //=====================
00361 // Integer from BCD
00362 //=====================
00363 unsigned char INT_BCD(unsigned char bcd)
00364 {
00365     return (((bcd >> 4) * 10) + (bcd & 0x0f));
00366 }
00367 
00368 //=========================================================
00369 // End of Program
00370 //=========================================================