±0.5°C Maximum Accuracy Digital Temperature Sensor
Dependents: mbed-os5-F303-18650-Manager-tp4056
Diff: MCP9808.h
- Revision:
- 3:54515d036e93
- Parent:
- 2:da266f1b2273
--- a/MCP9808.h Thu Apr 25 10:39:40 2019 +0000 +++ b/MCP9808.h Thu Apr 25 18:27:56 2019 +0000 @@ -22,7 +22,129 @@ /** Example: @code -[todo] +#include "mbed.h" +#include "MCP9808.h" + +MCP9808 myMCP9808 ( p28, p27, MCP9808::MCP9808_ADDRESS_0, 400000 ); // I2C_SDA | I2C_SCL +Serial pc ( USBTX, USBRX ); // tx, rx + +DigitalOut myled ( LED1 ); +Ticker newAction; + + + +// @brief Constants. + + +// @brief Variables. +volatile uint32_t myState; // State that indicates when to perform a new sample + + + +// @brief FUNCTION PROTOTYPES +void changeDATA ( void ); + + + + +//@brief FUNCTION FOR APPLICATION MAIN ENTRY. +int main() +{ + MCP9808::MCP9808_status_t aux; + MCP9808::MCP9808_config_reg_t myMCP9808_Config; + MCP9808::MCP9808_data_t myMCP9808_Data; + + pc.baud ( 115200 ); + + + myled = 1; + wait(3); + myled = 0; + + // Shutdown the device, low-power mode enabled + aux = myMCP9808.MCP9808_GetCONFIG ( &myMCP9808_Config ); + + myMCP9808_Config.shdn = MCP9808::CONFIG_SHDN_SHUTDOWN; + aux = myMCP9808.MCP9808_SetCONFIG ( myMCP9808_Config ); + + // Get manufacturer ID + aux = myMCP9808.MCP9808_GetManufacturerID ( &myMCP9808_Data ); + + // Get device ID and device revision + aux = myMCP9808.MCP9808_GetDeviceID ( &myMCP9808_Data ); + + // Configure the device + // - T_UPPER and T_LOWER limit hysteresis at 0C + // - Continuous conversion mode + // - T_CRIT unlocked + // - Window lock unlocked + // - Alert output control disabled + // - Alert output select: Alert for T_UPPER, T_LOWER and T_CRIT + // - Alert output polaruty: Active-low + // - Alert output mode: Comparator output + // + myMCP9808_Config.t_hyst = MCP9808::CONFIG_T_HYST_0_C; + myMCP9808_Config.shdn = MCP9808::CONFIG_SHDN_CONTINUOUS_CONVERSION; + myMCP9808_Config.t_crit = MCP9808::CONFIG_CRIT_LOCK_UNLOCKED; + myMCP9808_Config.t_win_lock = MCP9808::CONFIG_WIN_LOCK_UNLOCKED; + myMCP9808_Config.alert_cnt = MCP9808::CONFIG_ALERT_CNT_DISABLED; + myMCP9808_Config.alert_sel = MCP9808::CONFIG_ALERT_SEL_TUPPER_TLOWER_TCRIT; + myMCP9808_Config.alert_pol = MCP9808::CONFIG_ALERT_POL_ACTIVE_LOW; + myMCP9808_Config.alert_mod = MCP9808::CONFIG_ALERT_MOD_COMPARATOR_OUTPUT; + aux = myMCP9808.MCP9808_SetCONFIG ( myMCP9808_Config ); + + // Set resolution: +0.0625C ( t_CON ~ 250ms ) + myMCP9808_Data.resolution = MCP9808::RESOLUTION_0_0625_C; + aux = myMCP9808.MCP9808_SetResolution ( myMCP9808_Data ); + + + myState = 0UL; // Reset the variable + newAction.attach( &changeDATA, 1U ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) + + + // Let the callbacks take care of everything + while(1) { + sleep(); + + if ( myState == 1UL ) { + myled = 1U; + + // Get ambient temperature + aux = myMCP9808.MCP9808_GetTA ( &myMCP9808_Data ); + + // Send data through the UART + pc.printf ( "T: %0.4f C\r\n", myMCP9808_Data.t_a ); + + + // Reset the variables + myState = 0UL; + myled = 0U; + } + } +} + + + // @brief changeDATA ( void ) + // + // @details It changes myState variable + // + // @param[in] N/A + // + // @param[out] N/A. + // + // @return N/A. + // + // + // @author Manuel Caballero + // @date 25/April/2019 + // @version 25/April/2019 The ORIGIN + // @pre N/A + // @warning N/A. + // +void changeDATA ( void ) +{ + myState = 1UL; +} @endcode */