Wednesday, November 26, 2014

How to do temperature monitoring using CC2530 with TI Z-Stack

The battery monitor can also be used to do some simple temperature monitoring in CC2530. When the battery monitor is connected to the internal temperature sensor instead of the supply voltage AVDD5.

The following readTemperature function provides capability to read temperature using CC2530 internal ADC and sensor. When first time call this function, you have to keep temperature at 22oC for calibration.

int8 readTemperature(void)
{
  static uint16 voltageAtTemp22;
  static uint8 bCalibrate=TRUE; // Calibrate the first time the temp sensor is read
  uint16 value;
  int8 temp;

  ATEST = 0x01;
  TR0  |= 0x01;
 
  /* Clear ADC interrupt flag */
  ADCIF = 0;

  ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_512 | HAL_ADC_CHN_TEMP);

  /* Wait for the conversion to finish */
  while ( !ADCIF );

  /* Get the result */
  value = ADCL;
  value |= ((uint16) ADCH) << 8;

  // Use the 12 MSB of adcValue
  value >>= 4;
 
  /*
   * These parameters are typical values and need to be calibrated
   * See the datasheet for the appropriate chip for more details
   * also, the math below may not be very accurate
   */
    /* Assume ADC = 1480 at 25C and ADC = 4/C */
  #define VOLTAGE_AT_TEMP_25        1480
  #define TEMP_COEFFICIENT          4

  // Calibrate for 22C the first time the temp sensor is read.
  // This will assume that the demo is started up in temperature of 22C
  if(bCalibrate) {
    voltageAtTemp22=value;
    bCalibrate=FALSE;
  }
 
  temp = 22 + ( (value - voltageAtTemp22) / TEMP_COEFFICIENT );
 
  // Set 0C as minimum temperature, and 100C as max
  if( temp >= 100)
  {
    return 100;
  }
  else if (temp <= 0) {
    return 0;
  }
  else {
    return temp;
  }
}

No comments:

Post a Comment