Data logger: Sensors -> Barometer & temperature (BMP180), Humidity & temp. (RHT03), Sunshine (Cds): Display -> 20 chracters x 4 lines: Strage -> EEPROM (AT24C1024): Special functions -> Enter sleep mode to save current, reading the logging data via serial line

Dependencies:   AT24C1024 BMP180 M41T62 RHT03 TextLCD WakeUp mbed

Fork of LPC1114_barometer_with_data_logging by Kenji Arai

Please refer following Notebook.
http://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-data-logger/

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Sun Jun 15 03:35:18 2014 +0000
Parent:
10:398f62bb41f7
Child:
12:1e21119688fe
Commit message:
Barometer program / Step by step approach -> 5th step,; baro_BMP180.c -> Separated LCD part and BMP180 part and use exsiting Lib.(LCD) and new Lib.(BMP180)

Changed in this revision

BMP180.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
baro_BMP180.cpp Show diff for this revision Revisions of this file
lcd_AQM0802A.cpp Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BMP180.lib	Sun Jun 15 03:35:18 2014 +0000
@@ -0,0 +1,1 @@
+BMP180#9c1a7a1f0d97
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Sun Jun 15 03:35:18 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/wim/code/TextLCD/#986538f94abe
--- a/baro_BMP180.cpp	Sat Jun 14 01:37:54 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,303 +0,0 @@
-/*
- * mbed Application program for the mbed LPC1114F
- *  Control Bosch BMP180 Pressure Sensor, LCD "AQM0802A-RN-GBW"
- *
- *  Copyright (C) 2012,'13,'14 Kenji Arai/JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
- *  http://mbed.org/users/kenjiArai/
- *      September 30th, 2012    Change to STM32L
- *      August    14th, 2013
- *      May       24th, 2014    start mbed LPC1114
- *      June      13th, 2014
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
- * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-/*
- *---------------- REFERENCE ----------------------------------------------------------------------
- *      https://mbed.org/components/AQM0802A-RN-GBW/
- */
-/*
- *  LCD module "AQM0802A-RN-GBW" sample program
- *
- *  AQM0802A-RN-GBW is an I2C text LCD panel (Controller: Sitronix ST7032i)
- *  I bought this from AKIZUKI DENSHI TSUSHO CO.,LTD.
- *  http://akizukidenshi.com/catalog/g/gP-06669/ (Online shop page (Japanese))
- *  http://akizukidenshi.com/download/ds/sitronix/st7032.pdf (datasheet of the chip)
- *
- *  Original Arduino version was developed by
- *  Copyright (c) 2013 Masahiro WAKAYAMA at SWITCH SCIENCE
- *
- *  Copyright (c) 2013 Yoshihiro TSUBOI
- *  Released under the MIT License: http://mbed.org/license/mit
- *  revision 1.0  19-June-2013   a. 1st release
- */
-/*
- *---------------- REFERENCE ----------------------------------------------------------------------
- *      Bosch Sensortec BMP180 Datasheet : BST-BMP180-DS000-09 Revision: 2.5  Date: 5 April 2013
- */
-#include "mbed.h"
-
-I2C i2c(dp5,dp27);              // SDA, SCL
-
-//-------------------------------------------------------------------------------------------------
-//  LCD "AQM0802A-RN-GBW"
-//-------------------------------------------------------------------------------------------------
-// LCD fixed address
-const int AQCM0802_addr = 0x7C;
-// LCD contrast data
-unsigned char contrast = 0;     // 0-63
-char lcd_dt[2];
-
-//  LCD Control Program
-void lcd_cmd(char x) {      // Set command
-    lcd_dt[0] = 0x00; // CO = 0,RS = 0
-    lcd_dt[1] = x;
-    i2c.write(AQCM0802_addr, lcd_dt, 2);
-}
-
-void lcd_contdata(char x) {
-    lcd_dt[0] = 0xC0; //0b11000000 CO = 1, RS = 1
-    lcd_dt[1] = x;
-    i2c.write(AQCM0802_addr, lcd_dt, 2);
-}
-
-void lcd_lastdata(char x) {
-    lcd_dt[0] = 0x40; //0b10000000 CO = 0, RS = 1
-    lcd_dt[1] = x;
-    i2c.write(AQCM0802_addr, lcd_dt, 2);
-}
-
-void lcd_printStr(const char *s) {  // Print strings
-    while(*s) {
-        if(*(s + 1)) {
-          lcd_contdata(*s);
-        } else {
-          lcd_lastdata(*s);
-        }
-        s++;
-    }
-}
-
-void lcd_init() {       // Initialize LCD
-    wait(0.04);
-    // LCD initialize
-    lcd_cmd(0x38); // function set
-    lcd_cmd(0x39); // function set
-    lcd_cmd(0x04); // EntryModeSet
-    lcd_cmd(0x14); // interval osc
-    lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low
-    lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power
-    lcd_cmd(0x6C); // follower control
-    wait(0.2);
-    lcd_cmd(0x38); // function set
-    lcd_cmd(0x0C); // Display On
-    lcd_cmd(0x01); // Clear Display
-    wait(0.2); // need additional wait to Clear Display
-}
-
-void lcd_setCursor(unsigned char x,unsigned char y) {   // Set cursor position
-    lcd_cmd(0x80 | (y * 0x40 + x));
-}
-
-void setContrast(unsigned char c) {
-    lcd_cmd(0x39);
-    lcd_cmd(0x70 | (c & 0x0f)); // contrast Low
-    lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
-    lcd_cmd(0x38);
-}
-
-//-------------------------------------------------------------------------------------------------
-//  Barometer
-//-------------------------------------------------------------------------------------------------
-//  Barometer I2C ADDDRESS
-//  7bit address = 0b1110111(0x77) -> 8bit = 0b11101110(0xee) -> 0xef(Read) or 0xee(Write)
-#define BMP180ADDR  0xee                // No other choice
-
-// Over sampling Enable (1) or not(0)
-#define OSS_ON                  1
-
-//  Bosch barmeter ID
-#define BMP180_CHIP_ID          0x55
-#define UNKNOWN_ID              0
-
-//  register address
-#define BARO_PROM_ADDR          0xaa
-#define BARO_CHIP_ID_REG        0xd0
-#define BARO_VERSION_REG        0xd1
-#define BARO_CTRL_MEAS_REG      0xf4
-#define BARO_ADC_OUT_MSB_REG    0xf6
-#define BARO_ADC_OUT_LSB_REG    0xf7
-#define BARO_SOFT_RESET_REG     0xe0
-
-//  Calibration coefficients address
-#define B_AC1                   0xaa
-#define B_AC2                   0xac
-#define B_AC3                   0xae
-#define B_AC4                   0xb0
-#define B_AC5                   0xb2
-#define B_AC6                   0xb4
-#define B_B1                    0xb6
-#define B_B2                    0xb8
-#define B_MB                    0xba
-#define B_MC                    0xbc
-#define B_MD                    0xbe
-
-#define CONST_MG                3038
-#define CONST_MH                7357
-#define CONST_MI                3791
-
-//  Control data
-#define BARO_PROM_DATA__LEN     22
-#define B_TEMP_MEASURE          0x2e    // temperature measurent
-#define B_PRES_MEASURE          0x34    // pressure measurement
-#define B_PRES_MEASURE_OSS3     0xf4    // pressure /over sampling #3
-#define B_RESET_CMD             0xb6    // Reset chip command
-
-const char BMP180_addr = BMP180ADDR;    // define the I2C Address
-
-//  Barometer
-uint32_t baro_pres_data;                // Barometer /normalized
-int16_t  baro_temp_data;
-uint32_t baro_pressure_ave_data;        // Temperature /normalized
-int16_t  baro_temp_ave_data;
-uint8_t  sensor_type;                   // Get sensor ID
-
-//  Raw data
-char     baro_dt[BARO_PROM_DATA__LEN];
-int32_t  raw_pres;
-int32_t  raw_temp;
-uint8_t  oss_setting;
-
-//  EEPROM Data (Coefficient data)
-int16_t  eep_ac1;
-int16_t  eep_ac2;
-int16_t  eep_ac3;
-uint16_t eep_ac4;
-uint16_t eep_ac5;
-uint16_t eep_ac6;
-int16_t  eep_b1;
-int16_t  eep_b2;
-int16_t  eep_mb;     // not use
-int16_t  eep_mc;
-int16_t  eep_md;
-
-//  Barometer Control Program
-void baro_st_conv_temp( void ){     //  Start Conversion / temperature
-    baro_dt[0] = BARO_CTRL_MEAS_REG;
-    baro_dt[1] = B_TEMP_MEASURE; 
-    i2c.write(BMP180_addr, baro_dt, 2);
-}
-
-void baro_rd_press( void ){         //  Read temperature
-    baro_dt[0] = BARO_ADC_OUT_MSB_REG;    
-    i2c.write(BMP180_addr, baro_dt, 1);
-    i2c.read(BMP180_addr,baro_dt,3);
-    raw_pres = ( baro_dt[0] << 16 | baro_dt[1] << 8 | baro_dt[2] ) >> (8 - oss_setting );
-}
-
-void baro_st_conv_press( void ){    //  Start Conversion / Pressure
-    oss_setting = 3;
-    baro_dt[0] = BARO_CTRL_MEAS_REG;
-    baro_dt[1] = B_PRES_MEASURE_OSS3; 
-    i2c.write(BMP180_addr, baro_dt, 2);
-}
-
-void baro_rd_temp( void ){          //  Read pressure
-    baro_dt[0] = BARO_ADC_OUT_MSB_REG;    
-    i2c.write(BMP180_addr, baro_dt, 1);
-    i2c.read(BMP180_addr,baro_dt,3);
-    raw_temp = baro_dt[0] << 8 | baro_dt[1];
-}
-
-void baro_rd_coefficient( void ){   //  Read Coefficient data
-uint16_t i;
-
-    for( i= 0; i < BARO_PROM_DATA__LEN; i++ ){
-        baro_dt[i] = 0;
-    }
-    baro_dt[0] = BARO_PROM_ADDR;
-    i2c.write(BMP180_addr, baro_dt, 1);
-    i2c.read(BMP180_addr,baro_dt,BARO_PROM_DATA__LEN);
-    // parameters AC1-AC6
-    eep_ac1 =  (baro_dt[0] << 8)  | baro_dt[1];
-    eep_ac2 =  (baro_dt[2] << 8)  | baro_dt[3];
-    eep_ac3 =  (baro_dt[4] << 8)  | baro_dt[5];
-    eep_ac4 =  (baro_dt[6] << 8)  | baro_dt[7];
-    eep_ac5 =  (baro_dt[8] << 8)  | baro_dt[9];
-    eep_ac6 =  (baro_dt[10] << 8) | baro_dt[11];
-    // parameters B1,B2
-    eep_b1  =  (baro_dt[12] << 8) | baro_dt[13];
-    eep_b2  =  (baro_dt[14] << 8) | baro_dt[15];
-    // parameters MB,MC,MD
-    eep_mb  =  (baro_dt[16] << 8) | baro_dt[17];
-    eep_mc  =  (baro_dt[18] << 8) | baro_dt[19];
-    eep_md  =  (baro_dt[20] << 8) | baro_dt[21];
-}
-
-void baro_rd_id( void ){            //  Read Chip ID
-    baro_dt[0] = BARO_CHIP_ID_REG;
-    i2c.write(BMP180_addr, baro_dt, 1);
-    i2c.read(BMP180_addr,baro_dt,1);
-    sensor_type = baro_dt[0];
-}
-
-/*
- *  Pressure Nomailzation
- *      Reference: Bosch Sensortec  BMP180  Datasheet=BST-BMP180-DS000-09
- *                  Revision: 2.5  Date: 5 April 2013  Page15   
- *      http://www.bosch-sensortec.com/homepage/products_3/environmental_sensors_1/bmp180_1/bmp180
- */
-void cal_pressure( void ){
-int32_t  dt_x1 = 0, dt_x2 = 0, dt_x3, dt_b3, dt_b5 = 0, dt_b6;
-uint32_t dt_b4, dt_b7;
-long long int d;
-
-    if ( sensor_type == BMP180_CHIP_ID ){
-        dt_x1 = ( ( raw_temp - (int32_t)eep_ac6 ) * (int32_t)eep_ac5 ) >> 15;
-        dt_x2 = ( (int32_t)eep_mc << 11 ) / ( dt_x1 + (int32_t)eep_md );
-        dt_b5 = dt_x1 + dt_x2;
-    }
-    baro_temp_data = ( ( dt_b5 + 8 ) >> 4 );// temperature in 0.1 degC
-    // Cal_pressure
-    dt_b6 = dt_b5 - 4000;
-    dt_x1 = ( dt_b6 * dt_b6 ) >> 12;
-    dt_x1 *= eep_b2;
-    dt_x1 >>= 11;
-    dt_x2 = ( eep_ac2 * dt_b6 );
-    dt_x2 >>= 11;
-    dt_x3 = dt_x1 + dt_x2;
-    dt_b3 = ( ((((long)eep_ac1) * 4 + dt_x3 ) << oss_setting ) + 2 ) >> 2;
-    dt_x1 = ( eep_ac3 * dt_b6 ) >> 13;
-    dt_x2 = ( eep_b1 * ( ( dt_b6 * dt_b6 ) >> 12 ) ) >> 16;
-    dt_x3 = ( ( dt_x1 + dt_x2 ) + 2 ) >> 2;
-    dt_b4 = ( eep_ac4 * (uint32_t)(dt_x3 + 32768)) >> 15;
-    dt_b7 = ( (uint32_t)( raw_pres - dt_b3 ) * ( 50000>> oss_setting ) );
-    if (dt_b7 < 0x80000000){
-        baro_pres_data = (dt_b7 << 1) / dt_b4;
-    } else {
-        baro_pres_data = (dt_b7 / dt_b4) << 1;
-    }
-    d = (long long int)baro_pres_data;
-    d *= d;
-    dt_x1 = (int32_t)( d / 65536 );
-    dt_x1 = ( dt_x1 * CONST_MG ) >> 16;
-    dt_x2 = ( CONST_MH * baro_pres_data ) >> 16;
-    dt_x2 *= -1;
-    // pressure in Pa
-    baro_pres_data += ( dt_x1 + dt_x2 + CONST_MI ) >> 4;
-    /* averaging */
-    if ( baro_pressure_ave_data == 0){
-        baro_pressure_ave_data = baro_pres_data;
-    } else {
-        baro_pressure_ave_data = (( baro_pressure_ave_data * 9 ) + ( baro_pres_data * 1 )) / 10;
-    }
-    if ( baro_temp_ave_data == 0){
-        baro_temp_ave_data = baro_temp_data;
-    } else {
-        baro_temp_ave_data = (( baro_temp_ave_data * 9 ) + ( baro_temp_data * 1 )) / 10;
-    }
-}
--- a/lcd_AQM0802A.cpp	Sat Jun 14 01:37:54 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,217 +0,0 @@
-/*
- *  LCD module "AQM0802A-RN-GBW" sample program
- *
- *  AQM0802A-RN-GBW is an I2C text LCD panel (Controller: Sitronix ST7032i)
- *  I bought this from AKIZUKI DENSHI TSUSHO CO.,LTD.
- *  http://akizukidenshi.com/catalog/g/gP-06669/ (Online shop page (Japanese))
- *  http://akizukidenshi.com/download/ds/sitronix/st7032.pdf (datasheet of the chip)
- *
- *  Original Arduino version was developed by
- *  Copyright (c) 2013 Masahiro WAKAYAMA at SWITCH SCIENCE
- *
- *  Copyright (c) 2013 Yoshihiro TSUBOI
- *
- *  Released under the MIT License: http://mbed.org/license/mit
- *
- *  revision 1.0  19-June-2013   a. 1st release
- *
- */
-/*
- * CAUTION!!!!!!
- * NOT USE THIS FILE because most of functions were coppied into AQM0802A.h file
- *
- *  Modified by Kenji Arai/JH1PJL
- *  http://www.page.sannet.ne.jp/kenjia/index.html
- *  http://mbed.org/users/kenjiArai/
- */
-#if 0
- #include "mbed.h"
-
-I2C i2c(dp5, dp27); // sda, scl
-const int AQCM0802_addr = 0x7C;
-
-unsigned char mode;
-unsigned char contrast = 0; // 0-63
-unsigned char contrastFlag = false;
-int CGcounter;
-int FADEcounter;
-
-void lcd_cmd(char x) {
-  char data[2];
-  data[0] = 0x00; // CO = 0,RS = 0
-  data[1] = x;
-  i2c.write(AQCM0802_addr, data, 2);
-}
-
-void lcd_contdata(char x) {
-  char data[2];
-  data[0] = 0xC0; //0b11000000 CO = 1, RS = 1
-  data[1] = x;
-  i2c.write(AQCM0802_addr, data, 2);
-}
-
-void lcd_lastdata(char x) {
-  char data[2];
-  data[0] = 0x40; //0b11000000 CO = 0, RS = 1
-  data[1] = x;
-  i2c.write(AQCM0802_addr, data, 2);
-}
-
-void lcd_printStr(const char *s) {
-  while(*s) {
-    if(*(s + 1)) {
-      lcd_contdata(*s);
-    } else {
-      lcd_lastdata(*s);
-    }
-    s++;
-  }
-}
-
-void lcd_printHex(unsigned char num) {
-  lcd_contdata(num);
-}
-
-void lcd_init() {
-  wait(0.04);
-  // LCD initialize
-  lcd_cmd(0x38); // function set
-  lcd_cmd(0x39); // function set
-  lcd_cmd(0x04); // EntryModeSet
-  lcd_cmd(0x14); // interval osc
-  lcd_cmd(0x70 | (contrast & 0xF)); // contrast Low
-  lcd_cmd(0x5C | ((contrast >> 4) & 0x3)); // contast High/icon/power
-  lcd_cmd(0x6C); // follower control
-  wait(0.2);
-  lcd_cmd(0x38); // function set
-  lcd_cmd(0x0C); // Display On
-  lcd_cmd(0x01); // Clear Display
-  wait(0.2); // need additional wait to Clear Display
-}
-
-void lcd_setCursor(unsigned char x,unsigned char y) {
-  lcd_cmd(0x80 | (y * 0x40 + x));
-}
-
-unsigned char cg[13 * 8] = {
-/*
-  0b00001111,0b00010000,0b00010000,0b00001110,0b00000001,0b00000001,0b00011110,0b00000000,
-  0b00010001,0b00010001,0b00010001,0b00010101,0b00010101,0b00010101,0b00001010,0b00000000,
-  0b00001110,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00001110,0b00000000,
-  0b00011111,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00000000,
-  0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
-  0b00010001,0b00010001,0b00010001,0b00011111,0b00010001,0b00010001,0b00010001,0b00000000,
-*/
-  0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E,0x00, // S
-  0x11,0x11,0x11,0x15,0x15,0x15,0x0A,0x00, // W
-  0x0E,0x40,0x40,0x40,0x40,0x40,0x0E,0x00, // I
-  0x1F,0x40,0x40,0x40,0x40,0x40,0x40,0x00, // T
-  0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
-  0x11,0x11,0x11,0x1F,0x11,0x11,0x11,0x00, // H
-/*
-  0b00001111,0b00010000,0b00010000,0b00001110,0b00000001,0b00000001,0b00011110,0b00000000,
-  0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
-  0b00001110,0b00000100,0b00000100,0b00000100,0b00000100,0b00000100,0b00001110,0b00000000,
-  0b00011111,0b00010000,0b00010000,0b00011110,0b00010000,0b00010000,0b00011111,0b00000000,
-  0b00010001,0b00010001,0b00011001,0b00010101,0b00010011,0b00010001,0b00010001,0b00000000,
-  0b00001110,0b00010001,0b00010000,0b00010000,0b00010000,0b00010001,0b00001110,0b00000000,
-  0b00011111,0b00010000,0b00010000,0b00011110,0b00010000,0b00010000,0b00011111,0b00000000,
-*/
-  0x0F,0x10,0x10,0x0E,0x01,0x01,0x1E,0x00, // S
-  0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
-  0x0E,0x40,0x40,0x40,0x40,0x40,0x0E,0x00, // I
-  0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00, // E
-  0x11,0x11,0x19,0x15,0x13,0x11,0x11,0x00, // N
-  0x0E,0x11,0x10,0x10,0x10,0x11,0x0E,0x00, // C
-  0x1F,0x10,0x10,0x1E,0x10,0x10,0x1F,0x00  // E
-};
-
-void setCG(int src,int dst,int len) {
-  lcd_cmd(0x38);
-  lcd_cmd(0x40 + dst);
-  if (src >= 0) {
-    for (int i = 0;i < len;i++) lcd_printHex(cg[src + i]);
-  } else {
-    for (int i = 0;i < len;i++) lcd_printHex(0);
-  }
-}
-
-void setContrast(unsigned char c) {
-  lcd_cmd(0x39);
-  lcd_cmd(0x70 | (c & 0x0f)); // contrast Low
-  lcd_cmd(0x5C | ((c >> 4) & 0x03)); // contast High/icon/power
-  lcd_cmd(0x38);
-}
-
-int main() {
-    lcd_init();
-    while(1) {
-      switch(mode) {
-        case 0:  // init
-          lcd_setCursor(0, 0);
-          for (int i = 0;i < 6;i++) lcd_printHex(i);
-          lcd_setCursor(1, 1);
-          lcd_printStr("       ");
-          setCG(-1,0,6 * 8);
-          contrast = 35;
-          setContrast(contrast);
-          CGcounter = 0;
-          mode++;
-          break;
-        case 1:  // SWITCH
-          if(CGcounter <= (6 * 8)) {
-            setCG((CGcounter / 8) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
-            CGcounter++;
-          } else {
-            lcd_setCursor(0, 0);
-            lcd_printStr("SWITCH");
-            setCG(-1,0,3 * 8);
-            lcd_setCursor(1, 1);
-            for (int i = 0;i < 3;i++) lcd_printHex(i);
-            CGcounter = 0;
-            mode++;
-          }
-          break;
-        case 2:  // SCI
-          if(CGcounter <= (3 * 8)) {
-            setCG(((CGcounter / 8) + 6) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
-            CGcounter++;
-          } else {
-            lcd_setCursor(1, 1);
-            lcd_printStr("SCI");
-            setCG(-1,0,4 * 8);
-            lcd_setCursor(4, 1);
-            for (int i = 0;i < 4;i++) lcd_printHex(i);
-            CGcounter = 0;
-            mode++;
-          }
-          break;
-        case 3:  // ENCE
-          if(CGcounter <= (4 * 8)) {
-            setCG(((CGcounter / 8) + 9) * 8,((CGcounter / 8) * 8) + 7 - (CGcounter % 8),CGcounter % 8);
-            CGcounter++;
-          } else {
-            lcd_setCursor(4, 1);
-            lcd_printStr("ENCE");
-            FADEcounter = 0;
-            mode++;
-          }
-          break;
-        case 4:
-          if (contrastFlag == false) {
-            if (++contrast >= 54) contrastFlag = true;
-          } else {
-            if (--contrast <= 17) {
-              contrastFlag = false;
-              if(++FADEcounter >= 2) {
-                mode = 0;
-              }
-            }
-          }
-          setContrast(contrast);
-          break;
-      }
-      wait(0.05);
-    }
-}
-#endif
--- a/main.cpp	Sat Jun 14 01:37:54 2014 +0000
+++ b/main.cpp	Sun Jun 15 03:35:18 2014 +0000
@@ -14,17 +14,19 @@
  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
-#include "mbed.h"
-#include "RHT03.h" //Include neede to use the RHT03 lib
 
-#define DEBUG_L1       1        // 1=Debug, 0=Normal
-#define DEBUG_L2       1        // 1=Debug, 0=Normal
+#include "mbed.h"
+#include "BMP180.h"             // Pressure sensor             
+#include "RHT03.h"              // Humidity sensor
+#include "TextLCD.h"
 
-#define VREF_VOLT   2.482       // TA76431F Vref real measued data
-#define R_FIX       9930        // 10K ohm <- real measued data
-#define VOL_OFFSET  4           // Offset data ,= real measured data
+#define VREF_VOLT       2.482   // TA76431F Vref real measued data
+#define R_FIX           9930    // 10K ohm <- real measued data
+#define VOL_OFFSET      4       // Offset data ,= real measured data
 #define CDS_TBL_SIZE    13
 
+
+I2C         i2c(dp5,dp27);      // SDA, SCL
 DigitalOut  myled0(dp28);       // LED for Debug
 DigitalOut  myled1(dp14);       // Indicate state transition
 DigitalOut  analog_pwr(dp6);    // VCC for analog interface (vol, cds and vref)
@@ -35,7 +37,9 @@
 AnalogIn    vref(dp9);          // Input / Bandgap 2.5V
 AnalogIn    vol(dp10);          // Input / contrast volume
 RHT03       humtemp(dp26);      // RHT03 interface
-Serial      pc(dp16,dp15);
+Serial      pc(dp16,dp15);      // UART (vertual COM)
+BMP180      bmp180(i2c);        // Bosch sensor
+TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2);
 
 typedef enum {CDS = 0, VREF, VOL} ADC_Select;
 
@@ -44,16 +48,6 @@
 float r_cds, lux;
 uint32_t nor_vol;
 
-//  LCD
-extern unsigned char contrast;
-char buf[16];
-
-//  Barometer
-extern uint32_t baro_pressure_ave_data; // Temperature /normalized
-extern int16_t  baro_temp_ave_data;
-extern uint32_t baro_pres_data;         // Barometer /normalized
-extern int16_t  baro_temp_data;
-
 //  Humidity Sensor
 float humidity_temp, humidity;
 
@@ -63,195 +57,37 @@
     {{50,21194},{100,8356},{200,3294},{400,1299},{800,512},{1600,202},{3200,79.6},{6400,31.4},
     {12800,12.4},{25600,4.88},{51200,1.92},{102400,0.758},{409600,0.118}};
 
-#if DEBUG_L2
-char *const imsg0 = "-->Control Reg.";
-char *const imsg1 = "-->Status Reg.";
-char *const imsg2 = "-->Data Reg.";
-char *const imsg3 = "-->Clock control Reg.";
-static char *const io_port_name0 = "PIO0_";
-static char *const iomsg0 = "Func->select ";
-static char *const iomsg1 = "IO";
-static char *const iomsg2 = "Reserved";
-static char *const iomsg3 = "Std/F-md I2C";
-#endif  // DEBUG_L2
-
-// Functions
-//      Barometer
-void baro_st_conv_temp( void );         // Start Conversion
-void baro_st_conv_press( void );        // Start Conversion
-void baro_rd_temp( void );              // Receive Press. & Temp.
-void baro_rd_press( void );             // Receive Press. & Temp.
-void baro_rd_coefficient( void );       // Receive Coefficient data
-void baro_rd_id( void );                // Receive Chip ID 
-void cal_pressure( void );              // Calculate Pressure
-//      LCD
-void lcd_init();                        // Initialize LCD
-void lcd_printStr(const char *s);       // Put strings
-void lcd_setCursor(unsigned char x,unsigned char y);    // Set cursor position
-void setContrast(unsigned char c);      // Set contrast
-
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
-#if DEBUG_L2
-//  Put \r\n
-void put_rn ( void ){
-    pc.putc('\r');
-    pc.putc('\n');
-}
-
-//  Put \r
-void put_r ( void ){
-    pc.putc('\r');
-}
-
-// Put ", "
-void put_lin ( void ){
-    pc.printf(", ");
-}
-
-void debug_port_check( void ){
-uint32_t r0,r1,r2,r3,r4,r5,r6,r7;
-    // Show registers
-    put_rn();
-    pc.printf( "------- Show Registers -------" );
-    put_rn(); 
-    pc.printf( "**** P0_4,P0_5 Port usage ****" );
-    put_rn();
-    // P0_4
-    r0 = LPC_IOCON->PIO0_4;
-    pc.printf( "%s4(dp27)",io_port_name0 );
-    pc.printf( " 0x%08x", r0 );
-    put_rn();
-    pc.printf( iomsg0 );
-    switch ( r0 & 0x7 ){
-    case 0:        pc.printf( iomsg1 );     break;
-    case 1:        pc.printf("SCL");        break;
-    }
-    put_lin();
-    switch ( ( r0 >> 8 ) & 0x3 ){
-    case 0:        pc.printf( iomsg3 );     break;
-    case 1:        pc.printf( iomsg1);      break;
-    case 2:        pc.printf("Fast md");    break;
-    case 3:        pc.printf( iomsg2 );     break;
-    }
-    put_rn();
-    // P0_5
-    r0 = LPC_IOCON->PIO0_5;
-    pc.printf( "%s5(dp5)",io_port_name0 );
-    pc.printf( " 0x%08x", r0 );
-    put_rn();
-    pc.printf( iomsg0 );
-    switch ( r0 & 0x7 ){
-    case 0:        pc.printf( iomsg1 );     break;
-    case 1:        pc.printf("SDA");        break;
-    }
-    put_lin();
-    switch ( ( r0 >> 8 ) & 0x3 ){
-    case 0:        pc.printf( iomsg3 );     break;
-    case 1:        pc.printf( iomsg1 );     break;
-    case 2:        pc.printf("Fast md");    break;
-    case 3:        pc.printf( iomsg2 );     break;
-    }
-    put_rn();
-    // I2C Control register
-    r0 = LPC_I2C->CONSET;
-    r1 = LPC_I2C->STAT;
-    r2 = LPC_I2C->DAT;
-    r3 = LPC_I2C->SCLH;
-    r4 = LPC_I2C->SCLL;
-    r5 = LPC_I2C->CONCLR;
-    r6 = LPC_I2C->MMCTRL;
-    r7 = LPC_I2C->DATA_BUFFER;
-    pc.printf( "**** Show I2C Registers ****" );
-    put_rn();
-    pc.printf( "CONSET" );
-    pc.printf( imsg0 );
-    pc.printf( " 0x%08x", r0 );
-    put_rn();
-    pc.printf( "STAT" );
-    pc.printf( imsg1 );
-    pc.printf( " 0x%08x", r1 );
-    put_rn();
-    pc.printf( "DAT" );
-    pc.printf( imsg2 );
-    pc.printf( " 0x%08x", r2 );
-    put_rn();
-    pc.printf( "ADR0--Not support" );
-    put_rn();
-    pc.printf( "SCLH" );
-    pc.printf( imsg0 );
-    pc.printf( " 0x%08x", r3 );
-    put_rn();
-    pc.printf( "SCLL" );
-    pc.printf( imsg0 );
-    pc.printf( " 0x%08x", r4 );
-    put_rn();
-    pc.printf( "CONCLR" );
-    pc.printf( imsg0 );
-    pc.printf( " 0x%08x", r5 );
-    put_rn();
-    pc.printf( "MMCTRL" );
-    pc.printf( imsg0 );
-    pc.printf( " 0x%08x", r6 );
-    put_rn();
-    pc.printf( "ADR1,2,3--Not support" );
-    put_rn();
-    pc.printf( "DATA_BUFFER" );
-    pc.printf( imsg3 );
-    pc.printf( " 0x%08x", r7 );
-    put_rn();
-    pc.printf( "MASK0,1,2,3--Not support" );
-    put_rn();
-}
-#endif  // DEBUG_L2
-
+// Normalize ADC data
 void adc_normalize( ADC_Select n ){
 int i;
 float x1,y1,dx;
 
     switch (n){
     case CDS:
-        // v_adc = Rfix / (Rcds + Rfix)
-        // Rcds = ( Rfix / v_adc ) - Rfix 
+        // v_adc = Rfix / (Rcds + Rfix) ->  Rcds = ( Rfix / v_adc ) - Rfix 
         r_cds = (R_FIX / av_cds) - R_FIX;
         // CDS resistance to Lux conversion using convertion table (luc_cds[][])
-        //  with Linear interpolation method
-        for (i =0; i < CDS_TBL_SIZE; i++){
-            if ( r_cds <= lux_cds[i][0]){
+        for (i =0; i < CDS_TBL_SIZE; i++){  // search table
+            if ( r_cds <= lux_cds[i][0]){ break; }
+        }
+        // Check table position
+        if (i == 0){
+            lux = lux_cds[0][1];
+            break;
+        } else if ( i == CDS_TBL_SIZE ){
+            if ( r_cds <= lux_cds[i][0] ){
+                lux = lux_cds[i-1][1];
                 break;
             }
         }
-#if DEBUG_L1
-        pc.printf( "i=%d, ", i );
-#endif  // DEBUG_L1
-        if (i == 0){
-            lux = lux_cds[0][1];
-#if DEBUG_L1
-            pc.printf( "range over!\r\n" );
-#endif // DEBUG_L1
-        } else if ( i == CDS_TBL_SIZE ){
-            if ( r_cds <= lux_cds[i][0] ){
-                lux = lux_cds[i-1][1];
-            }
-        } else {
-            if ( i == CDS_TBL_SIZE ){
-                if ( r_cds <= lux_cds[i][0] ){
-                    lux = lux_cds[i-1][1];
-#if DEBUG_L1
-                    pc.printf("range over!\r\n");
-#endif // DEBUG_L1
-                    break;
-                }
-            }
-            y1 = lux_cds[i-1][1] - lux_cds[i][1];
-            x1 = lux_cds[i][0] - lux_cds[i-1][0];
-            dx = r_cds - lux_cds[i-1][0];
-            lux = lux_cds[i-1][1] - ((dx/x1) * y1);
-#if DEBUG_L1
-            pc.printf( "y1:%f, x1:%f, dx:%f, lux_tbl:%f\r\n", y1, x1, dx, lux_cds[i-1][1] );
-#endif // DEBUG_L1
-        }
+        //  Linear interpolation
+        y1 = lux_cds[i-1][1] - lux_cds[i][1];
+        x1 = lux_cds[i][0] - lux_cds[i-1][0];
+        dx = r_cds - lux_cds[i-1][0];
+        lux = lux_cds[i-1][1] - ((dx/x1) * y1);
         break;
     case VREF:
         //  vref = VREF_VOLT / VCC -> VCC = VREF_VOLT / vref
@@ -264,83 +100,64 @@
     }
 }
 
+//  Read adc data and averaging
 void adc_all_read( void){
-    if ( av_cds == 0 ){
-        av_cds = cds.read();
-    } else {
-        av_cds = av_cds *0.5 + cds.read() * 0.5;
+    if (av_cds == 0){   av_cds = cds.read();
+    } else {            av_cds = av_cds *0.5 + cds.read() * 0.5;
     }
-    if ( av_vref == 0 ){
-        av_vref = vref.read();
-    } else {    
-        av_vref = av_vref *0.9 + vref.read() * 0.1;
+    if (av_vref == 0){  av_vref = vref.read();
+    } else {            av_vref = av_vref *0.9 + vref.read() * 0.1;
     }
-    if ( av_vol == 0 ){        
-        av_vol = vol.read();
-    } else {  
-        av_vol = av_vol *0.2 + vol.read() * 0.8;
+    if (av_vol == 0){   av_vol = vol.read();
+    } else {            av_vol = av_vol *0.2 + vol.read() * 0.8;
     } 
 }
 
+// Read Humidity sensor data
 void hum_RHT03_read( void){
-    while (true){
-        if ( humtemp.readData() == RHT_ERROR_NONE ){ //Request data from the RHT03
-            break;
-        }
+    while (true){   // wait data
+        if ( humtemp.readData() == RHT_ERROR_NONE ){ break; }
     }
-    if ( humidity_temp == 0 ){
-        humidity_temp = humtemp.getTemperatureC();
-    } else {           
-        humidity_temp = humidity_temp * 0.9 + humtemp.getTemperatureC() * 0.1;
+    if (humidity_temp == 0){humidity_temp = humtemp.getTemperatureC();
+    } else {                humidity_temp = humidity_temp * 0.9 + humtemp.getTemperatureC() * 0.1;
     }
-    if ( humidity == 0 ){
-        humidity = humtemp.getHumidity();
-    } else {   
-        humidity = humidity * 0.9 + humtemp.getHumidity() * 0.1;
+    if ( humidity == 0 ){   humidity = humtemp.getHumidity();
+    } else {                humidity = humidity * 0.9 + humtemp.getHumidity() * 0.1;
     }
 }
 
+//-------------------------------------
+// Application program starts here
+//-------------------------------------
 int main() {
-uint16_t dt;
-
     pc.baud(9600);
-    pc.printf( "\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"\r\n" );
-    // Initialize LCD
-    lcd_init();
-    contrast = 25;
-    setContrast(contrast);
-    //Initial screen shot
-    lcd_setCursor(0, 0);
-    //            12345678
-    lcd_printStr("LPC1114F");
-    lcd_setCursor(0, 1);
-    //            12345678
-    lcd_printStr(" JH1PJL ");
-    // Read BMP180 data / only once
-    baro_rd_id();
-    baro_rd_coefficient();
-    // Show initial screen
-    wait(5.0);
+    pc.printf("\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"\r\n");
+    i2clcd.setContrast(25);
+    i2clcd.locate(0, 0);
+    i2clcd.printf("LPC1114F");
+    i2clcd.locate(0, 1);
+    i2clcd.printf(" JH1PJL ");
     // Initialize data
     av_cds = 0;
     av_vref = 0;
     av_vol = 0;
     humidity_temp = 0;
     humidity = 0;
+    // Show initial screen
+    wait(5.0);
     while(1) {
-        //  ---------- Cds Sensor, Vref, Volume ---------------------------------------------------
+    //  ---------- Cds Sensor, Vref, Volume ---------------------------------------------------
         // Power on / Analog sensor
         analog_pwr = 1;
         vref_pwr = 1;
         wait(0.2);
         adc_all_read();
         // Power off / Analog sensor
-        analog_pwr = 0;
-        //vref_pwr = 0; 
+        analog_pwr = 0; 
         // Normalize
-        adc_normalize( CDS );
-        adc_normalize( VREF );
-        adc_normalize( VOL );       
+        adc_normalize(CDS);
+        adc_normalize(VREF);
+        adc_normalize(VOL);       
         myled0 = 1;
         if (sw_chng == 1){  // SW Off
             pc.printf( "\r\nCds:%.0fohm -> %.1flux, Vcc:%.3fV, Vol:%d\r\n",
@@ -349,54 +166,32 @@
             pc.printf( "\r\nCds:%f, Vref:%f, Vol:%f\r\n", av_cds, av_vref, av_vol );
         }
         myled0 = 0;
-        lcd_setCursor(0, 0);    // 1st line top
-        dt = (uint16_t)(lux * 10);
-        //sprintf( buf,"l: %4.1f ", lux );
-        sprintf( buf,"L:%4d.%01d ", dt / 10, dt % 10 );
-        lcd_printStr(buf);
-        lcd_setCursor(0, 1);    // 2nd line top
-        sprintf( buf,"V: %1.3f ", cal_vcc );
-        lcd_printStr(buf);
+        i2clcd.locate(0, 0);    // 1st line top
+        //             12345678
+        i2clcd.printf("L: %4.1f", lux);
+        i2clcd.locate(0, 1);    // 2nd line top
+        i2clcd.printf("V: %1.3f", cal_vcc);
         wait(2.0);
-        //  ---------- Barometer Sensor / BMP180 --------------------------------------------------
-        baro_st_conv_temp();    // start temprerature measurment
-        wait(0.25);       // wait for convertion
-        baro_rd_temp();         // read it
-        baro_st_conv_press();   // start pressure measurement
-        wait(0.5);       // wait for convertion
-        baro_rd_press();        // read it
-        cal_pressure();         // Calculate baro & temp.
-        lcd_setCursor(0, 0);    // 1st line top
-        sprintf( buf,"P:%4d.%01d  ", baro_pres_data / 100, ( baro_pres_data % 100 ) /10 );
-        lcd_printStr(buf);
-        lcd_setCursor(0, 1);    // 2nd line top
-        if (sw_chng == 0){      // SW ON
-            baro_temp_data = -100;  // Dummy -10.0 degC -> Test printf() minus display
-        }
-        sprintf( buf,"T: %\+-d.%01d ", baro_temp_data / 10, baro_temp_data% 10 );
-        lcd_printStr(buf);
+    //  ---------- Barometer Sensor / BMP180 --------------------------------------------------
+        bmp180.normalize();
+        i2clcd.locate(0, 0);    // 1st line top
+        i2clcd.printf("P:%4.1f", bmp180.read_pressure());
+        i2clcd.locate(0, 1);    // 2nd line top
+        i2clcd.printf("T: %\+-0.1f", bmp180.read_temperature());
         myled1 = 1;
-        pc.printf( "Pres:%4d.%01dhPa, Temp:%\+-d.%01ddegC\r\n",
-             baro_pres_data / 100, ( baro_pres_data % 100 ) /10,
-             baro_temp_data / 10, baro_temp_data% 10 );
+        pc.printf("Pres:%4.1fhPa, Temp:%\+-0.1fdegC\r\n",
+                    bmp180.read_pressure(), bmp180.read_temperature());
         myled1 = 0;
         wait(4.0);
-        //  ---------- Humidity Sensor / RHT03 ----------------------------------------------------
+    //  ---------- Humidity Sensor / RHT03 ----------------------------------------------------
         hum_RHT03_read();       // Read Humidity data then avaraging
-        lcd_setCursor(0, 0);    // 1st line top
-        dt = (uint16_t)(lux * 10);
-        //sprintf( buf,"l: %4.1f ", lux );
-        sprintf( buf,"H:  %2.1f ", humidity );
-        lcd_printStr(buf);
-        lcd_setCursor(0, 1);    // 2nd line top
-        sprintf( buf,"T: %\+-0.1f", humidity_temp );
-        lcd_printStr(buf);
+        i2clcd.locate(0, 0);    // 1st line top
+        i2clcd.printf("H:  %2.1f", humidity);
+        i2clcd.locate(0, 1);    // 2nd line top
+        i2clcd.printf("T: %\+-0.1f", humidity_temp);
         myled1 = 1;
-        pc.printf( "Humid: %0.1f%cRH, Temp:%\+-0.1fdegC\r\n", humidity, '%', humidity_temp );        
+        pc.printf("Humid: %0.1f%cRH, Temp:%\+-0.1fdegC\r\n", humidity, '%', humidity_temp);        
         myled1 = 0;  
         wait(2.0);
-#if DEBUG_L2
-        debug_port_check();
-#endif  // DEBUG_L2     
     }
 }