2018-10-19 11:20 AM
Hi ,
I am trying to store values from adc pin in an array but could not able to do. I have assigned pina0 as ADC input, configured adc to take the values every one second( counter period 1000), apb clock 16 MHz, prescaler in timer 16000. but i didnt understand why my values are not storing in an array every one second. to check if the function is called every second i have placed a variable 'i' and incremented after every passing of function. The values of i are updated very quickly than what i have requested. I am in an impression that this below interrupt will be called every second but its not really happening that way. See below basic code.
I can able to read the values from the adc pin though.
I would like store the inputs of adc every second and store them in an array how can i do that ?
when i have placed these in the cnvtcpltcallback function :
a[i] = adcval;
i++;
the code isnt working the way i wanted to work i mean all the values in less than a second are getting stored in an array.
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
/* Prevent unused argument(s) compilation warning */
adcval= HAL_ADC_GetValue(&hadc1);
/* NOTE : This function Should not be modified, when the callback is needed,
the HAL_ADC_ConvCpltCallback could be implemented in the user file
*/
i++;
}
in the main function i have kept the following
HAL_Tim_Base_Start(&htim2);
HAL_ADC_Start_IT(&hadc1);
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
htim2.Instance = TIM2;
htim2.Init.Prescaler = 16000;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
2018-10-19 01:34 PM
this code works;
here I copy this new data set into the next row of my table.
there are 16 rows, spanning the last 16 sets of readings.
all post processing is done is a worker thread in the foreground.
extern "C" void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
HAL_ADC_CompleteCycle = true; // signal foreground to process
// copy 15 results away now
for (int i=0;i < ADC_ChannelCount;i++)
HAL_ADC_Channel_Buffer [ADC_RowCounter][i] = ADC_DMABuffer[i];
ADC_RowCounter ++; // for next time through the loop
if ( ADC_RowCounter >= ADC_RowCount) // 16 rows
ADC_RowCounter =0;
}
2018-10-19 02:15 PM
I used a period elapsed event of timer function and delclared an array there so the issue is solved but i am open for other comments as well