![](/media/cache/profiles/f0fcf351df4eb6786e9bb6fc4e2dee02.jpg.50x50_q85.jpg)
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
Please see https://mbed.org/users/kenjiArai/notebook/mbed-lpc1114fn28-barometer-with-data-logging/#
main.cpp@12:1e21119688fe, 2014-06-20 (annotated)
- Committer:
- kenjiArai
- Date:
- Fri Jun 20 21:45:12 2014 +0000
- Revision:
- 12:1e21119688fe
- Parent:
- 11:bccfd75e84a0
- Child:
- 13:950adc9d6d61
Barometer program / Step by step approach -> 6th step,; Added EEPROM(AT24C1024) control for data logging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 7:d3b49f1d9f76 | 1 | /* |
kenjiArai | 7:d3b49f1d9f76 | 2 | * mbed Application program for the mbed LPC1114FN28 |
kenjiArai | 12:1e21119688fe | 3 | * Test program -> Check EEPROM |
kenjiArai | 7:d3b49f1d9f76 | 4 | * |
kenjiArai | 7:d3b49f1d9f76 | 5 | * Copyright (c) 2014 Kenji Arai / JH1PJL |
kenjiArai | 7:d3b49f1d9f76 | 6 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 7:d3b49f1d9f76 | 7 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 7:d3b49f1d9f76 | 8 | * Created: June 13th, 2014 |
kenjiArai | 12:1e21119688fe | 9 | * Revised: June 21st, 2014 |
kenjiArai | 7:d3b49f1d9f76 | 10 | * |
kenjiArai | 7:d3b49f1d9f76 | 11 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 7:d3b49f1d9f76 | 12 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 7:d3b49f1d9f76 | 13 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 7:d3b49f1d9f76 | 14 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 7:d3b49f1d9f76 | 15 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 7:d3b49f1d9f76 | 16 | */ |
dan | 0:7dec7e9ac085 | 17 | |
kenjiArai | 11:bccfd75e84a0 | 18 | #include "mbed.h" |
kenjiArai | 12:1e21119688fe | 19 | #include "BMP180.h" // Own lib. / Pressure sensor |
kenjiArai | 12:1e21119688fe | 20 | #include "RHT03.h" // Std. lib./ Humidity sensor |
kenjiArai | 12:1e21119688fe | 21 | #include "TextLCD.h" // Std. lib./ LCD control |
kenjiArai | 12:1e21119688fe | 22 | #include "AT24C1024.h" // Std. lib./ EEPROM control |
kenjiArai | 8:f6aa5561f219 | 23 | |
kenjiArai | 11:bccfd75e84a0 | 24 | #define VREF_VOLT 2.482 // TA76431F Vref real measued data |
kenjiArai | 11:bccfd75e84a0 | 25 | #define R_FIX 9930 // 10K ohm <- real measued data |
kenjiArai | 12:1e21119688fe | 26 | #define VOL_OFFSET 3 // Offset data ,= real measured data |
kenjiArai | 8:f6aa5561f219 | 27 | #define CDS_TBL_SIZE 13 |
kenjiArai | 7:d3b49f1d9f76 | 28 | |
kenjiArai | 11:bccfd75e84a0 | 29 | I2C i2c(dp5,dp27); // SDA, SCL |
kenjiArai | 7:d3b49f1d9f76 | 30 | DigitalOut myled0(dp28); // LED for Debug |
kenjiArai | 7:d3b49f1d9f76 | 31 | DigitalOut myled1(dp14); // Indicate state transition |
kenjiArai | 8:f6aa5561f219 | 32 | DigitalOut analog_pwr(dp6); // VCC for analog interface (vol, cds and vref) |
kenjiArai | 8:f6aa5561f219 | 33 | DigitalOut vref_pwr(dp4); // VCC for Vref |
kenjiArai | 7:d3b49f1d9f76 | 34 | DigitalIn sw_chng(dp1,PullUp);// SW for select |
kenjiArai | 7:d3b49f1d9f76 | 35 | DigitalIn sw_mode(dp2,PullUp);// SW for Mode change |
kenjiArai | 8:f6aa5561f219 | 36 | AnalogIn cds(dp11); // Input / CDS data |
kenjiArai | 8:f6aa5561f219 | 37 | AnalogIn vref(dp9); // Input / Bandgap 2.5V |
kenjiArai | 8:f6aa5561f219 | 38 | AnalogIn vol(dp10); // Input / contrast volume |
kenjiArai | 10:398f62bb41f7 | 39 | RHT03 humtemp(dp26); // RHT03 interface |
kenjiArai | 11:bccfd75e84a0 | 40 | Serial pc(dp16,dp15); // UART (vertual COM) |
kenjiArai | 11:bccfd75e84a0 | 41 | BMP180 bmp180(i2c); // Bosch sensor |
kenjiArai | 12:1e21119688fe | 42 | TextLCD_I2C_N i2clcd(&i2c, 0x7c, TextLCD::LCD8x2); // LCD(Akizuki AQM0802A) |
kenjiArai | 12:1e21119688fe | 43 | AT24C1024 at24c1024(i2c); // Atmel 1Mbit EE-PROM |
kenjiArai | 8:f6aa5561f219 | 44 | |
kenjiArai | 8:f6aa5561f219 | 45 | typedef enum {CDS = 0, VREF, VOL} ADC_Select; |
kenjiArai | 8:f6aa5561f219 | 46 | |
kenjiArai | 9:ce80da60884a | 47 | // ADC |
kenjiArai | 8:f6aa5561f219 | 48 | float av_cds, av_vref, av_vol, cal_vcc; |
kenjiArai | 8:f6aa5561f219 | 49 | float r_cds, lux; |
kenjiArai | 8:f6aa5561f219 | 50 | uint32_t nor_vol; |
kenjiArai | 8:f6aa5561f219 | 51 | |
kenjiArai | 10:398f62bb41f7 | 52 | // Humidity Sensor |
kenjiArai | 10:398f62bb41f7 | 53 | float humidity_temp, humidity; |
kenjiArai | 10:398f62bb41f7 | 54 | |
kenjiArai | 12:1e21119688fe | 55 | // EEPROM |
kenjiArai | 12:1e21119688fe | 56 | uint8_t eep_buf[256 + 2]; |
kenjiArai | 12:1e21119688fe | 57 | |
kenjiArai | 8:f6aa5561f219 | 58 | // Cds GL5528 (Dark Resistance 1 Mohm type) SENBA OPTICAL & ELECTRONIC CO.,LTD. |
kenjiArai | 8:f6aa5561f219 | 59 | // Table value referrence: http://homepage3.nifty.com/skomo/f35/hp35_20.htm |
kenjiArai | 8:f6aa5561f219 | 60 | const float lux_cds[CDS_TBL_SIZE][2] = |
kenjiArai | 8:f6aa5561f219 | 61 | {{50,21194},{100,8356},{200,3294},{400,1299},{800,512},{1600,202},{3200,79.6},{6400,31.4}, |
kenjiArai | 8:f6aa5561f219 | 62 | {12800,12.4},{25600,4.88},{51200,1.92},{102400,0.758},{409600,0.118}}; |
kenjiArai | 8:f6aa5561f219 | 63 | |
kenjiArai | 12:1e21119688fe | 64 | // EEPROM test charcters |
kenjiArai | 12:1e21119688fe | 65 | const uint8_t eep_char[256] = |
kenjiArai | 12:1e21119688fe | 66 | //0 1 2 3 4 5 6 7 |
kenjiArai | 12:1e21119688fe | 67 | //12345678901234567890123456789012345678901234567890123456789012345678901 |
kenjiArai | 12:1e21119688fe | 68 | "The AT24C1024 provides 1048576 bits of serial electrically erasable and" |
kenjiArai | 12:1e21119688fe | 69 | //7 8 9 10 11 12 13 14 15 |
kenjiArai | 12:1e21119688fe | 70 | //2345678901234567890123456789012345678901234567890123456789012345678901234567890123 |
kenjiArai | 12:1e21119688fe | 71 | " programmable read only memory (EEPROM) organized as 131,072 words of 8 bits each." |
kenjiArai | 12:1e21119688fe | 72 | //15 16 17 18 19 20 21 22 23 24 |
kenjiArai | 12:1e21119688fe | 73 | //45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012 |
kenjiArai | 12:1e21119688fe | 74 | " The devices cascadable feature allows up to four devices to share a common two-wire bus." |
kenjiArai | 12:1e21119688fe | 75 | //24 25 256 |
kenjiArai | 12:1e21119688fe | 76 | //34567890123456 |
kenjiArai | 12:1e21119688fe | 77 | " JH1PJL Arai"; // eep_char[255] = 0; |
kenjiArai | 12:1e21119688fe | 78 | |
kenjiArai | 9:ce80da60884a | 79 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 9:ce80da60884a | 80 | // Control Program |
kenjiArai | 9:ce80da60884a | 81 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 11:bccfd75e84a0 | 82 | // Normalize ADC data |
kenjiArai | 8:f6aa5561f219 | 83 | void adc_normalize( ADC_Select n ){ |
kenjiArai | 8:f6aa5561f219 | 84 | int i; |
kenjiArai | 8:f6aa5561f219 | 85 | float x1,y1,dx; |
kenjiArai | 8:f6aa5561f219 | 86 | |
kenjiArai | 8:f6aa5561f219 | 87 | switch (n){ |
kenjiArai | 8:f6aa5561f219 | 88 | case CDS: |
kenjiArai | 11:bccfd75e84a0 | 89 | // v_adc = Rfix / (Rcds + Rfix) -> Rcds = ( Rfix / v_adc ) - Rfix |
kenjiArai | 8:f6aa5561f219 | 90 | r_cds = (R_FIX / av_cds) - R_FIX; |
kenjiArai | 8:f6aa5561f219 | 91 | // CDS resistance to Lux conversion using convertion table (luc_cds[][]) |
kenjiArai | 11:bccfd75e84a0 | 92 | for (i =0; i < CDS_TBL_SIZE; i++){ // search table |
kenjiArai | 11:bccfd75e84a0 | 93 | if ( r_cds <= lux_cds[i][0]){ break; } |
kenjiArai | 11:bccfd75e84a0 | 94 | } |
kenjiArai | 11:bccfd75e84a0 | 95 | // Check table position |
kenjiArai | 11:bccfd75e84a0 | 96 | if (i == 0){ |
kenjiArai | 11:bccfd75e84a0 | 97 | lux = lux_cds[0][1]; |
kenjiArai | 11:bccfd75e84a0 | 98 | break; |
kenjiArai | 11:bccfd75e84a0 | 99 | } else if ( i == CDS_TBL_SIZE ){ |
kenjiArai | 11:bccfd75e84a0 | 100 | if ( r_cds <= lux_cds[i][0] ){ |
kenjiArai | 11:bccfd75e84a0 | 101 | lux = lux_cds[i-1][1]; |
kenjiArai | 8:f6aa5561f219 | 102 | break; |
kenjiArai | 8:f6aa5561f219 | 103 | } |
kenjiArai | 8:f6aa5561f219 | 104 | } |
kenjiArai | 11:bccfd75e84a0 | 105 | // Linear interpolation |
kenjiArai | 11:bccfd75e84a0 | 106 | y1 = lux_cds[i-1][1] - lux_cds[i][1]; |
kenjiArai | 11:bccfd75e84a0 | 107 | x1 = lux_cds[i][0] - lux_cds[i-1][0]; |
kenjiArai | 11:bccfd75e84a0 | 108 | dx = r_cds - lux_cds[i-1][0]; |
kenjiArai | 11:bccfd75e84a0 | 109 | lux = lux_cds[i-1][1] - ((dx/x1) * y1); |
kenjiArai | 8:f6aa5561f219 | 110 | break; |
kenjiArai | 8:f6aa5561f219 | 111 | case VREF: |
kenjiArai | 8:f6aa5561f219 | 112 | // vref = VREF_VOLT / VCC -> VCC = VREF_VOLT / vref |
kenjiArai | 8:f6aa5561f219 | 113 | cal_vcc = VREF_VOLT / vref; |
kenjiArai | 8:f6aa5561f219 | 114 | break; |
kenjiArai | 8:f6aa5561f219 | 115 | case VOL: |
kenjiArai | 8:f6aa5561f219 | 116 | // Vol center = 1.00 (actual 100) |
kenjiArai | 8:f6aa5561f219 | 117 | nor_vol = (uint32_t)(av_vol * 200) + VOL_OFFSET; |
kenjiArai | 8:f6aa5561f219 | 118 | break; |
kenjiArai | 8:f6aa5561f219 | 119 | } |
kenjiArai | 8:f6aa5561f219 | 120 | } |
kenjiArai | 8:f6aa5561f219 | 121 | |
kenjiArai | 11:bccfd75e84a0 | 122 | // Read adc data and averaging |
kenjiArai | 8:f6aa5561f219 | 123 | void adc_all_read( void){ |
kenjiArai | 11:bccfd75e84a0 | 124 | if (av_cds == 0){ av_cds = cds.read(); |
kenjiArai | 11:bccfd75e84a0 | 125 | } else { av_cds = av_cds *0.5 + cds.read() * 0.5; |
kenjiArai | 10:398f62bb41f7 | 126 | } |
kenjiArai | 11:bccfd75e84a0 | 127 | if (av_vref == 0){ av_vref = vref.read(); |
kenjiArai | 11:bccfd75e84a0 | 128 | } else { av_vref = av_vref *0.9 + vref.read() * 0.1; |
kenjiArai | 10:398f62bb41f7 | 129 | } |
kenjiArai | 11:bccfd75e84a0 | 130 | if (av_vol == 0){ av_vol = vol.read(); |
kenjiArai | 11:bccfd75e84a0 | 131 | } else { av_vol = av_vol *0.2 + vol.read() * 0.8; |
kenjiArai | 10:398f62bb41f7 | 132 | } |
kenjiArai | 8:f6aa5561f219 | 133 | } |
kenjiArai | 8:f6aa5561f219 | 134 | |
kenjiArai | 11:bccfd75e84a0 | 135 | // Read Humidity sensor data |
kenjiArai | 10:398f62bb41f7 | 136 | void hum_RHT03_read( void){ |
kenjiArai | 11:bccfd75e84a0 | 137 | while (true){ // wait data |
kenjiArai | 11:bccfd75e84a0 | 138 | if ( humtemp.readData() == RHT_ERROR_NONE ){ break; } |
kenjiArai | 10:398f62bb41f7 | 139 | } |
kenjiArai | 11:bccfd75e84a0 | 140 | if (humidity_temp == 0){humidity_temp = humtemp.getTemperatureC(); |
kenjiArai | 11:bccfd75e84a0 | 141 | } else { humidity_temp = humidity_temp * 0.9 + humtemp.getTemperatureC() * 0.1; |
kenjiArai | 10:398f62bb41f7 | 142 | } |
kenjiArai | 11:bccfd75e84a0 | 143 | if ( humidity == 0 ){ humidity = humtemp.getHumidity(); |
kenjiArai | 11:bccfd75e84a0 | 144 | } else { humidity = humidity * 0.9 + humtemp.getHumidity() * 0.1; |
kenjiArai | 10:398f62bb41f7 | 145 | } |
kenjiArai | 8:f6aa5561f219 | 146 | } |
dan | 0:7dec7e9ac085 | 147 | |
kenjiArai | 12:1e21119688fe | 148 | // Clear LCD |
kenjiArai | 12:1e21119688fe | 149 | void cls( void){ |
kenjiArai | 12:1e21119688fe | 150 | i2clcd.locate(0, 0); |
kenjiArai | 12:1e21119688fe | 151 | i2clcd.printf(" "); |
kenjiArai | 12:1e21119688fe | 152 | i2clcd.locate(0, 1); |
kenjiArai | 12:1e21119688fe | 153 | i2clcd.printf(" "); |
kenjiArai | 12:1e21119688fe | 154 | } |
kenjiArai | 12:1e21119688fe | 155 | |
kenjiArai | 11:bccfd75e84a0 | 156 | //------------------------------------- |
kenjiArai | 11:bccfd75e84a0 | 157 | // Application program starts here |
kenjiArai | 11:bccfd75e84a0 | 158 | //------------------------------------- |
dan | 0:7dec7e9ac085 | 159 | int main() { |
kenjiArai | 12:1e21119688fe | 160 | int no =0; |
kenjiArai | 12:1e21119688fe | 161 | uint8_t dt[4]; |
kenjiArai | 12:1e21119688fe | 162 | AT24C_STATUS status; |
kenjiArai | 12:1e21119688fe | 163 | |
kenjiArai | 8:f6aa5561f219 | 164 | pc.baud(9600); |
kenjiArai | 12:1e21119688fe | 165 | pc.printf("\r\nmbed LPC1114FN28 test program by JH1PJL created on "__DATE__"(UTC)\r\n"); |
kenjiArai | 11:bccfd75e84a0 | 166 | i2clcd.setContrast(25); |
kenjiArai | 11:bccfd75e84a0 | 167 | i2clcd.locate(0, 0); |
kenjiArai | 11:bccfd75e84a0 | 168 | i2clcd.printf("LPC1114F"); |
kenjiArai | 11:bccfd75e84a0 | 169 | i2clcd.locate(0, 1); |
kenjiArai | 11:bccfd75e84a0 | 170 | i2clcd.printf(" JH1PJL "); |
kenjiArai | 10:398f62bb41f7 | 171 | // Initialize data |
kenjiArai | 10:398f62bb41f7 | 172 | av_cds = 0; |
kenjiArai | 10:398f62bb41f7 | 173 | av_vref = 0; |
kenjiArai | 10:398f62bb41f7 | 174 | av_vol = 0; |
kenjiArai | 10:398f62bb41f7 | 175 | humidity_temp = 0; |
kenjiArai | 10:398f62bb41f7 | 176 | humidity = 0; |
kenjiArai | 11:bccfd75e84a0 | 177 | // Show initial screen |
kenjiArai | 12:1e21119688fe | 178 | wait(5.0); |
kenjiArai | 7:d3b49f1d9f76 | 179 | while(1) { |
kenjiArai | 11:bccfd75e84a0 | 180 | // ---------- Cds Sensor, Vref, Volume --------------------------------------------------- |
kenjiArai | 9:ce80da60884a | 181 | // Power on / Analog sensor |
kenjiArai | 8:f6aa5561f219 | 182 | analog_pwr = 1; |
kenjiArai | 8:f6aa5561f219 | 183 | vref_pwr = 1; |
kenjiArai | 8:f6aa5561f219 | 184 | wait(0.2); |
kenjiArai | 8:f6aa5561f219 | 185 | adc_all_read(); |
kenjiArai | 9:ce80da60884a | 186 | // Power off / Analog sensor |
kenjiArai | 11:bccfd75e84a0 | 187 | analog_pwr = 0; |
kenjiArai | 9:ce80da60884a | 188 | // Normalize |
kenjiArai | 11:bccfd75e84a0 | 189 | adc_normalize(CDS); |
kenjiArai | 11:bccfd75e84a0 | 190 | adc_normalize(VREF); |
kenjiArai | 12:1e21119688fe | 191 | adc_normalize(VOL); |
kenjiArai | 12:1e21119688fe | 192 | cls(); |
kenjiArai | 11:bccfd75e84a0 | 193 | i2clcd.locate(0, 0); // 1st line top |
kenjiArai | 11:bccfd75e84a0 | 194 | // 12345678 |
kenjiArai | 12:1e21119688fe | 195 | i2clcd.printf("L:%.1f", lux); |
kenjiArai | 11:bccfd75e84a0 | 196 | i2clcd.locate(0, 1); // 2nd line top |
kenjiArai | 12:1e21119688fe | 197 | i2clcd.printf("V:%.3f", cal_vcc); |
kenjiArai | 12:1e21119688fe | 198 | myled0 = 1; |
kenjiArai | 12:1e21119688fe | 199 | pc.printf( "\r\nCds:%.0fohm -> %.1flux, Vcc:%.3fV, Vol:%d\r\n", |
kenjiArai | 12:1e21119688fe | 200 | r_cds, lux, cal_vcc, nor_vol ); |
kenjiArai | 12:1e21119688fe | 201 | myled0 = 0; |
kenjiArai | 10:398f62bb41f7 | 202 | wait(2.0); |
kenjiArai | 11:bccfd75e84a0 | 203 | // ---------- Barometer Sensor / BMP180 -------------------------------------------------- |
kenjiArai | 11:bccfd75e84a0 | 204 | bmp180.normalize(); |
kenjiArai | 12:1e21119688fe | 205 | cls(); |
kenjiArai | 11:bccfd75e84a0 | 206 | i2clcd.locate(0, 0); // 1st line top |
kenjiArai | 12:1e21119688fe | 207 | i2clcd.printf("P:%.1f", bmp180.read_pressure()); |
kenjiArai | 11:bccfd75e84a0 | 208 | i2clcd.locate(0, 1); // 2nd line top |
kenjiArai | 12:1e21119688fe | 209 | i2clcd.printf("T:%\+-6.1f", bmp180.read_temperature()); |
kenjiArai | 9:ce80da60884a | 210 | myled1 = 1; |
kenjiArai | 12:1e21119688fe | 211 | pc.printf("Pres:%4.1fhPa, Temp:%\+-0.1fdegC\r\n", bmp180.read_pressure(), bmp180.read_temperature()); |
kenjiArai | 8:f6aa5561f219 | 212 | myled1 = 0; |
kenjiArai | 10:398f62bb41f7 | 213 | wait(4.0); |
kenjiArai | 11:bccfd75e84a0 | 214 | // ---------- Humidity Sensor / RHT03 ---------------------------------------------------- |
kenjiArai | 10:398f62bb41f7 | 215 | hum_RHT03_read(); // Read Humidity data then avaraging |
kenjiArai | 12:1e21119688fe | 216 | cls(); |
kenjiArai | 11:bccfd75e84a0 | 217 | i2clcd.locate(0, 0); // 1st line top |
kenjiArai | 12:1e21119688fe | 218 | i2clcd.printf("H:%.1f", humidity); |
kenjiArai | 11:bccfd75e84a0 | 219 | i2clcd.locate(0, 1); // 2nd line top |
kenjiArai | 12:1e21119688fe | 220 | i2clcd.printf("T:%\+-6.1f", humidity_temp); |
kenjiArai | 10:398f62bb41f7 | 221 | myled1 = 1; |
kenjiArai | 12:1e21119688fe | 222 | pc.printf("Humid: %0.1f%%RH, Temp:%\+-0.1fdegC\r\n", humidity, humidity_temp); |
kenjiArai | 12:1e21119688fe | 223 | myled1 = 0; |
kenjiArai | 10:398f62bb41f7 | 224 | wait(2.0); |
kenjiArai | 12:1e21119688fe | 225 | // ---------- Check EEPROM ---------------------------------------------------- |
kenjiArai | 12:1e21119688fe | 226 | cls(); |
kenjiArai | 12:1e21119688fe | 227 | i2clcd.locate(0, 0); // 1st line top |
kenjiArai | 12:1e21119688fe | 228 | // 12345678 |
kenjiArai | 12:1e21119688fe | 229 | i2clcd.printf(" EEPROM ", humidity); |
kenjiArai | 12:1e21119688fe | 230 | pc.printf("EEPROM -> ", humidity, humidity_temp); |
kenjiArai | 12:1e21119688fe | 231 | i2clcd.locate(0, 1); // 2nd line top |
kenjiArai | 12:1e21119688fe | 232 | if (no == 4) { // check page write mode (part1) |
kenjiArai | 12:1e21119688fe | 233 | ++no; |
kenjiArai | 12:1e21119688fe | 234 | strcpy((char *)eep_buf,(char *)eep_char); // copy the data |
kenjiArai | 12:1e21119688fe | 235 | status = at24c1024.write_page(0x100, eep_buf, sizeof(eep_buf)); // 0x100= page address |
kenjiArai | 12:1e21119688fe | 236 | pc.printf("(Status: %d) ", status); |
kenjiArai | 12:1e21119688fe | 237 | if (status == AT24C_OK){ // wrote without trouble |
kenjiArai | 12:1e21119688fe | 238 | wait(1.0); |
kenjiArai | 12:1e21119688fe | 239 | for (int i=0; i < 256; i++){ // clear buffer for read data checking |
kenjiArai | 12:1e21119688fe | 240 | eep_buf[i] = 0; |
kenjiArai | 12:1e21119688fe | 241 | } |
kenjiArai | 12:1e21119688fe | 242 | status = at24c1024.read_page(0x100, eep_buf, sizeof(eep_buf)); |
kenjiArai | 12:1e21119688fe | 243 | pc.printf("(Status: %d) ", status); |
kenjiArai | 12:1e21119688fe | 244 | if (status == AT24C_OK){ // read w/o trouble |
kenjiArai | 12:1e21119688fe | 245 | pc.printf("\r\n%s\r\n", eep_buf); |
kenjiArai | 12:1e21119688fe | 246 | if (strncmp((char *)eep_buf, (char *)eep_char, 256) == 0){ // equal R/W data |
kenjiArai | 12:1e21119688fe | 247 | i2clcd.printf("page-OK"); |
kenjiArai | 12:1e21119688fe | 248 | pc.printf("Page access OK\r\n"); |
kenjiArai | 12:1e21119688fe | 249 | } else { // Read != Wrote |
kenjiArai | 12:1e21119688fe | 250 | i2clcd.printf("CMP-NG"); |
kenjiArai | 12:1e21119688fe | 251 | pc.printf("Page access Comp NG\r\n"); |
kenjiArai | 12:1e21119688fe | 252 | } |
kenjiArai | 12:1e21119688fe | 253 | } else { // read w/ trouble |
kenjiArai | 12:1e21119688fe | 254 | i2clcd.printf("RD-NG"); |
kenjiArai | 12:1e21119688fe | 255 | pc.printf("Page access read NG\r\n"); |
kenjiArai | 12:1e21119688fe | 256 | } |
kenjiArai | 12:1e21119688fe | 257 | } else { // write w/ trouble |
kenjiArai | 12:1e21119688fe | 258 | i2clcd.printf("WR -NG"); |
kenjiArai | 12:1e21119688fe | 259 | pc.printf("Write NG\r\n"); |
kenjiArai | 12:1e21119688fe | 260 | } |
kenjiArai | 12:1e21119688fe | 261 | wait(0.5); |
kenjiArai | 12:1e21119688fe | 262 | } else if (no == 5) { // check page write mode (part2) |
kenjiArai | 12:1e21119688fe | 263 | no = 0; |
kenjiArai | 12:1e21119688fe | 264 | for (int i=0; i < 256; i++){ // set writing data 255,254,,,,,3,2,1,0 |
kenjiArai | 12:1e21119688fe | 265 | eep_buf[i] = 255 - (uint8_t)i; |
kenjiArai | 12:1e21119688fe | 266 | } |
kenjiArai | 12:1e21119688fe | 267 | status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf)); |
kenjiArai | 12:1e21119688fe | 268 | pc.printf("(Status: %d) ", status); |
kenjiArai | 12:1e21119688fe | 269 | if (status == AT24C_OK){ |
kenjiArai | 12:1e21119688fe | 270 | wait(1.0); |
kenjiArai | 12:1e21119688fe | 271 | for (int i=0; i < 256; i++){ // clear buffer |
kenjiArai | 12:1e21119688fe | 272 | eep_buf[i] = 0; |
kenjiArai | 12:1e21119688fe | 273 | } |
kenjiArai | 12:1e21119688fe | 274 | status = at24c1024.read_page(0x1ff00, eep_buf, sizeof(eep_buf)); |
kenjiArai | 12:1e21119688fe | 275 | pc.printf("(Status: %d) ", status); |
kenjiArai | 12:1e21119688fe | 276 | if (status == AT24C_OK){ |
kenjiArai | 12:1e21119688fe | 277 | pc.printf("\r\n0:%d, 64:%d, 128:%d, 254:%d, 255:%d\r\n", |
kenjiArai | 12:1e21119688fe | 278 | eep_buf[0], eep_buf[64], eep_buf[128], eep_buf[254], eep_buf[255]); |
kenjiArai | 12:1e21119688fe | 279 | if ((eep_buf[0] == 255) && (eep_buf[64] == 255 - 64) |
kenjiArai | 12:1e21119688fe | 280 | && (eep_buf[128] == 255-128) && (eep_buf[255] == 0)){ |
kenjiArai | 12:1e21119688fe | 281 | i2clcd.printf("page-OK"); |
kenjiArai | 12:1e21119688fe | 282 | pc.printf("Page access OK\r\n"); |
kenjiArai | 12:1e21119688fe | 283 | } else { |
kenjiArai | 12:1e21119688fe | 284 | i2clcd.printf("CMP-NG"); |
kenjiArai | 12:1e21119688fe | 285 | pc.printf("Page access Comp NG\r\n"); |
kenjiArai | 12:1e21119688fe | 286 | } |
kenjiArai | 12:1e21119688fe | 287 | } else { |
kenjiArai | 12:1e21119688fe | 288 | i2clcd.printf("RD-NG"); |
kenjiArai | 12:1e21119688fe | 289 | pc.printf("Page access read NG\r\n"); |
kenjiArai | 12:1e21119688fe | 290 | } |
kenjiArai | 12:1e21119688fe | 291 | } else { |
kenjiArai | 12:1e21119688fe | 292 | i2clcd.printf("WR -NG"); |
kenjiArai | 12:1e21119688fe | 293 | pc.printf("Write NG\r\n"); |
kenjiArai | 12:1e21119688fe | 294 | } |
kenjiArai | 12:1e21119688fe | 295 | wait(0.5); |
kenjiArai | 12:1e21119688fe | 296 | } else { // Write and read (single byte) |
kenjiArai | 12:1e21119688fe | 297 | if (no == 0){ // Initial setting |
kenjiArai | 12:1e21119688fe | 298 | // Check EEPROM (step0) |
kenjiArai | 12:1e21119688fe | 299 | at24c1024.write(0,no); |
kenjiArai | 12:1e21119688fe | 300 | wait(0.1); |
kenjiArai | 12:1e21119688fe | 301 | at24c1024.write(1,0); |
kenjiArai | 12:1e21119688fe | 302 | wait(0.1); |
kenjiArai | 12:1e21119688fe | 303 | at24c1024.write(2,0x55); |
kenjiArai | 12:1e21119688fe | 304 | wait(0.1); |
kenjiArai | 12:1e21119688fe | 305 | at24c1024.write(3,0xaa); |
kenjiArai | 12:1e21119688fe | 306 | wait(0.1); |
kenjiArai | 12:1e21119688fe | 307 | dt[0] = 0xff; |
kenjiArai | 12:1e21119688fe | 308 | dt[1] = 1; |
kenjiArai | 12:1e21119688fe | 309 | dt[2] = 2; |
kenjiArai | 12:1e21119688fe | 310 | dt[3] = 3; |
kenjiArai | 12:1e21119688fe | 311 | } |
kenjiArai | 12:1e21119688fe | 312 | // read 4 bytes |
kenjiArai | 12:1e21119688fe | 313 | dt[0] = at24c1024.read(0); |
kenjiArai | 12:1e21119688fe | 314 | dt[1] = at24c1024.read(1); |
kenjiArai | 12:1e21119688fe | 315 | dt[2] = at24c1024.read(2); |
kenjiArai | 12:1e21119688fe | 316 | dt[3] = at24c1024.read(3); |
kenjiArai | 12:1e21119688fe | 317 | if (dt[0] != no){ |
kenjiArai | 12:1e21119688fe | 318 | i2clcd.locate(0, 1); // 2nd line top |
kenjiArai | 12:1e21119688fe | 319 | i2clcd.printf("NG", humidity_temp); |
kenjiArai | 12:1e21119688fe | 320 | pc.printf("NG\r\n", humidity, humidity_temp); |
kenjiArai | 12:1e21119688fe | 321 | } else { |
kenjiArai | 12:1e21119688fe | 322 | if ((dt[1] == 0) && (dt[2] == 0x55) && (dt[3] == 0xaa)){ |
kenjiArai | 12:1e21119688fe | 323 | i2clcd.printf("-> OK"); |
kenjiArai | 12:1e21119688fe | 324 | pc.printf("OK\r\n"); |
kenjiArai | 12:1e21119688fe | 325 | } else { |
kenjiArai | 12:1e21119688fe | 326 | i2clcd.printf("-> NG "); |
kenjiArai | 12:1e21119688fe | 327 | pc.printf("NG\r\n"); |
kenjiArai | 12:1e21119688fe | 328 | } |
kenjiArai | 12:1e21119688fe | 329 | } |
kenjiArai | 12:1e21119688fe | 330 | pc.printf("EEPROM 0x%x,0x%x,0x%x,0x%x\r\n", dt[0],dt[1],dt[2],dt[3]); |
kenjiArai | 12:1e21119688fe | 331 | at24c1024.write(0,++no); // write sequence number into EEPROM |
kenjiArai | 12:1e21119688fe | 332 | wait(1.0); |
kenjiArai | 12:1e21119688fe | 333 | } |
kenjiArai | 7:d3b49f1d9f76 | 334 | } |
dan | 0:7dec7e9ac085 | 335 | } |