ADC conversion at specific intervals - is there a better way to do this?
While trying to figure out how to trigger multiple ADC conversions every 100ms, I came up with what is listed below. This works but I wanted to know if there is a better way to do this? Also do all selected channels get monitored for conversion at the end of each 100ms period or does each single channel conversion happen every 100ms?
Any inputs would be appreciated!
TIM2 clock is at 84MHz (APB1)
PSC = 8400 which brings it down to 10Khz
ARR = 1000 which brings it down to 10Hz
Under TRGO parameters Master/Slave is Enabled and Trigger event selection is set to Timer 2 Update Event.
Under ADC1, clock is PCLK2/4 (84Mhz/4=21Mhz). Channels 0 and 1 are enabled. Resolution is 12 bits. Sampling time is 56 cycles (15 ADC cycles + 12 cycles as per the datasheet which works out to 27/21Mhz = 1.3us per sample). Scan conversion mode is Enabled. Continous conversion, DMA continous requests and Discontinous conversion modes are Disabled. Enternal trigger conversion source is set to Timer 2 Trigger Out with trigger detection on the rising edge.
Under DMA for ADC, the buffer is set to circular model with memory increment.
Under main.c this is the code,
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc1_values, ADC_BUF_SIZE) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_Base_Start_IT(&htim2) != HAL_OK)
{
Error_Handler();
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
HAL_ADC_Stop_DMA(&hadc1);
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2)
{
HAL_ADC_Start_DMA(&hadc1, (uint32_t*) adc1_values, ADC_BUF_SIZE);
}
}