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.

configuring 8bit or 14bit adc of cc2530

Other Parts Discussed in Thread: CC2530, Z-STACK

i am using the sample adc program it is used to get the 14bit adc value and it truncate the 14bit value by discarding some bits into an 8bit value and displays it ... but i needed to get its output as 14bit itself !!! Any ideas on how i can do that .. i already tried taking the truncation pgm down and all nono of them are working

  • If you intend to discard some bits, why don't you use 8 bit ADC reading directly?
  • okey sir i am planning to connect adc instead of internal temp sensor ... how to achieve that sir
  • You can connect your adc input to any pin on port0.
    The followings are my test code for reading ADC from P0.2 with AVDD (3.3V on my SmartRF05EB) as reference and I have no problem reading input voltage from 0 to 3.3V range.

    uint16 adc_ain2;

    HalAdcSetReference(HAL_ADC_REF_AVDD);
    adc_ain2=HalAdcRead(HAL_ADC_CHN_AIN2,HAL_ADC_RESOLUTION_8);

    You can connect your temperature sensor output to P0.2 and adc_ain2 is the ADC reading.
  • AdcValue = 0;
    for( i = 0; i < 4; i++ )
    {
    ADC_SINGLE_CONVERSION(ADC_REF_1_25_V | HAL_ADC_DEC_064 | ADC_TEMP_SENS); // 使用1.25V内部电压,12位分辨率,AD源为:温度传感器
    ADC_SAMPLE_SINGLE(); //开启单通道ADC
    while(!ADC_SAMPLE_READY()); //等待AD转换完成
    value = ADCL >> 2; //ADCL寄存器低2位无效
    value |= (((UINT16)ADCH) << 6);
    AdcValue += value; //AdcValue被赋值为4次AD值之和
    }
    value = AdcValue >> 2; //累加除以4,得到平均值
    value1 = AdcValue - 335 ;
    return value1 ; //根据AD值,计算出实际的温度
    This is the pgm i am using sir .... instead of ADC_TEMP_SENS i have to use the potentiomenter .. how can i do that sir ???
    now it is giving very random values ....
  • I have given you reference code in my previous post. Why do you insist using yours?
  • YES SIR ... but i can use that code if i am using zstack .. here i am using 1ly the demoprograms for cc2530
  • What do you mean you are using 1ly the demoprograms for cc2530?
  • /******************************************************************************
    XWWK-CC2530A模块AD测试程序
    Filename: CC2530AD1Test.c
    Target: cc2530
    Revised: 2012-01
    Revision: 1.0
    Description:
    通过内置温度传感器实现温度采集
    ******************************************************************************/

    #include "ioCC2530.h"
    #include "initUART_Timer.h"
    #include "stdio.h"

    #define HAL_ADC_REF_AVDD 0X80
    #define HAL_ADC_DEC_064 0X00
    #define HAL_ADC_CHN_AIN7 0X07

    INT16 AvgTemp;

    /****************************************************************
    温度传感器初始化函数
    ****************************************************************/
    void initTempSensor(void){
    DISABLE_ALL_INTERRUPTS(); //关闭所有中断
    InitClock(); //设置系统主时钟为32M
    *((BYTE __xdata*) 0x624B) = 0x01; //开启温度传感器
    *((BYTE __xdata*) 0x61BD) = 0x01; //将温度传感器与ADC连接起来
    }
    /****************************************************************
    读取温度传感器AD值函数
    ****************************************************************/
    INT8 getTemperature(void){
    UINT8 i;
    UINT16 AdcValue;
    UINT8 value;
    UINT8 value1;

    //AdcValue = 0;
    //for( i = 0; i < 4; i++ )
    //{
    ADC_SINGLE_CONVERSION(HAL_ADC_REF_AVDD | HAL_ADC_DEC_064 | HAL_ADC_CHN_AIN7); // 使用1.25V内部电压,12位分辨率,AD源为:温度传感器
    ADC_SAMPLE_SINGLE(); //开启单通道ADC
    while(!ADC_SAMPLE_READY()); //等待AD转换完成
    value = ADCL >> 2; //ADCL寄存器低2位无效
    value |= (((UINT16)ADCH) << 6);
    AdcValue += value; //AdcValue被赋值为4次AD值之和
    //}
    //value = AdcValue >> 2; //累加除以4,得到平均值
    //value1 = AdcValue - 335 ;
    return AdcValue ; //根据AD值,计算出实际的温度
    }
    /****************************************************************
    主函数
    ****************************************************************/
    void main(void)
    {
    char i;
    char TempValue[10];

    InitUART0(); //初始化串口
    initTempSensor(); //初始化ADC
    while(1)
    {
    //AvgTemp = 0;
    //for(i = 0 ; i < 64 ; i++)
    //{
    AvgTemp = getTemperature();
    // AvgTemp >>= 1; //每次累加后除2.
    // }

    sprintf(TempValue, (char *)"%dC ", (INT8)AvgTemp);
    UartTX_Send_String(TempValue,4);
    Delay(50000);
    }
    }
    using this pgm i am trying to display the value in the variable adcvalue via hyperterminal but it keeps on varying ... it is not steady sir
  • From your previous post, you said you use P0.2 as ADC input but it uses HAL_ADC_CHN_AIN7 which is P0.7 as ADC input. If you use P0.2, you should use HAL_ADC_CHN_AIN2.
  • both of the posts are different sir .. that post is for interfacing external sensor which make use of zigbee stack because it should be transmitt the data from ZED to ZC
    this post is about configuring the 8bit adc of cc2530 using the input via potentiometer . and displaying the same using uart in hyper terminal
  • I don't know what is inside your ADC_SINGLE_CONVERSION, ADC_SAMPLE_SINGLE and ADC_SAMPLE_READY. I suggest you using APIs in hal_adc.c. Since those ADC API implementations are nothing to do with Z-Stack, you can copy them and use in your program directly.
  • #define ADC_SINGLE_CONVERSION(settings) \
    do{ ADCCON3 = (settings); }while(0)

    #define ADC_SAMPLE_SINGLE() \
    do { ADC_STOP(); ADCCON1 |= 0x40; } while (0)

    #define ADC_SAMPLE_READY() (ADCCON1 & 0x80)

    #define ADC_STOP() \
    do { ADCCON1 |= 0x30; } while (0)
  • hi sir .. 

    i did the coding as u said but yet it is not at all giving a stabilized value sir !!   

  • This variation seems fine and it might be cause by AVDD variation.
  • ok sir ... but the value when i get lowest value in pot is 0 and for the highest value it is giving 0 .... the inbetween values are oscillating between positive and negative values .... that was the problem .. ???
  • for 8 bit adc the minimum value is 0 and maximum 255 shouldnt it be the values to be displayed ... but for my pgm the maximum and minimum is giving a value of 0 only sir ... any ideas y ???
  • For 8 bit adc, the maximum is 127 because it is in 2's complement format.
  • ok sir .... that means the code is wrong i think .

    static UINT8 getTemperature (void) {
    UINT8 i;
    UINT8 value;
    UINT8 adc_ain2;

    for (i = 0; i <4; i ++)
    {
    HalAdcSetReference (HAL_ADC_REF_AVDD);
    adc_ain2 = HalAdcRead (HAL_ADC_CHN_AIN7, HAL_ADC_DEC_064);
    // value = adc_ain2 & 0x00ff;
    }
    return adc_ain2;
    }
    void main (void)
    {
    int i;
    char TempValue [10];
    InitUART0 (); // initialize the serial port
    initTempSensor (); // initialize the ADC
    while (1)
    {
    ;
    for(i = 0 ; i < 64 ; i++)
    {
    AvgTemp = getTemperature();
    AvgTemp >>= 1; //After each addition to two cumulative.
    AvgTemp = AvgTemp / 2;
    }
    UartTX_Send_String (temp, 1);
    sprintf (TempValue, (char *) "% d ", AvgTemp);
    UartTX_Send_String (TempValue, 4);
    Delay (50000);
    }
    }

    this code is giving a minimum of 0 and a maximum of 63 sir and inbetween random numbers between 0 to 63 are comming sir !! how to rectify this ??
  • Please refer to ADC section in CC2530 user guide and you would see ADC reading is in 2's complement format. This is nothing to do with bits accuracy.
  • so u are saying is if the value is 255 the adc output will be 1 (ie 1111 1111 will change to 0000 0001
    and if the value is 0 means the output will be 0 (ie 0000 0000 will change to 1 0000 0000 )
  • For 2's complement, you can refer to here.

  • hi sir !! 

    the adc i configured is giving values from 0 to 63 and again o to 63 sir .. what can i do to eliminate that ???

    i mean for minimum value of pot it is giving 0 and for maximum value it is giving 63 but the in between variation is like 

    (minimum)0,1,2,3,..........61,62,62,0,1,2,3 ......... 61,61,63(maximum)

  • any comments sir !!
  • I don't understand your current problem well. Please describe it step by step.
  • i am configuring the cc2530dk board by in the way that i can connect its serial port to the pc and view the data via hyperterminal , i used the potentiometer of the board . on the whole i wish to display the value from the potentiometer in the hyper terminal . as u suggested i wrote the code for 8bit ADC 

    but the problem is

    1) the reading should be like from 0 to 63 or 0 to 127 when moving the potentiometer from minimum to maximum .

    2) but instead of 1) i am getting values like 0,1,2,3,.....61,62,63,0,1,2,......,61,62,63 .. what i mean is i am getting two sets of values 0 to 63 again another 0 to 63 in between the values potentiometer minimum to maximum 

  • 1. It should be 0 to 127.
    2. When you see the reading goes from 0 to 63, what is the reading on potentiometer .
  • but it is showing as like that only sir .. 0 to 127 is not at all displaying .what do u meant by " what is the reading on potentiometer" . what am i doing wrong sir ??
  • What is the input voltage on your ADC pin when your UART shows 63?

  • 3.275v i am getting at maximum value of the potentiometer ...
    112.4 mv for minimum value of the potentiometer sir ..
  • Can you take a picture of your HW?
  • the rounded part is the potentiometer which i used sir .. kindly see to it 

  • it is the normal cc2530dk board and i am using ist inbuilt potentiometer sir
  • Can you try to use power supply to provide the input voltage of ADC pin?
  • i didnt get u sir !! i mean i didnt get what u mentioned as power supply to the adc pin ...
  • is the code i used is rite sir ???

    INT8 getTemperature (void) {
    UINT8 i;
    //UINT8 value;
    //UINT8 value1;
    UINT8 adc_ain2;

    for (i = 0; i <4; i ++)
    {
    HalAdcSetReference (HAL_ADC_REF_AVDD);
    adc_ain2 = HalAdcRead (HAL_ADC_CHN_AIN7, HAL_ADC_DEC_064);
    // value = adc_ain2 & 0x00ff;
    // value = adc_ain2 - 1 ;
    // value1 = ~(value) ;
    }
    return adc_ain2;
    }
    void main (void)
    {
    int i;
    char TempValue [10];
    InitUART0 (); // initialize the serial port
    initTempSensor (); // initialize the ADC
    while (1)
    {
    for(i = 0 ; i < 255 ; i++)
    {
    AvgTemp = getTemperature();
    AvgTemp >>= 1; //After each addition to two cumulative.
    AvgTemp = AvgTemp / 2;
    }
    UartTX_Send_String (temp, 1);
    sprintf (TempValue, (char *) "% d ", AvgTemp);
    UartTX_Send_String (TempValue, 4);
    Delay (50000);
    }
    }
  • If you replace your following code
    for(i = 0 ; i < 255 ; i++)
    {
    AvgTemp = getTemperature();
    AvgTemp >>= 1; //After each addition to two cumulative.
    AvgTemp = AvgTemp / 2;
    }

    with
    AvgTemp = getTemperature();

    Do you still get MAX reading 63?
  • yes sir !! i tried that too .. but the values are 0(minimum value) to 255(maximum) and fluctuating like 0 to 255 , 0 to 255 , 0 to 255 etc .....
  • Why is "adc_ain2 = HalAdcRead (HAL_ADC_CHN_AIN7, HAL_ADC_DEC_064)"? It should be "adc_ain2 = HalAdcRead (HAL_ADC_CHN_AIN7, HAL_ADC_RESOLUTION_8)" if you intend to use 8 bit ADC resolution.

  • would u pl tell me the difference between HAL_ADC_DEC_064 and HAL_ADC_RESOLUTION_8 sir ??
    i used HAL_ADC_DEC_064 for many programs so confused
  • You can check the source code in HalAdcRead and will see the difference. HAL_ADC_RESOLUTION_8 is used for parameter of HalAdcRead and HAL_ADC_DEC_064 is used for configuring register ADCCON3.

  • it worked sir !! thank u ... when i give 10 bit resolution it is repeating the same sir .... i mean 0to255 again a value of 0 to 255 between the minimum and maximum of potentiometer
  • It is good to see your ADC finally works.