Hello
I want to read the values of two potentiometers from the Ain0 pin and the Ain1 pin through SAADC.
I could easily read one value through the saadc example, but I don't know how to add more.
APP_PWM_INSTANCE(PWM1,1);
#define SAMPLES_IN_BUFFER 5
volatile uint8_t state = 1;
static volatile bool ready_flag; // A flag indicating PWM status.
static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);
//static const nrf_drv_timer_t m_timer2 = NRF_DRV_TIMER_INSTANCE(1); //add
static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t m_ppi_channel;
static uint32_t m_adc_evt_counter;
void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
{
ready_flag = true;
}
void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
}
#define ADC_TIME 100
void saadc_sampling_event_init(void)
{
ret_code_t err_code;
err_code = nrf_drv_ppi_init();
APP_ERROR_CHECK(err_code);
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler);
APP_ERROR_CHECK(err_code);
/* setup m_timer for compare event every 400ms */
uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, ADC_TIME);
nrf_drv_timer_extended_compare(&m_timer,
NRF_TIMER_CC_CHANNEL0,
ticks,
NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
false);
nrf_drv_timer_enable(&m_timer);
uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer,
NRF_TIMER_CC_CHANNEL0);
uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get();
/* setup ppi channel so that timer compare event is triggering sample task in SAADC */
err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
timer_compare_event_addr,
saadc_sample_task_addr);
APP_ERROR_CHECK(err_code);
}
void saadc_sampling_event_enable(void)
{
ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);
APP_ERROR_CHECK(err_code);
}
uint16_t pwm_period;
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
int16_t adc_value = 0; //0 ~ 550
int16_t adc_value2 = 0; //0 ~ 550
int16_t pwm_value = 0; //35 ~ 55kHz
int16_t duty_value = 0; // 0 ~ 100
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
ret_code_t err_code;
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
int i;
//NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);
for (i = 0; i < SAMPLES_IN_BUFFER; i++)//output 5times
{
//NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);
adc_value += p_event->data.done.p_buffer[i]; //adc adverage
}
m_adc_evt_counter++;
adc_value /= SAMPLES_IN_BUFFER; //abc adverage
NRF_LOG_INFO("ADC1 value : %d", (int)adc_value); //OK
NRF_LOG_INFO("ADC2 value : %d", (int)adc_value2); //Fail
}
pwm_value = adc_value * 10 / 550 + 18; //map() : (adc_value - 0) * (28 - 18) / (550 - 0) + 18, (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
//pwm period : 18~28
/*if(pwm_value > 28)
{
pwm_value = 28;
}
else if(pwm_value < 18)
{
pwm_value = 18;
}*/
pwm_period = pwm_value;
NRF_LOG_INFO("PWM period : %d", (int)pwm_period);
}
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_AIN0);
nrf_saadc_channel_config_t channel_config2 = //add
NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
err_code = nrf_drv_saadc_init(NULL, saadc_callback);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_channel_init(1, &channel_config2); //add
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
APP_ERROR_CHECK(err_code);
}
Can I get some advice on this?
Thank you.
=================================================================
(Edit)
The value of adc is outputting strangely.
Reading the adc value of the potentiometer, the value is printed as shown below.
<info> app: ADC1 value : 470 <info> app: ADC1 value : 388 <info> app: ADC1 value : 469 <info> app: ADC1 value : 387 <info> app: ADC1 value : 467 <info> app: ADC1 value : 388 <info> app: ADC1 value : 467 <info> app: ADC1 value : 387 <info> app: ADC1 value : 470 <info> app: ADC1 value : 387
Like this, the adc value has up and down repeatedly. I am calculating the pwm period using the value of adc. But because of this problem, the pwm is output strangely.
And the potentiometer ranges from about 0 to 600. Is this the right value?
Thank you.