test

Dependencies:   FatFileSystem TextLCD mbed

Committer:
y_notsu
Date:
Tue Sep 18 07:24:22 2012 +0000
Revision:
0:2c37ad282618
test

Who changed what in which revision?

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