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