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.

Version: Temperature Sensor Driver Version 1.0.0
1.4.1.1.1 Data structures
1.4.1.1.3 Functions

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.

Parameters:
float

tempVal - Temperature value to be converted.

Returns:

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.

Parameters:
float

tempVal - Temperature value to be converted.

Returns:

Converted value in Celsius.

1.4.1.2.3 Temp_Sensor_Initialize()

TEMP_SENSOR_STATUS_t Temp_Sensor_Initialize (void )

Initializes the temperature sensor.

Parameters:
None.
Returns:

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.

Parameters:
None.
Returns:

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.

Parameters:
None.
Returns:

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.

Parameters:
float*

tempCelsiusVal - Pointer to a user-defined variable where the temperature in Celsius is to be stored.

Returns:

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.

Parameters:
float*

tempFahrenheitVal - Pointer to a user-defined variable where the temperature in Fahrenheit is to be stored.

Returns:

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.

Parameters:
uint16_t*

tempRawVal - Pointer to a user-defined variable where the temperature raw value is to be stored.

Returns:

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

The examples provided here show how to configure the Temperature Sensor Library with the MCP9808 temperature sensor, and how to implement the APIs for various use cases. The settings below are generic for all the use cases listed in this document:
  • 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

Below is an example of the specific settings for the AVR Cellular Mini Board:
  • 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
Reads temperature and sends the value in degrees Celsius to the serial terminal.
#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
Reads temperature and sends the value in degrees Fahrenheit to the serial terminal.
#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.2 Field Documentation

The documentation for this struct was generated from the following file:

source/

temp_sensor.h

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

1.6.2.3 Detailed Description

Contains the API definitions for the Temperature Sensor module.

Temp_Sensor generated driver source file.

Version: Temperature Sensor Driver Version 1.0.0

1.6.2.4 Variable Documentation

1.6.3 source/temp_sensor.h File Reference

#include <stdint.h>

1.6.3.1 Data structures

1.6.3.2 Functions

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