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 test pin

Other Parts Discussed in Thread: CC2540

 

Dear experts,

I'm trying to add an analog sensor to the CC2540. In the Keyfob Reference Design I found that pin 1 and 3 correspond respectively to P0_6 and P0_7. I decided to use P0_6 as analog input and P0_7 to control the power feed of my sensor.

I realized my Hal_sensor.c and .h files containing the functions:

HalSensorInit( void ); // Initialize Sensor Service

HalSensorPwOn( void ); // Set the Sensor Power Supply to ON

HalSensorPwOff( void ); // Set the Sensor Power Supply to OFF

That actually contains macros defined in hal_board_cfg.h:

#define HAL_TURN_OFF_SENSOR_PW_SUPPLY()       st( SENSOR_PW_SBIT = SENSOR_PW_POLARITY (0); ) 

#define HAL_TURN_ON_SENSOR_PW_SUPPLY()       st( SENSOR_PW_SBIT = SENSOR_PW_POLARITY (1); )

In hal_board_cfg.h I declared:

#define SENSOR_PW_BV           BV(7)

#define SENSOR_PW_SBIT         P0_7

#define SENSOR_PW_DDR          P0DIR

#define SENSOR_PW_POLARITY     ACTIVE_HIGH

#define SENSOR_ADC_BV          BV(6)

#define SENSOR_ADC_SBIT        P0_6

#define SENSOR_ADC_DDR         P0DIR

#define SENSOR_ADC_POLARITY    ACTIVE_HIGH

and in the macro HAL_BOARD_INIT I added the following code:

SENSOR_PW_DDR |= SENSOR_PW_BV;                                     \

SENSOR_ADC_DDR &=~SENSOR_ADC_BV;                              \

Finally in SimpleBLEPeripheral.c, inside SimpleBLEPeripheral_Init I modified the port configuration:

  // For keyfob board set GPIO pins into a power-optimized state

  // Note that there is still some leakage current from the buzzer,

  // accelerometer, LEDs, and buttons on the PCB.  

  P0SEL = 0; // Configure Port 0 as GPIO

  P1SEL = 0; // Configure Port 1 as GPIO

  P2SEL = 0; // Configure Port 2 as GPIO

  // Set AIN6 as analog input

  APCFG = 0x40;                                                      

  /*

  P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons),

                // all others (P0.2-P0.7) as output

  */

  P0DIR = 0xBC; // Port 0 pins P0.0, P0.1 and P0.6 as input (buttons and sensor),

                // all others (P0.2-P0.5 and P0.7) as output

  P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output

  P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output

  P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)

  P1 = 0;   // All pins on port 1 to low

  P2 = 0;   // All pins on port 2 to low    

With the command HalAdcRead( HAL_ADC_CHN_AIN6, HAL_ADC_RESOLUTION_8 ) it looks like I can read the value given by my sensor. The problem I have is that I cannot turn my sensor's power off. If I measure the voltage of P0_7 I always find 3V (even if I use my macro HAL_TURN_OFF_SENSOR_PW_SUPPLY). Where am I wrong?

Many thanks!

Marco

 

 

 

 

  • Marco,

    I am attempting to do a similar application however without the need to power my sensor from the CC2540. I was wondering if you had made any headway at all on this? And if there was any information you could share with me to get me started off in the right direction?

    Thank you,

    Christopher Calvo

  • Do you know how much current your sensor draws? The output drive strength is 4 mA on all outputs, except for the two high-drive outputs, P1.0 and P1.1,
    which each have 20-mA output drive strength.

  • Christopher,

    I'm sorry if I didn't post my progress here, however my code works, so I think you can use it. At the end it was an HW problem.

  • Marco,

    By any chance is there a way you can send me your code so I can see how you went about getting the ADC to work?

    -Christopher

  • You can use the API given from Texas:

    //Initialize ADC with internal 1.25V set as reference

    HalAdcSetReference( HAL_ADC_REF_125V );

     // Read the ADC

    adc_read = HalAdcRead( HAL_ADC_CHN_AIN6, HAL_ADC_RESOLUTION_14 );

  • Marco,

    I took your advice and went ahead and tried to retro-fit some code together to at least get the pin P0_6 to be read in as an analog ADC value. I used the battery code in the keyfobdemo and changed it around as to read in P0_6 ADC analog value (adc_read) and if the value is less than 511 it sets batteryLevel to 50 however if the analog value (adc_read) is greater than 511 it sets batteryLevel to 100. The problem I having now is that no matter what I do the code always outputs 50 as the batteryLevel meaning that adc_read never gets to be greater than 511 which is simply impossible because I have P0_6 (which I believe is test pin 1) connected to a power supply outputting 1.25 V. What do you think I am doing wrong? Here is the code:

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //First of all this is the change of pin registers to set up AIN6 and AIN7 as analog inputs:

      P0SEL = 0xC0; // Configure Port 0 as GPIO
      P1SEL = 0x40; // Configure Port 1 as GPIO, except P1.6 for peripheral function for buzzer
      P2SEL = 0; // Configure Port 2 as GPIO

     // Set AIN6 as analog input
      APCFG = 0xC0;
      /*
      P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons),
                    // all others (P0.2-P0.7) as output
      */

      P0DIR = 0x3C; // Port 0 pins P0.0, P0.1 and P0.6  and P0.7 as input (buttons and sensor),
                    // all others (P0.2-P0.5 and P0.7) as output
      P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output
      P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output
     
      P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)
      P1 = 0;   // All pins on port 1 to low
      P2 = 0;   // All pins on port 2 to low 


      // initialize the ADC for battery reads
      HalAdcInit();

     

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // Lastly this is the code I edited within the checkBattery function:

    //Initialize ADC with internal 1.25V set as reference
      HalAdcSetReference( HAL_ADC_REF_125V );
     
      // Read the ADC
      adc_read = HalAdcRead( HAL_ADC_CHANNEL_6, HAL_ADC_RESOLUTION_12 );

    if( batteryLevel_current_read < batteryLevel )
        batteryLevel = batteryLevel_current_read;

      if( batteryLevel < BATTERY_LEVEL_CRITICAL_PCT )
        batteryState = BATTERY_STATE_CRITICAL_REPLACE_NOW;
      else
        batteryState = BATTERY_STATE_DISCHARGING;
     
      if( adc_read > (uint16)(511))
        batteryLevel = (uint8)(100);
      if( adc_read <= (uint16)(511))
        batteryLevel = (uint8)(50);

      // Update the Battery Profile
      Battery_SetParameter( BATTERY_ATTR_LEVEL, sizeof ( uint8 ), &batteryLevel );
      Battery_SetParameter( BATTERY_ATTR_STATE, sizeof ( uint8 ), &batteryState );

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

  • Hi, are you sure that you can read battery level from that pin?

  • Hi, Chris,

    I came cross similar problem with you. I use the battery service to read the analog voltage. It seems that the voltage I read is always lower than the MinBattLevel previously defined, which is impossible.

    Have figure it out?

    Thanks a lot

    Andy