Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017
Dependencies: ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822
Fork of UPAS_BLE_and_USB by
main.cpp@7:a24d7156bc02, 2015-05-06 (annotated)
- Committer:
- caseyquinn
- Date:
- Wed May 06 04:08:03 2015 +0000
- Revision:
- 7:a24d7156bc02
- Parent:
- 6:2328feb48792
- Child:
- 8:204c21adf693
Added RTC and STC3100 Gas Gauge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caseyquinn | 0:14d46ef4b6cb | 1 | #include "mbed.h" |
caseyquinn | 0:14d46ef4b6cb | 2 | #include "SDFileSystem.h" |
caseyquinn | 0:14d46ef4b6cb | 3 | #include "Adafruit_ADS1015.h" |
caseyquinn | 1:37babeb68ab9 | 4 | #include "MCP40D17.h" |
caseyquinn | 5:0edf35b6d79a | 5 | #include "BMP280.h" |
caseyquinn | 7:a24d7156bc02 | 6 | #include "wire.h" |
caseyquinn | 7:a24d7156bc02 | 7 | #include "STC3100.h" |
caseyquinn | 1:37babeb68ab9 | 8 | |
caseyquinn | 1:37babeb68ab9 | 9 | #define SERIAL_BAUD_RATE 9600 |
caseyquinn | 7:a24d7156bc02 | 10 | #define SCL 20 |
caseyquinn | 7:a24d7156bc02 | 11 | #define SDA 22 |
caseyquinn | 7:a24d7156bc02 | 12 | #define Crono 0xD0 //D0 for the chronoDot |
caseyquinn | 0:14d46ef4b6cb | 13 | |
caseyquinn | 0:14d46ef4b6cb | 14 | I2C i2c(p22, p20); |
caseyquinn | 0:14d46ef4b6cb | 15 | Adafruit_ADS1115 ads(&i2c); |
caseyquinn | 1:37babeb68ab9 | 16 | MCP40D17 DigPot(&i2c); |
caseyquinn | 5:0edf35b6d79a | 17 | BMP280 bmpsensor(p22, p20); |
caseyquinn | 7:a24d7156bc02 | 18 | STC3100 gasG(p22, p20); |
caseyquinn | 2:e596e685eb39 | 19 | Serial pc(USBTX, USBRX); |
caseyquinn | 1:37babeb68ab9 | 20 | DigitalOut blower(p29, 1); |
caseyquinn | 0:14d46ef4b6cb | 21 | |
caseyquinn | 7:a24d7156bc02 | 22 | //Timer t; |
caseyquinn | 4:69bd7e8a994c | 23 | |
caseyquinn | 7:a24d7156bc02 | 24 | //float battAmps; |
caseyquinn | 7:a24d7156bc02 | 25 | //float battVolts; |
caseyquinn | 7:a24d7156bc02 | 26 | //float battGG; |
caseyquinn | 0:14d46ef4b6cb | 27 | int omronReading; |
caseyquinn | 0:14d46ef4b6cb | 28 | int vInReading; |
caseyquinn | 0:14d46ef4b6cb | 29 | int vBlowerReading; |
caseyquinn | 0:14d46ef4b6cb | 30 | int omronDiff; |
caseyquinn | 7:a24d7156bc02 | 31 | int digital_pot_setpoint = 0x46; //min = 0x7F, max = 0x00 |
caseyquinn | 3:2d059d88582b | 32 | char filename[] = "/sd/UPASLOG00.txt"; |
caseyquinn | 7:a24d7156bc02 | 33 | |
caseyquinn | 7:a24d7156bc02 | 34 | TwoWire Wire = TwoWire(NRF_TWI0); |
caseyquinn | 7:a24d7156bc02 | 35 | SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14) |
caseyquinn | 7:a24d7156bc02 | 36 | |
caseyquinn | 7:a24d7156bc02 | 37 | uint8_t Seconds = 0;//Seconds |
caseyquinn | 7:a24d7156bc02 | 38 | uint8_t Minutes = 0;//Minutes |
caseyquinn | 7:a24d7156bc02 | 39 | uint8_t Hour = 0;//Hour |
caseyquinn | 7:a24d7156bc02 | 40 | uint8_t Date = 0;//Date |
caseyquinn | 7:a24d7156bc02 | 41 | uint8_t Month = 0;//Month |
caseyquinn | 7:a24d7156bc02 | 42 | uint8_t Year = 0;//Year |
caseyquinn | 7:a24d7156bc02 | 43 | |
caseyquinn | 7:a24d7156bc02 | 44 | void get_time() |
caseyquinn | 7:a24d7156bc02 | 45 | { |
caseyquinn | 7:a24d7156bc02 | 46 | Wire.beginTransmission(Crono); // address DS3231 |
caseyquinn | 7:a24d7156bc02 | 47 | Wire.write(0x0E); // select register |
caseyquinn | 7:a24d7156bc02 | 48 | Wire.write(0x1C); // write register bitmap, bit 7 is /EOSC |
caseyquinn | 7:a24d7156bc02 | 49 | Wire.endTransmission(); |
caseyquinn | 7:a24d7156bc02 | 50 | wait(0.05); |
caseyquinn | 7:a24d7156bc02 | 51 | Wire.beginTransmission(Crono); |
caseyquinn | 7:a24d7156bc02 | 52 | Wire.write(0x00); |
caseyquinn | 7:a24d7156bc02 | 53 | Wire.endTransmission(); |
caseyquinn | 7:a24d7156bc02 | 54 | Wire.requestFrom(Crono+1, 7); |
caseyquinn | 2:e596e685eb39 | 55 | |
caseyquinn | 7:a24d7156bc02 | 56 | while( Wire.available() > 0 ) |
caseyquinn | 7:a24d7156bc02 | 57 | { |
caseyquinn | 7:a24d7156bc02 | 58 | Seconds = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 59 | Minutes = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 60 | Hour = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 61 | uint8_t day =Wire.read(); // we don't uses this, it's just day of the week |
caseyquinn | 7:a24d7156bc02 | 62 | Date = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 63 | Month = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 64 | Year = Wire.read(); |
caseyquinn | 7:a24d7156bc02 | 65 | //pc.printf("%f", sec); pc.printf("\n");//this is still in bit form |
caseyquinn | 7:a24d7156bc02 | 66 | //pc.printf("%f", min); pc.printf("\n"); |
caseyquinn | 7:a24d7156bc02 | 67 | //pc.printf("%f", hr); pc.printf("\n"); |
caseyquinn | 7:a24d7156bc02 | 68 | } |
caseyquinn | 7:a24d7156bc02 | 69 | Year = ((Year&0xF0)>>4)*10 + (Year&0x0F); //Year |
caseyquinn | 7:a24d7156bc02 | 70 | Month = ((Month&0x10)>>4)*10 + (Month&0x0F); //Month |
caseyquinn | 7:a24d7156bc02 | 71 | Date = ((Date&0x30)>>4)*10 + (Date&0x0F); //Date |
caseyquinn | 7:a24d7156bc02 | 72 | Hour = ((Hour&0x10)>>4)*10 + (Hour&0x0F); //Hour |
caseyquinn | 7:a24d7156bc02 | 73 | Minutes = ((Minutes&0x70)>>4)*10 + (Minutes&0x0F); //Minutes |
caseyquinn | 7:a24d7156bc02 | 74 | Seconds = ((Seconds&0x70)>>4)*10 + (Seconds&0x0F); //Seconds |
caseyquinn | 7:a24d7156bc02 | 75 | } |
caseyquinn | 2:e596e685eb39 | 76 | |
caseyquinn | 0:14d46ef4b6cb | 77 | int main() |
caseyquinn | 0:14d46ef4b6cb | 78 | { |
caseyquinn | 2:e596e685eb39 | 79 | |
caseyquinn | 7:a24d7156bc02 | 80 | //t.start(); |
caseyquinn | 2:e596e685eb39 | 81 | // create a new file |
caseyquinn | 2:e596e685eb39 | 82 | |
caseyquinn | 7:a24d7156bc02 | 83 | Wire.begin(SCL, SDA, TWI_FREQUENCY_100K);// |
caseyquinn | 7:a24d7156bc02 | 84 | wait(1); |
caseyquinn | 7:a24d7156bc02 | 85 | |
caseyquinn | 7:a24d7156bc02 | 86 | |
caseyquinn | 3:2d059d88582b | 87 | for (uint8_t i = 0; i < 100; i++) { |
caseyquinn | 3:2d059d88582b | 88 | filename[11] = i/10 + '0'; |
caseyquinn | 3:2d059d88582b | 89 | filename[12] = i%10 + '0'; |
caseyquinn | 2:e596e685eb39 | 90 | FILE *fp = fopen(filename, "r"); |
caseyquinn | 2:e596e685eb39 | 91 | if (fp == NULL) { |
caseyquinn | 2:e596e685eb39 | 92 | // only open a new file if it doesn't exist |
caseyquinn | 2:e596e685eb39 | 93 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 2:e596e685eb39 | 94 | fclose(fp); |
caseyquinn | 2:e596e685eb39 | 95 | break; // leave the loop! |
caseyquinn | 7:a24d7156bc02 | 96 | } |
caseyquinn | 2:e596e685eb39 | 97 | } |
caseyquinn | 7:a24d7156bc02 | 98 | |
caseyquinn | 2:e596e685eb39 | 99 | |
caseyquinn | 1:37babeb68ab9 | 100 | |
caseyquinn | 0:14d46ef4b6cb | 101 | while(1){ |
caseyquinn | 7:a24d7156bc02 | 102 | |
caseyquinn | 7:a24d7156bc02 | 103 | get_time(); |
caseyquinn | 6:2328feb48792 | 104 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 6:2328feb48792 | 105 | //omronReading = ads.readADC_SingleEnded(0, 0xC183); // read channel 0 PGA = 1 : Full Scale Range = 4.096V |
caseyquinn | 0:14d46ef4b6cb | 106 | vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 |
caseyquinn | 0:14d46ef4b6cb | 107 | vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 |
caseyquinn | 0:14d46ef4b6cb | 108 | omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 |
caseyquinn | 7:a24d7156bc02 | 109 | //battAmps = gasG.getAmps(); |
caseyquinn | 7:a24d7156bc02 | 110 | //battVolts = gasG.getVolts(); |
caseyquinn | 7:a24d7156bc02 | 111 | //battGG = gasG.getCharge(); |
caseyquinn | 7:a24d7156bc02 | 112 | //pc.printf("%d Amps, %d Volts, %d GG\n", gasG.getAmps(), gasG.getVolts(), gasG.getCharge()); |
caseyquinn | 2:e596e685eb39 | 113 | //Mount the filesystem |
caseyquinn | 2:e596e685eb39 | 114 | sd.mount(); |
caseyquinn | 3:2d059d88582b | 115 | FILE *fp = fopen(filename, "a"); |
caseyquinn | 7:a24d7156bc02 | 116 | fprintf(fp, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%2.2f,%04.2f\r\n", Year,Month,Date,Hour,Minutes,Seconds,omronReading,vInReading, vBlowerReading, omronDiff, gasG.getAmps(), gasG.getVolts(), gasG.getCharge(), bmpsensor.getTemperature(), bmpsensor.getPressure()); |
caseyquinn | 2:e596e685eb39 | 117 | fclose(fp); |
caseyquinn | 2:e596e685eb39 | 118 | //Unmount the filesystem |
caseyquinn | 2:e596e685eb39 | 119 | sd.unmount(); |
caseyquinn | 2:e596e685eb39 | 120 | |
caseyquinn | 7:a24d7156bc02 | 121 | wait(10); |
caseyquinn | 2:e596e685eb39 | 122 | |
caseyquinn | 2:e596e685eb39 | 123 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 2:e596e685eb39 | 124 | |
caseyquinn | 2:e596e685eb39 | 125 | |
caseyquinn | 0:14d46ef4b6cb | 126 | } |
caseyquinn | 0:14d46ef4b6cb | 127 | } |
caseyquinn | 0:14d46ef4b6cb | 128 | |
caseyquinn | 0:14d46ef4b6cb | 129 | |
caseyquinn | 0:14d46ef4b6cb | 130 |