This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

problem in analog read.........?

hai

i am using mbed to program my nrf51822. in my application i need to read two analog sensor values. i am using p5 and p6 pin for that.

 AnalogIn   analog_pin_1(p5);
 AnalogIn   analog_pin_2(p6);

now i have a periodic call back attached to a Ticker and the call back will execute in every second

ticker.attach(periodicCallback, 1);

and the analog values are read inside this periodic callbcak. and this values are converted into percentage using a custom procedure. then the percentage value will be added into the service data of my ble.

void periodicCallback(void)
 {
  service_data[4] = calculate_percentage(analog_pin_1.read_u16());
  service_data[5] = calculate_percentage(analog_pin_2.read_u16());
  printf(" percentage_1 : %d and perncentage_2 : %d \n\r",service_data[4],service_data[5]);
  update_service_data();
}

void update_service_data(){
    ble.gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA,service_data, sizeof(service_data));
    }

now every thing works fine. and my service data is updated fine.

image description

but if i comment the printf statement or close UART to reduce power consumption the analog value is not updating as expected , that is the first analog read is not correct and the secons read is correct as before.

void periodicCallback(void)
 {
  service_data[4] = calculate_percentage(analog_pin_1.read_u16());
  service_data[5] = calculate_percentage(analog_pin_2.read_u16());
  //printf(" percentage_1 : %d and perncentage_2 : %d \n\r",service_data[4],service_data[5]);
  update_service_data();
}

OR

NRF_UART0->TASKS_STOPTX = 1;
NRF_UART0->TASKS_STOPRX = 1;
NRF_UART0->ENABLE = 0;

so how can i correct my analog read same in both situation that is with and without UART

Related