dat logger with S25FL128P

You are viewing an older revision! See the latest version

Homepage

#include "mbed.h"
//#include "FRAMSPI.h"
#include "ADXL345_I2C.h"
#include "MAX44009.h"
#include "SHT21_ncleee.h"
#include "ms5611.h"
#include "S25FL128P.h"
 
 
 //SPI spi(D11,D12,D13);
 //FRAMSPI fram(spi,D10);
 S25FL128P s25fl(D11,D12,D13,D10);
 ADXL345_I2C accelerometer(D14,D15);
 I2C i2c(D14,D15);
 MAX44009 max44009(&i2c, MAX_ADDR);
 ms5611 ms(D14, D15, ms5611::CSBpin_1);
 SHT21 sht(&i2c);
 Serial pc(USBTX, USBRX);
 
 char buffer[32];
// stores 1 of 3 different types of data
// data is grouped to minimise wasted space.
union dataUnion {   
    int16_t acceleration[3];
    double pressure;
    float tempHumidityLux[3]; 
};

// values used to indicate which type of data is being stored
const char AccelData = 1; 
const char PressureData = 2;
const char TempData = 3;

// structure used to store data in FRAM
typedef struct {
    char dataType;              // what type of data is in this structure
    time_t dataTime;            // when it was recorded
    union dataUnion dataValue;  // the actual values used.
} dataStore;


//const int FRAMSize = 8192; // size of FRAM in bytes
const int S25FLSize = 16384000; // size of S25FL128P

//const int framBlockSize = sizeof(dataStore); // size needed to store a data structure
const int s25flBlockSize = sizeof(dataStore); // size needed to store a data structure

 
 int main() {
     pc.baud(115200);
     ms.cmd_reset();

     s25fl.eraseChip(); // erase the whole chip


     set_time(1387198323); // Set RTC time to 16 December 2013 10:05:23 UTC
     
      int readings[3] = {0, 0, 0};
     
     //pc.printf("Starting ADXL345 test...\n");
     //wait(.001);
     //pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
    //wait(.001);
    
     // These are here to test whether any of the initialization fails. It will print the failure
    if (accelerometer.setPowerControl(0x00)){
         pc.printf("didn't intitialize power control\n"); 
         return 0;  }
     //Full resolution, +/-16g, 4mg/LSB.
     //wait(.001);
     
     if(accelerometer.setDataFormatControl(0x0B)){
        pc.printf("didn't set data format\n");
        return 0;  }
     //wait(.001);
     
     //3.2kHz data rate.
     if(accelerometer.setDataRate(ADXL345_3200HZ)){
        pc.printf("didn't set data rate\n");
        return 0;    }
     //wait(.001);
     
     //Measurement mode.
     
     if(accelerometer.setPowerControl(MeasurementMode)) {
        pc.printf("didn't set the power control to measurement\n"); 
        return 0;   } 
        
     int recordNumber = 0;

    dataStore dataToWrite;

    time_t lastUpdate = 0;
    time_t seconds;   
     
     while(1) { 
     time_t seconds = time(NULL);
     strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
     accelerometer.getOutput(readings);
     
        dataToWrite.dataTime = seconds;
        dataToWrite.dataValue.acceleration[0] = readings[0];
        dataToWrite.dataValue.acceleration[1] = readings[1];
        dataToWrite.dataValue.acceleration[2] = readings[2];
        dataToWrite.dataType = AccelData;

        //fram.write(recordNumber*framBlockSize, (char*)(&dataToWrite), framBlockSize);
        s25fl.write(recordNumber*s25flBlockSize, (char*)(&dataToWrite), s25flBlockSize);

        recordNumber++;
        if ((recordNumber*s25flBlockSize) >= (S25FLSize - s25flBlockSize)) {
            recordNumber--;
            pc.printf("\r\n S25FL FULL");
            break;
        }

      if ((int)seconds != (int)lastUpdate) {
            dataToWrite.dataValue.pressure = ms.calcPressure();
            dataToWrite.dataType = PressureData;
            s25fl.write(recordNumber*s25flBlockSize, (char*)(&dataToWrite), s25flBlockSize);

            recordNumber++;
            if ((recordNumber*s25flBlockSize) >= (S25FLSize - s25flBlockSize)) {
                recordNumber--;
                pc.printf("\r\n S25FL FULL");
                break;
            }

            dataToWrite.dataValue.tempHumidityLux[0] = sht.readTemp();
            dataToWrite.dataValue.tempHumidityLux[1] = sht.readHumidity();
            dataToWrite.dataValue.tempHumidityLux[2] = max44009.getLUXReading();
            dataToWrite.dataType = TempData;
            s25fl.write(recordNumber*s25flBlockSize, (char*)(&dataToWrite), s25flBlockSize);
            recordNumber++;
            if ((recordNumber*s25flBlockSize) >= (S25FLSize - s25flBlockSize)) {
                recordNumber--;
                pc.printf("\r\n S25FL FULL");
                break;
            }


            lastUpdate = seconds;
        }
    } // end write loop
    
       dataStore dataFromS25FL;


// code to read data back
    int readRecord = 0;
    //time_t seconds = time(NULL);
    char buffer[32];
    while (readRecord < recordNumber) {

        s25fl.read(readRecord*s25flBlockSize, (char*)(&dataFromS25FL), s25flBlockSize);
        //printf("                Time = %I:%M:%S %p", localtime(&(dataFromFRAM.dataTime)));
        strftime(buffer, 32, "%I:%M:%S %p", localtime(&(dataFromS25FL.dataTime)));
        printf("                Time = %s", buffer);
        switch (dataFromS25FL.dataType) {
            case AccelData:
                printf("\r\n Accelerometer: X: %ig, Y: %ig, Z: %ig", dataFromS25FL.dataValue.acceleration[0], dataFromS25FL.dataValue.acceleration[1],dataFromS25FL.dataValue.acceleration[2]);
                break;
            case PressureData:
                printf("\r\n Barometer pressure: %.1f hpa", dataFromS25FL.dataValue.pressure);
                break;
            case TempData:
                printf("\r\n Temperature is: [%3.2f C ]", dataFromS25FL.dataValue.tempHumidityLux[0]);
                printf("\r\n Humidity is: [ %3.2f %% ]", dataFromS25FL.dataValue.tempHumidityLux[1]);
                printf("\r\n LUX: %f \r\n\r\n ",dataFromS25FL.dataValue.tempHumidityLux[2]);
                break;
        }
        readRecord++;
    }
}

All wikipages