Dependencies:   MAX30205 max32630fthr

Fork of MAX30205_Demo by John Greene

main.cpp

Committer:
coreyharris
Date:
2017-08-22
Revision:
4:7865b145ff3c
Parent:
3:4a8ab8ffd52d
Child:
5:fb64c72bb867

File content as of revision 4:7865b145ff3c:

#include "mbed.h"
#include "max32630fthr.h"
#include "MAX30205.h"

MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);

bool temp_sensor_config(MAX30205 &temp_sensor);

int main()
{
    
    Serial pc(USBTX, USBRX);        // Use USB debug probe for serial link
    pc.baud(115200);                // Baud rate = 115200
        
    DigitalOut rLed(LED1, LED_OFF);     // Debug LEDs
    DigitalOut gLed(LED2, LED_OFF);
    DigitalOut bLed(LED3, LED_OFF);
    
    I2C i2cBus(I2C1_SDA, I2C1_SCL);     // I2C bus, P3_4 = SDA, P3_5 = SCL   
    
    MAX30205 temp_sensor(i2cBus, 0x48);         // New MAX30205 on i2cBus
    int rc = temp_sensor_config(temp_sensor);   // Configure sensor, return 0 on success
    
    Timer temp_sensor_sampleTimer;      // Create temp. sensor sample timer 
    temp_sensor_sampleTimer.start();    // Start the timer
    
    MAX30205::Configuration_u temp_cfg;
    uint16_t rawTemperatureRead, temp_conversion_flag;
    float temperature;
    
    while(1) {
        
        if( rc == 0 ) {
        
            // Start a new temperature conversion 
            if ( ( temp_sensor_sampleTimer.read() > 1.0f ) && !temp_conversion_flag && ( rc == 0 ) ) {
                
                temp_cfg.bits.one_shot = 1;
                rc = temp_sensor->writeConfiguration(temp_cfg);     // Send one-shot cmd to begin conversion  
                temp_conversion_flag = 1;                           // Raise flag indicating a conversion has begun
                
            } 
            
            // Read the completed temperature conversion 
            if ( ( temp_sensor_sampleTimer.read() > 1.05f ) && temp_conversion_flag && ( rc == 0 ) ){
                
                temp_conversion_flag = 0;                                   // Lower flag when conversion has completed
                rc = temp_sensor->readTemperature(rawTemperatureRead);      // Read the temperature data
                temperature = temp_sensor->toCelsius(rawTemperatureRead);   // Convert temp data to Celsius
                temp_sensor_sampleTimer.reset();                            // Reset timer 
                
                pc.printf("Temperature is %2.3f deg. C \r\n", temperature);
                  
            }
        
        }else{
            
            pc.printf("Something went wrong, check the I2C bus and power connections... \r\n");
            bLed = LED_OFF;
            gLed = LED_OFF;
            
            while(1){
                rLed = !rLed;
                wait(0.5);   
            }
            
        }
    }
}


bool temp_sensor_config(MAX30205 &temp_sensor){
     
    int rc = 0;
    
    MAX30205::Configuration_u temp_cfg;
    temp_cfg.all = 0;
    temp_cfg.bits.shutdown = 1;     // Shutdown mode
    temp_cfg.bits.comp_int = 1;     // Interrupt mode
    temp_cfg.bits.os_polarity = 0;  // Active low OS
    temp_cfg.bits.fault_queue = 1;  // Two faults for OS condition
    temp_cfg.bits.data_format = 0;  // Normal data format
    temp_cfg.bits.timeout = 0;      // I2C timeout reset enabled
    temp_cfg.bits.one_shot = 0;     // Start with one-shot = 0
        
    rc = temp_sensor.writeConfiguration(temp_cfg);  // Write config to MAX30205    
    
    return rc;
}