This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2540 how to read the temperature sensor?

Other Parts Discussed in Thread: CC2540, CC2541, LM35

Hi!

I am in need of some help reading the temperature sensor of the CC2540 chip.
I'm using the development kit.

The code I'm currently trying is this (I think I found it in some forum thread or on the wiki):

#define SAMPLE_TEMP_SENSOR(v) \
do { \
 ADCCON2 = 0x3E; \
 ADCCON1 = 0x73; \
 while(!(ADCCON1 & 0x80)); \
 v = ADCL; \
 v |= (((unsigned int)ADCH) << 8); \
 } while(0)
 
#define TEMP_CONST 0.61065 // (1250 / 2047) 
#define TEMP_OFFSET_DATASHEET 750 
#define TEMP_OFFSET_MEASURED_AT_25_DEGREES_CELCIUS 29.75 
#define TEMP_OFFSET (TEMP_OFFSET_DATASHEET + TEMP_OFFSET_MEASURED_AT_25_DEGREES_CELCIUS) // 779.75 
#define TEMP_COEFF 4.5	
 
float getTemp(void){ 
    unsigned int adcValue; 
    float outputVoltage; 
    SAMPLE_TEMP_SENSOR(adcValue); 
 
    // Note that the conversion result always resides in MSB section of ADCH:ADCL 
    adcValue >>= 4; // Shift 4 due to 12 bits resolution 
    outputVoltage = adcValue * TEMP_CONST; 
 
    return ((outputVoltage - TEMP_OFFSET) / TEMP_COEFF); 
} 

I get a constant reading from this, and I am at loss.
I have looked at both the data sheet and the user guide - but I cannot figure this out. 

Any help, pointers or code would be very much appreciated!

  • To use ADC, I would suggest you using API HalAdcSetReference() and HalAdcRead() in hal_adc.c.

  • Thanks,

    I'm getting close correct values now, I'll update this post when I get it exactly right (for future reference for other newbies like me).

    This is my current getTemp:

    float getTemp(void){ 
        unsigned int adcValue; 
        float outputVoltage; 
     
        TR0 |= 0x01; // TR0 must be modified prior to ATEST
        ATEST |= 0x01; // otherwise P0_0 and P0_1 forced with 0
        HalAdcSetReference ( HAL_ADC_REF_125V );
        adcValue = HalAdcRead( HAL_ADC_CHN_TEMP, HAL_ADC_RESOLUTION_12 );
        TR0 &= ~0x01;
        ATEST &= ~0x01;
        
        outputVoltage = adcValue * TEMP_CONST; 
     
        return ((outputVoltage - TEMP_OFFSET) / TEMP_COEFF); 
    } 

     

  • The temperature sensor on the processor isn't calibrated and isn't consistent between devices. Also, the typical offset for the cc2541 is 1480 (from datasheet), not 750. The sample code you're using is for a different processor and I think the constants and coefficients are different.

    I've found that the following gives me something close to 1 degree C per count.  But take note, this hasn't been fully tested.

    You will have to add/subtract an offset to the result , different for different devices.  See what temperature the device reports, note what the actual temperature is, the difference between the two is your offset.  Note that the scaling factor (or lack of one) may produce errors, but it's close enough for my purposes.

    int8 temperature_2541( void )  //  retrieve temperature.
    {
        
        TR0 = 0x01;  //  Connect temperature sensor to ADC.
        ATEST |= 0x01;

        ADCCON3 = 0x3e;  //  Start the ADC conversion using the temperature sensor input.
        
        int timeout = 1000;
        
        while( !( ADCCON1 & 0x80 ) && --timeout );  // Poll the conversion ready flag.
        
        if( timeout == 0 )
            return 0xFF;
        else
        {
            //  Put the upper and lower bytes together.
            uint16 result = ( ( uint16 )ADCH << 8 );
            result |= ADCL;
            
            result >>= 4;  //  12 bit ADC, shift into position
            
            result -= 1480;  //  Offset at 25 degrees C, from the datasheet.  We ultimately only have 8 bits for temperature, so we need to get rid of the offset here.
            
            //  If we shift down 2 bits, we get something close to 1 count per degree.        
            result >>= 2;
            
            return ( int8) result;
        }
    }

  • Hi everyone, 

    I have two voltage output sensors (external  temperature and humidity). I have connected them to the CC2540 module. I attempting to read the output and convert it to temperature and humidity readings. 

    1. My question is Dan Nelson can i use the code upwards or it is for the internal sensor on the module. 

    2. If not is there formula to convert the ADC readings into humidity or temperature. 

    3. Can i do read two adc ADC0 and ADC1 at the same time or i have to give like a delay. In giving a delay is timers the best way since they are not that accurate. 

    Any input will be very much appreciated. My deadline is coming up really fast.

    Thank you.

    Joseph

  • Can you tell me how to connect temperature sensor to CC2540. I am doing a project with two RF05 EB attached to cc2540 EM. I dont know to interface sensor and dont know to program temperature sensor for low power bluetooth communication.

  • What temperature sensor do you use?
  • I am using LM35 temperature sensor as Analog output
  • Please see my answer in your other thread: e2e.ti.com/.../469207

    Best wishes
  • You can use API in hal_adc.c to do analog ADC reading.
  • I am doing project with CC2540 development kit. I have a central device and a peripheral device. i want to program for temperature sensor. How did you create this code and on what basics?

  • What temperature sensor do you use?