Dependencies: MAX30205 max32630fthr
Fork of MAX30205_Demo by
main.cpp@4:7865b145ff3c, 2017-08-22 (annotated)
- Committer:
- coreyharris
- Date:
- Tue Aug 22 21:54:03 2017 +0000
- Revision:
- 4:7865b145ff3c
- Parent:
- 3:4a8ab8ffd52d
- Child:
- 5:fb64c72bb867
Fixed whitespace, added comments (post code review)
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 | 4:7865b145ff3c | 21 | MAX30205 temp_sensor(i2cBus, 0x48); // New MAX30205 on i2cBus |
coreyharris | 4:7865b145ff3c | 22 | int rc = temp_sensor_config(temp_sensor); // Configure sensor, return 0 on success |
coreyharris | 0:6e28e543a11c | 23 | |
coreyharris | 4:7865b145ff3c | 24 | Timer temp_sensor_sampleTimer; // Create temp. sensor sample timer |
coreyharris | 3:4a8ab8ffd52d | 25 | temp_sensor_sampleTimer.start(); // Start the timer |
coreyharris | 0:6e28e543a11c | 26 | |
coreyharris | 0:6e28e543a11c | 27 | MAX30205::Configuration_u temp_cfg; |
coreyharris | 0:6e28e543a11c | 28 | uint16_t rawTemperatureRead, temp_conversion_flag; |
coreyharris | 0:6e28e543a11c | 29 | float temperature; |
coreyharris | 0:6e28e543a11c | 30 | |
coreyharris | 0:6e28e543a11c | 31 | while(1) { |
coreyharris | 0:6e28e543a11c | 32 | |
coreyharris | 0:6e28e543a11c | 33 | if( rc == 0 ) { |
coreyharris | 0:6e28e543a11c | 34 | |
coreyharris | 4:7865b145ff3c | 35 | // Start a new temperature conversion |
coreyharris | 4:7865b145ff3c | 36 | if ( ( temp_sensor_sampleTimer.read() > 1.0f ) && !temp_conversion_flag && ( rc == 0 ) ) { |
coreyharris | 0:6e28e543a11c | 37 | |
coreyharris | 0:6e28e543a11c | 38 | temp_cfg.bits.one_shot = 1; |
coreyharris | 2:cf07a3eb2f30 | 39 | rc = temp_sensor->writeConfiguration(temp_cfg); // Send one-shot cmd to begin conversion |
coreyharris | 2:cf07a3eb2f30 | 40 | temp_conversion_flag = 1; // Raise flag indicating a conversion has begun |
coreyharris | 0:6e28e543a11c | 41 | |
coreyharris | 0:6e28e543a11c | 42 | } |
coreyharris | 0:6e28e543a11c | 43 | |
coreyharris | 4:7865b145ff3c | 44 | // Read the completed temperature conversion |
coreyharris | 4:7865b145ff3c | 45 | if ( ( temp_sensor_sampleTimer.read() > 1.05f ) && temp_conversion_flag && ( rc == 0 ) ){ |
coreyharris | 0:6e28e543a11c | 46 | |
coreyharris | 2:cf07a3eb2f30 | 47 | temp_conversion_flag = 0; // Lower flag when conversion has completed |
coreyharris | 2:cf07a3eb2f30 | 48 | rc = temp_sensor->readTemperature(rawTemperatureRead); // Read the temperature data |
coreyharris | 2:cf07a3eb2f30 | 49 | temperature = temp_sensor->toCelsius(rawTemperatureRead); // Convert temp data to Celsius |
coreyharris | 2:cf07a3eb2f30 | 50 | temp_sensor_sampleTimer.reset(); // Reset timer |
coreyharris | 0:6e28e543a11c | 51 | |
coreyharris | 0:6e28e543a11c | 52 | pc.printf("Temperature is %2.3f deg. C \r\n", temperature); |
coreyharris | 0:6e28e543a11c | 53 | |
coreyharris | 0:6e28e543a11c | 54 | } |
coreyharris | 0:6e28e543a11c | 55 | |
coreyharris | 0:6e28e543a11c | 56 | }else{ |
coreyharris | 4:7865b145ff3c | 57 | |
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 | 4:7865b145ff3c | 61 | |
coreyharris | 0:6e28e543a11c | 62 | while(1){ |
coreyharris | 0:6e28e543a11c | 63 | rLed = !rLed; |
coreyharris | 0:6e28e543a11c | 64 | wait(0.5); |
coreyharris | 0:6e28e543a11c | 65 | } |
coreyharris | 4:7865b145ff3c | 66 | |
coreyharris | 0:6e28e543a11c | 67 | } |
coreyharris | 0:6e28e543a11c | 68 | } |
coreyharris | 0:6e28e543a11c | 69 | } |
coreyharris | 0:6e28e543a11c | 70 | |
coreyharris | 0:6e28e543a11c | 71 | |
coreyharris | 0:6e28e543a11c | 72 | bool temp_sensor_config(MAX30205 &temp_sensor){ |
coreyharris | 0:6e28e543a11c | 73 | |
coreyharris | 0:6e28e543a11c | 74 | int rc = 0; |
coreyharris | 0:6e28e543a11c | 75 | |
coreyharris | 3:4a8ab8ffd52d | 76 | MAX30205::Configuration_u temp_cfg; |
coreyharris | 3:4a8ab8ffd52d | 77 | temp_cfg.all = 0; |
coreyharris | 3:4a8ab8ffd52d | 78 | temp_cfg.bits.shutdown = 1; // Shutdown mode |
coreyharris | 3:4a8ab8ffd52d | 79 | temp_cfg.bits.comp_int = 1; // Interrupt mode |
coreyharris | 3:4a8ab8ffd52d | 80 | temp_cfg.bits.os_polarity = 0; // Active low OS |
coreyharris | 3:4a8ab8ffd52d | 81 | temp_cfg.bits.fault_queue = 1; // Two faults for OS condition |
coreyharris | 3:4a8ab8ffd52d | 82 | temp_cfg.bits.data_format = 0; // Normal data format |
coreyharris | 3:4a8ab8ffd52d | 83 | temp_cfg.bits.timeout = 0; // I2C timeout reset enabled |
coreyharris | 3:4a8ab8ffd52d | 84 | temp_cfg.bits.one_shot = 0; // Start with one-shot = 0 |
coreyharris | 0:6e28e543a11c | 85 | |
coreyharris | 3:4a8ab8ffd52d | 86 | rc = temp_sensor.writeConfiguration(temp_cfg); // Write config to MAX30205 |
coreyharris | 0:6e28e543a11c | 87 | |
coreyharris | 0:6e28e543a11c | 88 | return rc; |
coreyharris | 0:6e28e543a11c | 89 | } |