±0.5°C Maximum Accuracy Digital Temperature Sensor

Dependents:   mbed-os5-F303-18650-Manager-tp4056

Files at this revision

API Documentation at this revision

Comitter:
mcm
Date:
Thu Apr 25 18:27:56 2019 +0000
Parent:
2:da266f1b2273
Commit message:
The driver was completed and tested ( mbed LPC1768 ), it works as expected.

Changed in this revision

MCP9808.cpp Show annotated file Show diff for this revision Revisions of this file
MCP9808.h Show annotated file Show diff for this revision Revisions of this file
diff -r da266f1b2273 -r 54515d036e93 MCP9808.cpp
--- a/MCP9808.cpp	Thu Apr 25 10:39:40 2019 +0000
+++ b/MCP9808.cpp	Thu Apr 25 18:27:56 2019 +0000
@@ -121,8 +121,8 @@
                       myCONFIG.alert_stat | myCONFIG.alert_cnt | myCONFIG.alert_sel | myCONFIG.alert_pol | myCONFIG.alert_mod );
   
   cmd[0]   =   MCP9808_CONFIG;
-  cmd[1]   =   (char)( myConfigAux >> 8U );
-  cmd[2]   =   (char)( myConfigAux & 0xFF );
+  cmd[1]   =   (uint8_t)( myConfigAux >> 8U );
+  cmd[2]   =   (uint8_t)( myConfigAux & 0xFF );
   aux      =   _i2c.write ( _MCP9808_Addr, &cmd[0], sizeof( cmd )/sizeof( cmd[0] ), false );
 
 
@@ -210,8 +210,8 @@
  */
 MCP9808::MCP9808_status_t MCP9808::MCP9808_GetTA ( MCP9808_data_t* myTA )
 {
-  char          myUpperByte   =   0U;
-  char          myLowerByte   =   0U;
+  uint8_t          myUpperByte   =   0U;
+  uint8_t          myLowerByte   =   0U;
   MCP9808::MCP9808_status_t aux;
 
   /* Read the register   */
@@ -225,17 +225,17 @@
   myTA->t_a_sign     =   (MCP9808_t_a_sign_t)( myTA->t_a_raw & T_A_TA_SIGN_MASK );
 
   /* Mask the ambient temperature value  */
-  myUpperByte  =   (char)( myTA->t_a_raw >> 8U );
-  myLowerByte  =   (char)( myTA->t_a_raw & 0xFF );
+  myUpperByte  =   (uint8_t)( myTA->t_a_raw >> 8U );
+  myLowerByte  =   (uint8_t)( myTA->t_a_raw & 0xFF );
 
   /* Clean the flags   */
-  myUpperByte &=  ~(char)( ( T_A_TA_VS_TCRIT_MASK | T_A_TA_VS_TUPPER_MASK | T_A_TA_VS_TLOWER_MASK ) >> 8U );
+  myUpperByte &=  ~(uint8_t)( ( T_A_TA_VS_TCRIT_MASK | T_A_TA_VS_TUPPER_MASK | T_A_TA_VS_TLOWER_MASK ) >> 8U );
   
   /* Check if T_A is negative/positive   */
   if ( myTA->t_a_sign == T_A_TA_SIGN_NEGATIVE )
   {
-    /* Ambient temperature is NEGATIVE   */
-    myUpperByte &=  ~(char)( T_A_TA_SIGN_MASK >> 8U );                                     // Clear the SIGN flag
+    /* Ambient uint8_t is NEGATIVE   */
+    myUpperByte &=  ~(uint8_t)( T_A_TA_SIGN_MASK >> 8U );                                     // Clear the SIGN flag
     myTA->t_a    =   256.0f - (float)( ( myUpperByte * 16.0f ) + ( myLowerByte / 16.0f ) );   // Ambient temperature value
   }
   else
@@ -466,7 +466,7 @@
 {
   char     cmd[2]     = { 0U };
   int8_t   myDecimal  = 0U;
-  char     myIntegral = 0U;
+  int8_t   myIntegral = 0U;
   uint32_t aux;
   
   /* Only temperature limit registers can keep going   */
@@ -478,7 +478,7 @@
   {
     /* Parse the data  */
     myIntegral =   (int8_t)myTValue_Limit.t_upper;
-    myDecimal  =   (char)( ( myTValue_Limit.t_upper - myIntegral ) * 100.0f );
+    myDecimal  =   (uint8_t)( ( myTValue_Limit.t_upper - myIntegral ) * 100.0f );
   
     /* Check the decimal part is correct; Valid decimal values: 0.00, 0.25, 0.50 or 0.75   */
     myIntegral <<=  4U;
@@ -532,7 +532,7 @@
 MCP9808::MCP9808_status_t MCP9808::MCP9808_GetT_Limit ( MCP9808_registers_t myTLimit, MCP9808_data_t* myTValue_Limit )
 {
   char     cmd        = 0U;
-  char     myDecimal  = 0U;
+  uint8_t  myDecimal  = 0U;
   float    myAuxValue = 0U;
   uint32_t aux;
   
@@ -575,7 +575,6 @@
 
       default:
         return  MCP9808_FAILURE;
-        break;
     }
 
   
diff -r da266f1b2273 -r 54515d036e93 MCP9808.h
--- 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
 */