Dependencies: MAX30205 max32630fthr
Fork of MAX30205_Demo by
main.cpp@3:4a8ab8ffd52d, 2017-08-21 (annotated)
- Committer:
- coreyharris
- Date:
- Mon Aug 21 14:57:25 2017 +0000
- Revision:
- 3:4a8ab8ffd52d
- Parent:
- 2:cf07a3eb2f30
- Child:
- 4:7865b145ff3c
Comments completed (or nearly) and added another rc check
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coreyharris | 0:6e28e543a11c | 1 | #include "mbed.h" |
coreyharris | 0:6e28e543a11c | 2 | #include "max32630fthr.h" |
coreyharris | 0:6e28e543a11c | 3 | #include "MAX30205.h" |
coreyharris | 0:6e28e543a11c | 4 | |
coreyharris | 0:6e28e543a11c | 5 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
coreyharris | 0:6e28e543a11c | 6 | |
coreyharris | 0:6e28e543a11c | 7 | bool temp_sensor_config(MAX30205 &temp_sensor); |
coreyharris | 0:6e28e543a11c | 8 | |
coreyharris | 0:6e28e543a11c | 9 | int main() |
coreyharris | 0:6e28e543a11c | 10 | { |
coreyharris | 0:6e28e543a11c | 11 | |
coreyharris | 3:4a8ab8ffd52d | 12 | Serial pc(USBTX, USBRX); // Use USB debug probe for serial link |
coreyharris | 3:4a8ab8ffd52d | 13 | pc.baud(115200); // Baud rate = 115200 |
coreyharris | 0:6e28e543a11c | 14 | |
coreyharris | 3:4a8ab8ffd52d | 15 | DigitalOut rLed(LED1, LED_OFF); // Debug LEDs |
coreyharris | 0:6e28e543a11c | 16 | DigitalOut gLed(LED2, LED_OFF); |
coreyharris | 0:6e28e543a11c | 17 | DigitalOut bLed(LED3, LED_OFF); |
coreyharris | 0:6e28e543a11c | 18 | |
coreyharris | 3:4a8ab8ffd52d | 19 | I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus, P3_4 = SDA, P3_5 = SCL |
coreyharris | 0:6e28e543a11c | 20 | |
coreyharris | 0:6e28e543a11c | 21 | MAX30205 * temp_sensor; |
coreyharris | 0:6e28e543a11c | 22 | temp_sensor = new MAX30205(i2cBus, 0x48); |
coreyharris | 3:4a8ab8ffd52d | 23 | int rc = temp_sensor_config(*temp_sensor); // Configure the temp. sensor |
coreyharris | 0:6e28e543a11c | 24 | |
coreyharris | 3:4a8ab8ffd52d | 25 | Timer temp_sensor_sampleTimer; // Create temp. sesnor sample timer |
coreyharris | 3:4a8ab8ffd52d | 26 | temp_sensor_sampleTimer.start(); // Start the timer |
coreyharris | 0:6e28e543a11c | 27 | |
coreyharris | 0:6e28e543a11c | 28 | MAX30205::Configuration_u temp_cfg; |
coreyharris | 0:6e28e543a11c | 29 | uint16_t rawTemperatureRead, temp_conversion_flag; |
coreyharris | 0:6e28e543a11c | 30 | float temperature; |
coreyharris | 0:6e28e543a11c | 31 | |
coreyharris | 0:6e28e543a11c | 32 | while(1) { |
coreyharris | 0:6e28e543a11c | 33 | |
coreyharris | 0:6e28e543a11c | 34 | if( rc == 0 ) { |
coreyharris | 0:6e28e543a11c | 35 | |
coreyharris | 0:6e28e543a11c | 36 | /* Start a new temperature conversion */ |
coreyharris | 3:4a8ab8ffd52d | 37 | if ( temp_sensor_sampleTimer.read() > 1.0 && !temp_conversion_flag && rc == 0) { |
coreyharris | 0:6e28e543a11c | 38 | |
coreyharris | 0:6e28e543a11c | 39 | temp_cfg.bits.one_shot = 1; |
coreyharris | 2:cf07a3eb2f30 | 40 | rc = temp_sensor->writeConfiguration(temp_cfg); // Send one-shot cmd to begin conversion |
coreyharris | 2:cf07a3eb2f30 | 41 | temp_conversion_flag = 1; // Raise flag indicating a conversion has begun |
coreyharris | 0:6e28e543a11c | 42 | |
coreyharris | 0:6e28e543a11c | 43 | } |
coreyharris | 0:6e28e543a11c | 44 | |
coreyharris | 0:6e28e543a11c | 45 | /* Read the completed temperature conversion */ |
coreyharris | 3:4a8ab8ffd52d | 46 | if ( temp_sensor_sampleTimer.read() > 1.05 && temp_conversion_flag && rc == 0){ |
coreyharris | 0:6e28e543a11c | 47 | |
coreyharris | 2:cf07a3eb2f30 | 48 | temp_conversion_flag = 0; // Lower flag when conversion has completed |
coreyharris | 2:cf07a3eb2f30 | 49 | rc = temp_sensor->readTemperature(rawTemperatureRead); // Read the temperature data |
coreyharris | 2:cf07a3eb2f30 | 50 | temperature = temp_sensor->toCelsius(rawTemperatureRead); // Convert temp data to Celsius |
coreyharris | 2:cf07a3eb2f30 | 51 | temp_sensor_sampleTimer.reset(); // Reset timer |
coreyharris | 0:6e28e543a11c | 52 | |
coreyharris | 0:6e28e543a11c | 53 | pc.printf("Temperature is %2.3f deg. C \r\n", temperature); |
coreyharris | 0:6e28e543a11c | 54 | |
coreyharris | 0:6e28e543a11c | 55 | } |
coreyharris | 0:6e28e543a11c | 56 | |
coreyharris | 0:6e28e543a11c | 57 | }else{ |
coreyharris | 3:4a8ab8ffd52d | 58 | pc.printf("Something went wrong, check the I2C bus and power connections... \r\n"); |
coreyharris | 0:6e28e543a11c | 59 | bLed = LED_OFF; |
coreyharris | 0:6e28e543a11c | 60 | gLed = LED_OFF; |
coreyharris | 0:6e28e543a11c | 61 | while(1){ |
coreyharris | 0:6e28e543a11c | 62 | rLed = !rLed; |
coreyharris | 0:6e28e543a11c | 63 | wait(0.5); |
coreyharris | 0:6e28e543a11c | 64 | } |
coreyharris | 0:6e28e543a11c | 65 | } |
coreyharris | 0:6e28e543a11c | 66 | } |
coreyharris | 0:6e28e543a11c | 67 | } |
coreyharris | 0:6e28e543a11c | 68 | |
coreyharris | 0:6e28e543a11c | 69 | |
coreyharris | 0:6e28e543a11c | 70 | bool temp_sensor_config(MAX30205 &temp_sensor){ |
coreyharris | 0:6e28e543a11c | 71 | |
coreyharris | 0:6e28e543a11c | 72 | int rc = 0; |
coreyharris | 0:6e28e543a11c | 73 | |
coreyharris | 3:4a8ab8ffd52d | 74 | MAX30205::Configuration_u temp_cfg; |
coreyharris | 3:4a8ab8ffd52d | 75 | temp_cfg.all = 0; |
coreyharris | 3:4a8ab8ffd52d | 76 | temp_cfg.bits.shutdown = 1; // Shutdown mode |
coreyharris | 3:4a8ab8ffd52d | 77 | temp_cfg.bits.comp_int = 1; // Interrupt mode |
coreyharris | 3:4a8ab8ffd52d | 78 | temp_cfg.bits.os_polarity = 0; // Active low OS |
coreyharris | 3:4a8ab8ffd52d | 79 | temp_cfg.bits.fault_queue = 1; // Two faults for OS condition |
coreyharris | 3:4a8ab8ffd52d | 80 | temp_cfg.bits.data_format = 0; // Normal data format |
coreyharris | 3:4a8ab8ffd52d | 81 | temp_cfg.bits.timeout = 0; // I2C timeout reset enabled |
coreyharris | 3:4a8ab8ffd52d | 82 | temp_cfg.bits.one_shot = 0; // Start with one-shot = 0 |
coreyharris | 0:6e28e543a11c | 83 | |
coreyharris | 3:4a8ab8ffd52d | 84 | rc = temp_sensor.writeConfiguration(temp_cfg); // Write config to MAX30205 |
coreyharris | 0:6e28e543a11c | 85 | |
coreyharris | 0:6e28e543a11c | 86 | return rc; |
coreyharris | 0:6e28e543a11c | 87 | } |