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.

Read analog value CC2540 question

Other Parts Discussed in Thread: CC2540

Dear experts.

I am trying to read a value from a CC2540 on an OLP425 board.

Pin numbering here:
http://support.connectblue.com/display/PRODBTSPA/cB-OLx425+Electrical+Mechanical+Data+Sheet#cB-OLx425ElectricalMechanicalDataSheet-PinNumbering

I have attached an external sensor to analog input 31, which in the CC2540 should equal to pin 19 / port name P0_0.

Reading through posts, it seems I have to use the simpleGATT profile example, which I've got running, but I dont know how to read the analog pin values.

So my question is exactly that: How do I read the pin 19, port P0_0 value?

Thanks in advance :)

  • Hi Jesper,

    Given that the pin numbering is correct, then you need to set up p0.0 as analog, and set up the adc peripheral module. Please refer to the CC2540/1 User Guide http://www.ti.com/lit/swru191, chapter 7, esp 7.6.6 and 7.11 for pin configuration and chapter 12 for ADC operation.

    If you want to, you could also use the ADC driver included in the stack. The keyfobdemo uses this for the battery monitoring. You will need to define HAL_ADC under Project->Options->C/C++Compiler->Preprocessor->Defined symbols. You will also need to include hal_adc.h where you want to use the adc in the code, and you will need to set up the pin for analog function manually. Take a look at hal_adc.c, found inside IAR under HAL/Target/CC2540EB/Drivers.

    Best regards,
    Aslak 

  • Hi Aslak, thanks for the fast reply :)

    I have tried to do the following as an extention to the simpleGATTprofile example file:

    #define cbADC_PORT        (APCFG)
    ...
    cbADC_PORT = 1; // enable analog read?
    ...
    HalAdcSetReference( HAL_ADC_REF_AVDD );
    uint16 adc;
    adc = HalAdcRead( HAL_ADC_CHN_AIN0, HAL_ADC_RESOLUTION_10 );

    and then i return the adc variable, but i get bogus values, jumping up and down where the external sensor (in this case a thermistor), is expected to go in one direction only as i heat it up.

    Any idea where i might be wrong?

  • If Aslak isn't available, can anyone else provide me with some constructive feedback for my problem? I'm kind of stuck, and I have browsed the forum for answers, where I have found a lot of similar problems but trying out peoples solutions have not solved my problem unfortunately. I do survey the thermistor with a multimeter, to see if it correctly falls in value as the temperature increases, which is the case, so the problem shouldn't be there unless I am missing something

  • Hi Jesper,

    Jesper Hoff said:
    i get bogus values, jumping up and down where the external sensor (in this case a thermistor), is expected to go in one direction only as i heat it up.

    Sounds like the 2 Byte reading might be in wrong order (or used in wrong order). Could you post some actual read values and some you'd typically expect.

    Best regards.

  • Hi Nick. Thank you so much for replying :)

    For the sake of avoiding problems with multiple bytes / byte ordering, I have changed the code to only return the MSB using bitshifting. Im still using the very little modified version of simpleGATTprofile.c:

    in simpleProfile_ReadAttrCB:

    ....
    APCFG = 0x01;
     P0DIR = 0x01;
    HalAdcInit();

    HalAdcSetReference( HAL_ADC_REF_AVDD );

    uint16 adc;

    adc = HalAdcRead( HAL_ADC_CHANNEL_0, HAL_ADC_RESOLUTION_12 );

    uint8 adc8bit = (uint8)(adc & 0xFF);

    ....

    pValue[0] = adc8bit;

    If you want, I'll gladly post the entire .c file (its a bit messy though).

    Anyway regarding actual read values and expected ones;

    At the thermistors room temperature, I get a reading of ca. 9.95Kohm. The value i can read from btool is anywhere from 120 to 150 (decimal).

    Heating up the thermistor, causing it to fall to 8Kohm, still produces the same numbering range. The absolutely wierdest part is: If i Squeeze the termistor, the values increase, but the resistance of the termistor doesnt change. 

    I have no clue what is going on. Any ideas?

  • Considering my lacking knowledge of this subject, maybe a better approach to get it to work, would be if you told me the minimal steps you would undergo to read an analog value from pin P0_0 ? :)

  • Hi Jesper,

    How have you wired the thermistor? If you use a voltmeter between the pin input and ground, do you see the expected value changes?

    Aslak

  • At first I wired the thermistor directly between the +3V pin and the analog in pin.

    Then since that didn't work, i now tried doing something like this:

    http://arduino.cc/playground/uploads/ComponentLib/simple10ktherm_schem_new.png

    Didnt work for me either, now I only get very low values. 

    When i use a voltmeter between the input pin and ground, i get nothing, yet the resistor values do change according to temperature. Do you think i have a short circuit or something?

    Also can you verify that the code part is correct? Or perhaps provide me with a small example to work from? Then at the very least, I can redo the soldering and not worry about that part.

  • Hi,

    I'm sorry, I don't have the time to test the setup right now. But I would advise you to consider the range of the thermistor with regards to the dividing resistor.

    Try this out on a breadboard or similar, and watch with a voltmeter.

    You can experiment a bit on the link below, where I have set up a sample circuit.

    http://www.falstad.com/circuit/#%24+1+5.0E-6+10.20027730826997+50+5.0+50%0Ap+416+208+416+304+0%0A172+288+144+224+144+0+6+3.0+5.0+0.0+0.0+0.5+Voltage%0Ag+304+304+304+352+0%0Ar+304+304+304+224+0+10000.0%0A174+304+208+304+80+0+10000.0+0.9950000000000001+Resistance%0Aw+304+208+416+208+0%0Aw+304+304+416+304+0%0Aw+304+208+304+224+0%0Ao+0+64+0+34+5.0+9.765625E-5+0+-1%0A

    Or if that doesn't work, go to falstad.com/circuit and import the following circuit:

    $ 1 5.0E-6 10.20027730826997 50 5.0 50
    p 432 224 432 320 0
    172 304 160 240 160 0 6 3.0 5.0 0.0 0.0 0.5 Voltage
    g 320 320 320 368 0
    r 320 320 320 240 0 10000.0
    174 320 224 320 96 0 10000.0 0.9950000000000001 Resistance
    w 320 224 432 224 0
    w 320 320 432 320 0
    w 320 224 320 240 0
    o 0 64 0 34 5.0 9.765625E-5 0 -1

    Best regards,
    Aslak 

  • Thank you so much for all your help! I managed to figure out what the problem was:

    The main problem was not knowing what flags to set: I never got any power through, because the +3V output pin was never set.

    After setting the +3V pin direction to out, and enabling it right before i read it, I now get the proper values! :)

    For anyone who might get stuck like I was, the relevant code is here:

    // during initialization code:
    APCFG |= 0x01;
    P0SEL |= 0x01;
    P0DIR &= 0xFE; // Force a 0 on P0_0 which i use as analog in, making it an input pin.
    P1DIR |= 0x01; // Force a 1 on P1_0 which is the +3V, making it an output pin

    HalAdcInit();

    And this reads the pin value:

    // during read
    HalAdcSetReference( HAL_ADC_REF_AVDD );
    uint16 adc;

    P1_0 = 1; // enable +3V
    adc = HalAdcRead( HAL_ADC_CHN_AIN0, HAL_ADC_RESOLUTION_12 );
    P1_0 = 0; // disable +3V again to save power

    Again, great support, I really appreciate all the help and the fast responses :)

  • Hi Jesper

    I know this was a couple years ago, but I am trying to do the same thing. Where did you place this code? I am having trouble!

    Or can anyone else help me I am using Connect Blue OLP425 and want to read an analog input on port p0_0 and power from a 3V pin. Basically I am creating a voltage divider.

    Thanks for your help!!!