1 Temperature Sensor Library
Driver Library supporting Temperature Sensor operations
1.1 Introduction
The MCC Melody Temperature Sensor Library provides APIs for a temperature sensor's core functionalities. This library provides quick access to temperature readings, value conversions, and power mode selection.
1.2 Supported Device Families
PIC16F | PIC18F | tiny AVR | AVR |
1.3 Required Header Files
#include "mcc_generated_files/temperature/temp_sensor.h"
1.4 Module Documentation
1.4.1 TEMP_SENSOR
Contains the API declarations and related data classes for the Temperature Sensor driver.
1.4.1.1 Module description
Contains the API declarations and related data classes for the Temperature Sensor driver.
1.4.1.1.1 Data structures
struct TEMP_SENSOR_INTERFACE_t
Structure containing the function pointers to the Temperature Sensor driver APIs.
1.4.1.1.2 Enumerations
enum _TEMP_SENSOR_STATUS { TEMP_SENSOR_NO_ERROR = 0U, TEMP_SENSOR_ERROR = 1U, TEMP_SENSOR_COMMUNICATION_ERROR = 2U }
Enumeration of the Temperature Sensor driver status codes.
1.4.1.1.3 Functions
TEMP_SENSOR_STATUS_t Temp_Sensor_Initialize (void)
Initializes the temperature sensor.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureRawValueRead (uint16_t *tempRawVal)
Reads the temperature sensor data.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureCelsiusRead (float *tempCelsiusVal)
Reads the temperature sensor value in Celsius.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureFahrenheitRead (float *tempFahrenheitVal)
Reads the temperature sensor value in Fahrenheit.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerEnable (void)
Shifts the temperature sensor operation to Low-Power mode.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerDisable (void)
Returns the temperature sensor power mode to Normal.
float Temp_Sensor_CelsiusToFahrenheit (float tempVal)
Converts the temperature value from Celsius to Fahrenheit.
float Temp_Sensor_FahrenheitToCelsius (float tempVal)
Converts the temperature value from Fahrenheit to Celsius.
1.4.1.2 Function Documentation
1.4.1.2.1 Temp_Sensor_CelsiusToFahrenheit()
float Temp_Sensor_CelsiusToFahrenheit (float tempVal)
Converts the temperature value from Celsius to Fahrenheit.
float |
tempVal - Temperature value to be converted. |
Converted value in Fahrenheit. |
1.4.1.2.2 Temp_Sensor_FahrenheitToCelsius()
float Temp_Sensor_FahrenheitToCelsius (float tempVal)
Converts the temperature value from Fahrenheit to Celsius.
float |
tempVal - Temperature value to be converted. |
Converted value in Celsius. |
1.4.1.2.3 Temp_Sensor_Initialize()
TEMP_SENSOR_STATUS_t Temp_Sensor_Initialize (void )
Initializes the temperature sensor.
None. |
TEMP_SENSOR status code. |
1.4.1.2.4 Temp_Sensor_LowPowerDisable()
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerDisable (void )
Returns the temperature sensor power mode to Normal.
None. |
TEMP_SENSOR status code. |
1.4.1.2.5 Temp_Sensor_LowPowerEnable()
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerEnable (void )
Shifts the temperature sensor operation to Low-Power mode.
None. |
TEMP_SENSOR status code. |
1.4.1.2.6 Temp_Sensor_TemperatureCelsiusRead()
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureCelsiusRead (float * tempCelsiusVal)
Reads the temperature sensor value in Celsius.
float* |
tempCelsiusVal - Pointer to a user-defined variable where the temperature in Celsius is to be stored. |
TEMP_SENSOR status code. |
1.4.1.2.7 Temp_Sensor_TemperatureFahrenheitRead()
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureFahrenheitRead (float * tempFahrenheitVal)
Reads the temperature sensor value in Fahrenheit.
float* |
tempFahrenheitVal - Pointer to a user-defined variable where the temperature in Fahrenheit is to be stored. |
TEMP_SENSOR status code. |
1.4.1.2.8 Temp_Sensor_TemperatureRawValueRead()
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureRawValueRead (uint16_t * tempRawVal)
Reads the temperature sensor data.
uint16_t* |
tempRawVal - Pointer to a user-defined variable where the temperature raw value is to be stored. |
TEMP_SENSOR status code. |
1.4.1.3 Enumeration Type Documentation
1.4.1.3.1 _TEMP_SENSOR_STATUS
enum _TEMP_SENSOR_STATUS
Enumeration of the Temperature Sensor driver status codes.
TEMP_SENSOR_NO_ERROR | |
TEMP_SENSOR_ERROR | |
TEMP_SENSOR_COMMUNICATION_ERROR |
1.4.2 Temp Sensor Use Cases
1.4.2.1 Temp Sensor Configuration Instructions
-
Add Temperature > Temperature Sensor
-
Select Device "Off-chip: I2C Temp Sensor MCP98xx"
-
Select I2C Temp Sensor MCP98xx > Communication Settings > I2C Host Dependency > I2Cx (TWIx for AVR devices)
-
Configure the A2, A1, and A0 Address pins (please check board schematic)
-
Select I2Cx > Interrupt driven: Off
-
Add Drivers > UART
-
Select UART > Dependency Selector > UARTx (depends on use case)
-
Select UART > Redirect Printf to UART > On
-
Go to Pin Grid View
-
Select the I2C SDA and SCL pins connected to the MCP98xx device (please check board schematic)
-
Select the UART TX pin connected to the CDC port (please check board schematic)
-
Go to the Notifications tab and address all warnings
-
Generate
-
Make and Program Device Main Project
-
Select MCP98xx > I2C Host Dependency > TWI1
-
Select MCP98xx > A0 > VCC
-
Select TWI1 interface > Interrupt driven: Off
-
Select UART > Dependency Selector > USART3
1.4.2.2 Read Temperature
Reads temperature in degrees Celsius or Fahrenheit. Implements APIs to get sensor readings, and display these through the serial terminal.
1.4.2.2.1 Read temperature in Celsius
#include "mcc_generated_files/system/system.h" #include "mcc_generated_files/temperature-generic/temp_sensor.h" #include "mcc_generated_files/temperature-mcp98xx/mcp9808.h" uint16_t rawTemp; float celsiusVal; TEMP_SENSOR_STATUS_t errorStat; int main(void) { /* Initialize system */ SYSTEM_Initialize(); /* Initialize sensor */ errorStat = Temp_Sensor_Initialize(); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad init.\n"); return TEMP_SENSOR_ERROR; } while(1) { /* Read raw temperature value*/ errorStat = Temp_Sensor_TemperatureRawValueRead(&rawTemp); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad sensor reading.\n"); return TEMP_SENSOR_ERROR; } /* Convert raw value to Celsius*/ celsiusVal = MCP9808_TemperatureRawToCelsius(rawTemp); printf("Temp in Celsius: %f\n", celsiusVal); } }
1.4.2.2.2 Read temperature in Fahrenheit
#include "mcc_generated_files/system/system.h" #include "mcc_generated_files/temperature-generic/temp_sensor.h" #include "mcc_generated_files/temperature-mcp98xx/mcp9808.h" uint16_t rawTemp; float fahrenheitVal; TEMP_SENSOR_STATUS_t errorStat; int main(void) { /* Initialize system */ SYSTEM_Initialize(); /* Initialize sensor */ errorStat = Temp_Sensor_Initialize(); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad init.\n"); return TEMP_SENSOR_ERROR; } while(1) { /* Read raw temperature value */ errorStat = Temp_Sensor_TemperatureRawValueRead(&rawTemp); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad sensor reading.\n"); return TEMP_SENSOR_ERROR; } /* Convert raw value to Fahrenheit*/ fahrenheitVal = MCP9808_TemperatureRawToFahrenheit(rawTemp); printf("Temp in Fahrenheit: %f\n", fahrenheitVal); } }
1.4.2.3 Low power mode
All MCC Melody settings are defined in Temp Sensor Configuration Instructions, except for one additional configuration:
-
Add Driver > Timer > DELAY
Displays low power functionality of the MCP9808 device. Sets the device to sleep, wakes the device, takes sensor reading in Celsius, displays the reading through the serial terminal, and goes back to sleep.
#include "mcc_generated_files/system/system.h" #include "mcc_generated_files/temperature-generic/temp_sensor.h" #include "mcc_generated_files/temperature-mcp98xx/mcp9808.h" #include "mcc_generated_files/timer/delay.h uint16_t rawTemp; float celsiusVal; TEMP_SENSOR_STATUS_t errorStat; int main(void) { /* Initialize system */ SYSTEM_Initialize(); /* Initialize sensor */ errorStat = Temp_Sensor_Initialize(); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad init \n"); return TEMP_SENSOR_ERROR; } while(1) { /* Set sensor to low power mode*/ DELAY_milliseconds(300); printf("Entering sleep...\n"); errorStat = Temp_Sensor_LowPowerEnable(); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Failed to switch to low power mode. \n"); return TEMP_SENSOR_ERROR; } else{ printf("Sleep mode enabled... \n\n"); } /* Wake up sensor */ DELAY_milliseconds(1000); // Wait for 1 second before waking up the device errorStat = Temp_Sensor_LowPowerDisable(); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Failed to wake up device.\n"); return TEMP_SENSOR_ERROR; } printf("I'm awake!!!\n"); /* Read raw temperature value */ DELAY_milliseconds(300); errorStat = Temp_Sensor_TemperatureRawValueRead(&rawTemp); if (errorStat != TEMP_SENSOR_NO_ERROR) { printf("Error: Bad sensor reading.\n"); return TEMP_SENSOR_ERROR; } /* Convert raw value to Celsius*/ celsiusVal = MCP9808_TemperatureRawToCelsius(rawTemp); printf("Temp in Celsius: %f\n", celsiusVal); } }
1.5 Data Structure Documentation
1.5.1 TEMP_SENSOR_INTERFACE_t Struct Reference
Structure containing the function pointers to the Temperature Sensor driver APIs.
1.5.1.1 Detailed Description
Structure containing the function pointers to the Temperature Sensor driver APIs.
#include <temp_sensor.h>
1.5.1.1.1 Data Fields
TEMP_SENSOR_STATUS_t(* TempRead )(uint16_t *tempRawVal)
TEMP_SENSOR_STATUS_t(* TempCelsiusRead )(float *tempCelsiusVal)
TEMP_SENSOR_STATUS_t(* TempFahrenheitRead )(float *tempFahrenheitVal)
float(* CelsiusToFahrenheit )(float tempVal)
float(* FahrenheitToCelsius )(float tempVal)
1.5.1.2 Field Documentation
1.5.1.2.1 CelsiusToFahrenheit
float(* CelsiusToFahrenheit) (float tempVal)
1.5.1.2.2 FahrenheitToCelsius
float(* FahrenheitToCelsius) (float tempVal)
1.5.1.2.3 Initialize
TEMP_SENSOR_STATUS_t(* Initialize) ()
1.5.1.2.4 LowPowerDisable
TEMP_SENSOR_STATUS_t(* LowPowerDisable) ()
1.5.1.2.5 LowPowerEnable
TEMP_SENSOR_STATUS_t(* LowPowerEnable) ()
1.5.1.2.6 TempCelsiusRead
TEMP_SENSOR_STATUS_t(* TempCelsiusRead) (float *tempCelsiusVal)
1.5.1.2.7 TempFahrenheitRead
TEMP_SENSOR_STATUS_t(* TempFahrenheitRead) (float *tempFahrenheitVal)
1.5.1.2.8 TempRead
TEMP_SENSOR_STATUS_t(* TempRead) (uint16_t *tempRawVal)
1.6 File Documentation
1.6.1 source/temp-sensor-lib.dox File Reference
1.6.2 source/temp_sensor.c File Reference
Contains the API definitions for the Temperature Sensor module.
#include "../../temperature/temp_sensor.h"
1.6.2.1 Functions
TEMP_SENSOR_STATUS_t Temp_Sensor_Initialize (void)
Initializes the temperature sensor.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureRawValueRead (uint16_t *tempRawValue)
Reads the temperature sensor data.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureCelsiusRead (float *tempCelsiusVal)
Reads the temperature sensor value in Celsius.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureFahrenheitRead (float *tempFahrenheitVal)
Reads the temperature sensor value in Fahrenheit.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerEnable (void)
Shifts the temperature sensor operation to Low-Power mode.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerDisable (void)
Returns the temperature sensor power mode to Normal.
float Temp_Sensor_CelsiusToFahrenheit (float tempVal)
Converts the temperature value from Celsius to Fahrenheit.
float Temp_Sensor_FahrenheitToCelsius (float tempVal)
Converts the temperature value from Fahrenheit to Celsius.
1.6.2.2 Variables
1.6.2.3 Detailed Description
Contains the API definitions for the Temperature Sensor module.
Temp_Sensor generated driver source file.
1.6.2.4 Variable Documentation
1.6.2.4.1 Temp_Sensor
const TEMP_SENSOR_INTERFACE_t Temp_Sensor
Initial value:
={ .Initialize = Temp_Sensor_Initialize, .TempRead = Temp_Sensor_TemperatureRawValueRead, .TempCelsiusRead = Temp_Sensor_TemperatureCelsiusRead, .TempFahrenheitRead = Temp_Sensor_TemperatureFahrenheitRead, .LowPowerEnable = Temp_Sensor_LowPowerEnable, .LowPowerDisable = Temp_Sensor_LowPowerDisable, .CelsiusToFahrenheit = Temp_Sensor_CelsiusToFahrenheit, .FahrenheitToCelsius = Temp_Sensor_FahrenheitToCelsius }
1.6.3 source/temp_sensor.h File Reference
#include <stdint.h>
1.6.3.1 Data structures
struct TEMP_SENSOR_INTERFACE_t
Structure containing the function pointers to the Temperature Sensor driver APIs.
1.6.3.2 Functions
TEMP_SENSOR_STATUS_t Temp_Sensor_Initialize (void)
Initializes the temperature sensor.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureRawValueRead (uint16_t *tempRawVal)
Reads the temperature sensor data.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureCelsiusRead (float *tempCelsiusVal)
Reads the temperature sensor value in Celsius.
TEMP_SENSOR_STATUS_t Temp_Sensor_TemperatureFahrenheitRead (float *tempFahrenheitVal)
Reads the temperature sensor value in Fahrenheit.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerEnable (void)
Shifts the temperature sensor operation to Low-Power mode.
TEMP_SENSOR_STATUS_t Temp_Sensor_LowPowerDisable (void)
Returns the temperature sensor power mode to Normal.
float Temp_Sensor_CelsiusToFahrenheit (float tempVal)
Converts the temperature value from Celsius to Fahrenheit.
float Temp_Sensor_FahrenheitToCelsius (float tempVal)
Converts the temperature value from Fahrenheit to Celsius.
1.6.3.3 Typedefs
typedef enum _TEMP_SENSOR_STATUS TEMP_SENSOR_STATUS_t
1.6.3.4 Enumerations
enum _TEMP_SENSOR_STATUS { TEMP_SENSOR_NO_ERROR = 0U, TEMP_SENSOR_ERROR = 1U, TEMP_SENSOR_COMMUNICATION_ERROR = 2U }
Enumeration of the Temperature Sensor driver status codes.
1.6.3.5 Detailed Description
Temperature Sensor generated driver header file.
1.6.3.6 Typedef Documentation
1.6.3.6.1 TEMP_SENSOR_STATUS_t
typedef enum _TEMP_SENSOR_STATUS TEMP_SENSOR_STATUS_t