Revision 3:b7c589b093e6, committed 2018-07-03
- Comitter:
- mcm
- Date:
- Tue Jul 03 08:41:44 2018 +0000
- Parent:
- 2:677d79e0be8c
- Commit message:
- The driver was completed and tested, it works as expected using a NUCLEO-L152RE
Changed in this revision
TMP102.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/TMP102.h Mon Jul 02 10:43:51 2018 +0000 +++ b/TMP102.h Tue Jul 03 08:41:44 2018 +0000 @@ -22,7 +22,84 @@ /** Example: @code -[todo] +#include "mbed.h" +#include "TMP102.h" + +TMP102 myTMP102 ( I2C_SDA, I2C_SCL, TMP102::TMP102_ADDRESS_A0_GROUND, 400000 ); +Serial pc ( USBTX, USBRX ); + +DigitalOut myled ( LED1 ); +Ticker newReading; + +TMP102::TMP102_status_t aux; +TMP102::TMP102_vector_data_t myTMP102_Data; +uint32_t myState = 0; + + +void changeDATA ( void ) +{ + myState = 1; +} + + +int main() +{ + pc.baud ( 115200 ); + + myled = 1; + wait(3); + myled = 0; + + + // Read TLOW value + aux = myTMP102.TMP102_Read_T_LOW_Register ( &myTMP102_Data ); + + // Read THIGH value + aux = myTMP102.TMP102_Read_T_HIGH_Register ( &myTMP102_Data ); + + // Set polarity LOW + aux = myTMP102.TMP102_SetPolarityAlertPinOutput ( TMP102::TMP102_CONFIGURATION_POL_ALERT_PIN_ACTIVE_LOW ); + + // Set Normal mode operation + aux = myTMP102.TMP102_SetModeOperation ( TMP102::TMP102_CONFIGURATION_EM_NORMAL_MODE_OPERATION ); + + // Set Conversion Rate: 4Hz + aux = myTMP102.TMP102_SetConversionRate ( TMP102::TMP102_CONFIGURATION_CR_4_HZ ); + + // Set Shutdown mode + aux = myTMP102.TMP102_SetShutdownMode ( TMP102::TMP102_CONFIGURATION_SD_ENABLED ); + + + newReading.attach( &changeDATA, 1 ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) + + // Let the callbacks take care of everything + while(1) { + sleep(); + + myled = 1; + + if ( myState == 1 ) { + // New reading + aux = myTMP102.TMP102_TriggerSingleTemperatureConversion (); + + // Wait until the conversion is finished + do { + aux = myTMP102.TMP102_ReadConfigurationRegister ( &myTMP102_Data ); + wait_ms(1); + } while( ( myTMP102_Data.ConfigurationRegister & TMP102::TMP102_CONFIGURATION_OS_MASK ) == TMP102::TMP102_CONFIGURATION_OS_BUSY ); // [TODO] Dangerous!!! The uC may get stuck here if something goes wrong! + // [WORKAROUND] Insert a counter. + + // Read the new temperature value + aux = myTMP102.TMP102_GetTemperature ( &myTMP102_Data ); + + // Send data through the UART + pc.printf( "Temperature: %0.4f C \r\n", myTMP102_Data.Temperature ); + myState = 0; // Reset the variable + } + + myled = 0; + } +} @endcode */