Barometer program : Data Logger function includes Barometer & temperature (BMP180), Humidity & temp. (RHT03), Sunshine (Cds), RTC(M41T62) data. : Logging data saves into EEPROM (AT24C1024) using ring buffer function.

Dependencies:   AT24C1024 RHT03 TextLCD BMP180 M41T62

Fork of mbed_blinky by Mbed

Please see https://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-barometer-with-data-logging/#

Revision:
12:1e21119688fe
Parent:
11:bccfd75e84a0
Child:
13:950adc9d6d61
--- a/main.cpp	Sun Jun 15 03:35:18 2014 +0000
+++ b/main.cpp	Fri Jun 20 21:45:12 2014 +0000
@@ -1,12 +1,12 @@
 /*
  * mbed Application program for the mbed LPC1114FN28
- * Test program -> Check LED & Switch function 
+ * Test program -> Check EEPROM
  *
  * Copyright (c) 2014 Kenji Arai / JH1PJL
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: June      13th, 2014
- *      Revised: June      14th, 2014
+ *      Revised: June      21st, 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
@@ -16,16 +16,16 @@
  */
 
 #include "mbed.h"
-#include "BMP180.h"             // Pressure sensor             
-#include "RHT03.h"              // Humidity sensor
-#include "TextLCD.h"
+#include "BMP180.h"             // Own lib. / Pressure sensor             
+#include "RHT03.h"              // Std. lib./ Humidity sensor
+#include "TextLCD.h"            // Std. lib./ LCD control
+#include "AT24C1024.h"             // Std. lib./ EEPROM control
 
 #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 VOL_OFFSET      3       // 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
@@ -39,7 +39,8 @@
 RHT03       humtemp(dp26);      // RHT03 interface
 Serial      pc(dp16,dp15);      // UART (vertual COM)
 BMP180      bmp180(i2c);        // Bosch sensor
-TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2);
+TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki AQM0802A)
+AT24C1024   at24c1024(i2c);     // Atmel 1Mbit EE-PROM 
 
 typedef enum {CDS = 0, VREF, VOL} ADC_Select;
 
@@ -51,12 +52,30 @@
 //  Humidity Sensor
 float humidity_temp, humidity;
 
+//  EEPROM
+uint8_t eep_buf[256 + 2];
+
 // Cds GL5528 (Dark Resistance 1 Mohm type) SENBA OPTICAL & ELECTRONIC CO.,LTD.
 //      Table value referrence: http://homepage3.nifty.com/skomo/f35/hp35_20.htm
 const float lux_cds[CDS_TBL_SIZE][2] =
     {{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}};
 
+// EEPROM test charcters
+const uint8_t eep_char[256] =
+//0        1         2         3         4         5         6         7
+//12345678901234567890123456789012345678901234567890123456789012345678901
+ "The AT24C1024 provides 1048576 bits of serial electrically erasable and"
+//7       8         9        10        11        12        13        14        15
+//2345678901234567890123456789012345678901234567890123456789012345678901234567890123
+ " programmable read only memory (EEPROM) organized as 131,072 words of 8 bits each."
+//15   16        17        18        19        20        21        22        23        24
+//45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
+ " The devices cascadable feature allows up to four devices to share a common two-wire bus."
+//24    25   256
+//34567890123456
+ "  JH1PJL Arai"; // eep_char[255] = 0;
+
 //-------------------------------------------------------------------------------------------------
 //  Control Program
 //-------------------------------------------------------------------------------------------------
@@ -126,12 +145,24 @@
     }
 }
 
+// Clear LCD
+void cls( void){
+    i2clcd.locate(0, 0);
+    i2clcd.printf("        ");
+    i2clcd.locate(0, 1);
+    i2clcd.printf("        ");
+}
+
 //-------------------------------------
 // Application program starts here
 //-------------------------------------
 int main() {
+int no =0;
+uint8_t dt[4];
+AT24C_STATUS status;
+
     pc.baud(9600);
-    pc.printf("\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"\r\n");
+    pc.printf("\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"(UTC)\r\n");
     i2clcd.setContrast(25);
     i2clcd.locate(0, 0);
     i2clcd.printf("LPC1114F");
@@ -144,7 +175,7 @@
     humidity_temp = 0;
     humidity = 0;
     // Show initial screen
-    wait(5.0);
+    wait(5.0);    
     while(1) {
     //  ---------- Cds Sensor, Vref, Volume ---------------------------------------------------
         // Power on / Analog sensor
@@ -157,41 +188,148 @@
         // Normalize
         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",
-                r_cds, lux, cal_vcc, nor_vol );
-        } else {            // SW On
-            pc.printf( "\r\nCds:%f, Vref:%f, Vol:%f\r\n", av_cds, av_vref, av_vol );
-        }
-        myled0 = 0;
+        adc_normalize(VOL);
+        cls();       
         i2clcd.locate(0, 0);    // 1st line top
         //             12345678
-        i2clcd.printf("L: %4.1f", lux);
+        i2clcd.printf("L:%.1f", lux);
         i2clcd.locate(0, 1);    // 2nd line top
-        i2clcd.printf("V: %1.3f", cal_vcc);
+        i2clcd.printf("V:%.3f", cal_vcc);       
+        myled0 = 1;
+        pc.printf( "\r\nCds:%.0fohm -> %.1flux, Vcc:%.3fV, Vol:%d\r\n",
+            r_cds, lux, cal_vcc, nor_vol );
+        myled0 = 0;
         wait(2.0);
     //  ---------- Barometer Sensor / BMP180 --------------------------------------------------
         bmp180.normalize();
+        cls();
         i2clcd.locate(0, 0);    // 1st line top
-        i2clcd.printf("P:%4.1f", bmp180.read_pressure());
+        i2clcd.printf("P:%.1f", bmp180.read_pressure());
         i2clcd.locate(0, 1);    // 2nd line top
-        i2clcd.printf("T: %\+-0.1f", bmp180.read_temperature());
+        i2clcd.printf("T:%\+-6.1f", bmp180.read_temperature());
         myled1 = 1;
-        pc.printf("Pres:%4.1fhPa, Temp:%\+-0.1fdegC\r\n",
-                    bmp180.read_pressure(), bmp180.read_temperature());
+        pc.printf("Pres:%4.1fhPa, Temp:%\+-0.1fdegC\r\n", bmp180.read_pressure(), bmp180.read_temperature());
         myled1 = 0;
         wait(4.0);
     //  ---------- Humidity Sensor / RHT03 ----------------------------------------------------
         hum_RHT03_read();       // Read Humidity data then avaraging
+        cls();
         i2clcd.locate(0, 0);    // 1st line top
-        i2clcd.printf("H:  %2.1f", humidity);
+        i2clcd.printf("H:%.1f", humidity);
         i2clcd.locate(0, 1);    // 2nd line top
-        i2clcd.printf("T: %\+-0.1f", humidity_temp);
+        i2clcd.printf("T:%\+-6.1f", humidity_temp);
         myled1 = 1;
-        pc.printf("Humid: %0.1f%cRH, Temp:%\+-0.1fdegC\r\n", humidity, '%', humidity_temp);        
-        myled1 = 0;  
+        pc.printf("Humid: %0.1f%%RH, Temp:%\+-0.1fdegC\r\n", humidity, humidity_temp);          
+        myled1 = 0; 
         wait(2.0);
+    //  ---------- Check EEPROM ----------------------------------------------------
+        cls();
+        i2clcd.locate(0, 0);    // 1st line top
+        //             12345678
+        i2clcd.printf(" EEPROM ", humidity);
+        pc.printf("EEPROM -> ", humidity, humidity_temp);
+        i2clcd.locate(0, 1);    // 2nd line top  
+        if (no == 4) {  // check page write mode (part1)
+            ++no;
+            strcpy((char *)eep_buf,(char *)eep_char);   // copy the data         
+            status = at24c1024.write_page(0x100, eep_buf, sizeof(eep_buf)); // 0x100= page address
+            pc.printf("(Status: %d) ", status);
+            if (status == AT24C_OK){    // wrote without trouble
+                wait(1.0);
+                for (int i=0; i < 256; i++){    // clear buffer for read data checking
+                    eep_buf[i] = 0;
+                }
+                status = at24c1024.read_page(0x100, eep_buf, sizeof(eep_buf));
+                pc.printf("(Status: %d) ", status);
+                if (status == AT24C_OK){    // read w/o trouble
+                    pc.printf("\r\n%s\r\n", eep_buf);
+                    if (strncmp((char *)eep_buf, (char *)eep_char, 256) == 0){ // equal R/W data
+                        i2clcd.printf("page-OK");
+                        pc.printf("Page access OK\r\n");
+                    } else {    // Read != Wrote
+                        i2clcd.printf("CMP-NG");
+                        pc.printf("Page access Comp NG\r\n");
+                    }
+                } else {    // read w/ trouble
+                    i2clcd.printf("RD-NG");
+                    pc.printf("Page access read NG\r\n");
+                }
+            } else {        // write w/ trouble
+                i2clcd.printf("WR -NG");
+                pc.printf("Write NG\r\n");
+            }
+            wait(0.5);       
+        } else if (no == 5) {  // check page write mode (part2)
+            no = 0;
+            for (int i=0; i < 256; i++){    // set writing data 255,254,,,,,3,2,1,0
+                eep_buf[i] = 255 - (uint8_t)i;
+            }        
+            status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
+            pc.printf("(Status: %d) ", status);
+            if (status == AT24C_OK){
+                wait(1.0);
+                for (int i=0; i < 256; i++){    // clear buffer
+                    eep_buf[i] = 0;
+                }
+                status = at24c1024.read_page(0x1ff00, eep_buf, sizeof(eep_buf));
+                pc.printf("(Status: %d) ", status);
+                if (status == AT24C_OK){
+                    pc.printf("\r\n0:%d, 64:%d, 128:%d, 254:%d, 255:%d\r\n",
+                            eep_buf[0], eep_buf[64], eep_buf[128], eep_buf[254], eep_buf[255]);
+                    if ((eep_buf[0] == 255) && (eep_buf[64] == 255 - 64)
+                            && (eep_buf[128] == 255-128) && (eep_buf[255] == 0)){
+                        i2clcd.printf("page-OK");
+                        pc.printf("Page access OK\r\n");
+                    } else {
+                        i2clcd.printf("CMP-NG");
+                        pc.printf("Page access Comp NG\r\n");
+                    }
+                } else {
+                    i2clcd.printf("RD-NG");
+                    pc.printf("Page access read NG\r\n");
+                }
+            } else {
+                i2clcd.printf("WR -NG");
+                pc.printf("Write NG\r\n");
+            }
+            wait(0.5);
+        } else {    // Write and read (single byte)
+            if (no == 0){   // Initial setting
+                // Check EEPROM (step0)
+                at24c1024.write(0,no);
+                wait(0.1);
+                at24c1024.write(1,0);
+                wait(0.1);
+                at24c1024.write(2,0x55);
+                wait(0.1);
+                at24c1024.write(3,0xaa);
+                wait(0.1);
+                dt[0] = 0xff;
+                dt[1] = 1;
+                dt[2] = 2;
+                dt[3] = 3;
+            }
+            // read 4 bytes
+            dt[0] = at24c1024.read(0);
+            dt[1] = at24c1024.read(1);
+            dt[2] = at24c1024.read(2);
+            dt[3] = at24c1024.read(3);     
+            if (dt[0] != no){
+                i2clcd.locate(0, 1);    // 2nd line top
+                i2clcd.printf("NG", humidity_temp);
+                pc.printf("NG\r\n", humidity, humidity_temp);
+            } else {           
+                if ((dt[1] == 0) && (dt[2] == 0x55) && (dt[3] == 0xaa)){
+                    i2clcd.printf("-> OK");
+                    pc.printf("OK\r\n");        
+                } else {
+                    i2clcd.printf("-> NG ");
+                    pc.printf("NG\r\n");
+                }
+            }
+            pc.printf("EEPROM 0x%x,0x%x,0x%x,0x%x\r\n", dt[0],dt[1],dt[2],dt[3]);
+            at24c1024.write(0,++no);    // write sequence number into EEPROM
+            wait(1.0);
+        }    
     }
 }