Hi, I programmed nRF52 DK with BLE_APP_UART_SAADC_TIMER_DRIVEN with AIN6 pin as input and is working correctly. But when I programmed the same code in BMD300 the adc count value is not coming as expected! It looks like an offset of 43-45 is coming always when the pin is left unconnected. The count returns a value of 110-114(whereas it is 1023 in nRF52DK) when connected to VDD. I've also tried reading the same pin by making all the surrounding pins low. But same result.
#define SAADC_SAMPLES_IN_BUFFER 1
#define SAADC_SAMPLE_RATE_DIVIDER 0x2000;
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
uint16_t adc_value;
uint8_t value[SAADC_SAMPLES_IN_BUFFER*2];
uint8_t bytes_to_send;
// set buffers
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
// print samples on hardware UART and parse data for BLE transmission
// printf("ADC event number: %d\r\n",(int)m_adc_evt_counter);
for (int i = 0; i < SAADC_SAMPLES_IN_BUFFER; i++)
{
// printf("%d\r\n", p_event->data.done.p_buffer[i]);
adc_value = (uint16_t)p_event->data.done.p_buffer[i];
// value[i*2] = adc_value;
// value[(i*2)+1] = adc_value >> 8;
}
adc_count_temp = adc_value;
sprintf(value,"M=%d",adc_count_temp); // prints current voltage
err_code = ble_nus_string_send(&m_nus, value, sizeof(value));
// sprintf(value,"c=%d",adc_value);
adc_flag=1;
// Send data over BLE via NUS service. Makes sure not to send more than 20 bytes.
if((SAADC_SAMPLES_IN_BUFFER*2) <= 20)
{
bytes_to_send = (SAADC_SAMPLES_IN_BUFFER*2);
}
else
{
bytes_to_send = 20;
}
// err_code = ble_nus_string_send(&m_nus, value, 10);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
m_adc_evt_counter++;
}
}
void saadc_init(void)
{
ret_code_t err_code;
nrf_saadc_channel_config_t channel_config =
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6);
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(6, &channel_config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0],SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1],SAADC_SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
}
I have made this change as well.
#define NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P) \
{ \
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
.gain = NRF_SAADC_GAIN1_4, \
.reference = SAADC_CH_CONFIG_REFSEL_VDD1_4, \
.acq_time = NRF_SAADC_ACQTIME_10US, \
.mode = NRF_SAADC_MODE_SINGLE_ENDED, \
.pin_p = (nrf_saadc_input_t)(PIN_P), \
.pin_n = NRF_SAADC_INPUT_DISABLED \
}
This is the main function
int main(void)
{
uint32_t err_code;
bool erase_bonds;
// Initialize.
nrf_gpio_cfg_output(9);
nrf_gpio_cfg_output(10);
nrf_gpio_cfg_output(12);
nrf_gpio_cfg_output(13);
nrf_gpio_pin_clear(9);
nrf_gpio_pin_clear(10);
nrf_gpio_pin_clear(12);
nrf_gpio_pin_clear(13);
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
uart_init();
gpio_init();
buttons_leds_init(&erase_bonds);
ble_stack_init();
gap_params_init();
services_init();
advertising_init();
conn_params_init();
saadc_sampling_event_init();
saadc_init();
saadc_sampling_event_enable();
printf("\r\nUART Start!\r\n");
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
uint8_t c[15];
// Enter main loop.
for (;;)
{
power_manage();
}
}
In the hardware side nothing is connected to the pin and yes I am testing with AIN6 (P0.30) or Pin 11 of BMD300. Is the settings correct? Or am I missing something in the code?