Q: FAQ: Reading the temperature sensor of the CC2430 from Z-stack application (ZigBee)

A user can add the following code snippets to their application to read the temperature sensor on the CC2430. This is based on a modification of the sample application GenericApp. Refer to the application note shipped with the Z-stack called AN 012 – Create New Application to create a new application called “TempSensor”, prior to adding the code snippets below. Some familiarity with the TI Zigbee Dev Kits is assumed.

#include "iocc2430.h"


float getTemp( void );
/*********************************************************************
 * MACROS
 */

#define ADC14_TO_CELSIUS(ADC_VALUE) \
  ( (float)ADC_VALUE * (float)GRADIENT_C + OFFSET_C )

#define ADC14_TO_FARENHEIT(ADC_VALUE) \
  ( (float)ADC_VALUE * (float)GRADIENT_F + OFFSET_F )

#define SAMPLE_TEMP_SENSOR(v)      \
  do{                                                      \
      ADCCON2 = 0x3E;                          \
      ADCCON1 = 0x73;                           \
      while(!(ADCCON1 & 0x80));              \
      v = ADCL;                                       \
      v |= ( ( (unsigned int)ADCH) << 8 );    \
     } while (0)

/*********************************************************************
 * TYPEDEFS
 */

/* OFFSET_C must be calculated/measured for each individual chip */
#define GRADIENT_C    0.01496
#define GRADIENT_F    (9.0/5.0 * GRADIENT_C)
#define OFFSET_C      -296
#define OFFSET_F      (9.0/5.0 * OFFSET_C) + 32


Add code to print out received temp value to LCD

void TempSensor_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
  switch ( pkt->clusterId )
  {
    case TEMPSENSOR_CLUSTERIDY:
      // "the" message

#if defined( LCD_SUPPORTED )

    HalWriteLCDScreen("Temperature:", (char*)pkt->cmd.Data);

#elif defined( WIN32 )
      WPRINTSTR( pkt->cmd.Data );
#endif
      break;
  }
}


Add the following code to TempSensor_SendTheMessage()

    unsigned char buf[16];
    float temperature;
    temperature = getTemp();

     if (temperature < 0)
    {
      // insert minus sign and find the ABS value
      buf[0] = '-';
      temperature *= -1;
    }
    else
    {
      buf[0] = ' ';
    }

    buf[1] = (unsigned char) temperature / 10 + '0';
    if(buf[1] == '0') buf[1] = ' ';

    buf[2] = (unsigned char) temperature % 10 + '0';

    buf[3] = '.';

    // remove integer part of temperature
    temperature -= (unsigned char)temperature;
    // multiply with 10
    temperature *= 10;
    buf[4] = (unsigned char) temperature + '0';

    buf[5] = ‘\0’;


Add this to the end of TempSensor.c

float getTemp(void)
{
  unsigned int value;
  SAMPLE_TEMP_SENSOR(value);
  return ADC14_TO_CELSIUS(value);
  // return ADC14_TO_FARENHEIT(value);
}


Instructions for testing

     •     Build a CoordinatorEB and download to SmartRF04EB board
     •     Build a RouterDB and download to CC2430DB board
     •     Press SW4 (joystick to the left) on the RouterDB board to bind the temp sensor
     •     Temperature readings will display on the CoordinatorEB LCD screen