6. DA14531 Temperature Sensor

The DA14531 contains a built-in temperature sensor that can be read using General Purpose ADC (GPADC). The sensor has a temperature range of -40°C to 105°C and an absolute accuracy (after one-point calibration) of +/- 4°C (assuming a 25°C reference temperature). A 25°C single point calibration reference value is provided in OTP memory.

Note

You must have created a modified version of the empty_peripheral_template example project, as described in the Initial Project chapter before proceeding!

6.1. Using the Temperature Sensor

The following section provides step-by-step instructions showing how to update the modified empty_peripheral_template example project you created in the Initial Project chapter to read the Temperature Sensor via the GPADC peripheral driver provided with SDK6.

6.1.1. Adding the GPADC Driver

The GPADC driver is implemented in the file adc_531.c and this has already been added to the empty_peripheral_template example project. However, in order to use the ADC driver functions we need to add the appropriate include files into the user_empty_peripheral_template.c file:

#include "adc.h"
#include "adc_531.h"

6.1.2. Reading the Temperature Sensor

The SDK6 GPADC driver contains a function (adc_get_temp) that can be used to read the temperature sensor. Before calling this function the GPADC needs to be configured to select the temperature sensor input. Both the configuration of the GPADC and the reading of the temperature sensor can be acheived by adding the following function to the end of the user_empty_peripheral_template.c file:

static int8_t temp_sensor_read(void)
{
    int8_t temp;

    /* Most of the ADC configuration is performed by the driver when the temperature
       sensor input is selected so we can just leave it blank. */
    adc_config_t adc_cfg =
    {
        .input_mode = ADC_INPUT_MODE_SINGLE_ENDED,
        .input = ADC_INPUT_SE_TEMP_SENS,
    };

    adc_init(&adc_cfg);
    temp = adc_get_temp();
    adc_disable();

    return temp;
}

Note

The adc_get_temp function reads the sensor, applies the single point calibration value stored in OTP, and returns a temperature value in degrees centigrade.

We will now start a software timer that will periodically read the temperature using the above function and output it via the serial debug port.

6.1.3. Starting the Timer

Add the following prototype and variable declarations to the user_empty_peripheral_template.c file just before the user_on_init function:

static void timer_cb(void);
static int8_t temp_sensor_read(void);
static timer_hnd timer_id __attribute__((section("retention_mem_area0"),zero_init));

To start the timer add the following code into the user_on_set_dev_config_complete function contained within user_empty_peripheral_template.c:

/* Start timer - period has units of 10ms i.e. 200 = 2000ms */
timer_id = app_easy_timer(200, timer_cb);

Note

Because the kernel is not yet running when the user_on_init callback occurs the timer cannot be started in that function.

Finally implement the callback that will be executed when the timer expires. To do this add the following function to the end of the user_empty_peripheral_template.c file:

static void timer_cb(void)
{
    /* Measure the temperature using the GPADC */
    int8_t temp_c = temp_sensor_read();

    arch_printf("\n\rTemperature: %dC", temp_c);

    /* Restart the timer */
    timer_id = app_easy_timer(200, timer_cb);
}

Now build the project and load it onto your target. When it is executed the measured temperature will be output via the serial debug port every 2 seconds:

_images/temp_output.png

Figure 9 Serial debug output on Temperature Measurement

If you place your thumb or finger onto the top of the DA14531 device you should see the temperature rise a few degrees.