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:
11:bccfd75e84a0
Parent:
10:398f62bb41f7
Child:
12:1e21119688fe
--- 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     
     }
 }