Can't get TIMER to perfect count and Toogle Pin
Ok, so I decided to change from Arduino IDE (STM32F103) to STM32CubeIDE (STM32F334R8).
I can't get perfect timing.
My program just have to toogle my pin after x us (that is presented inside a vector)
Just to illustrate, here is my arduino code (I only get 1us of error):
// Function called when overflow count is detected
void Update_IT_callback(void){
// Tootlge pin
digitalWrite(pin, !digitalRead(pin));
// This loop is just because I need to repeat it
if (id_vector >= aux_signalsize){
id_vector = 0;
loop_count++;
if (loop_count >= size_loop){
MyTim->pause();
MyTim->refresh();
loop_count = 0;
}
}
// Set wait time in us to next toogle
MyTim->setOverflow(aux_signal[id_vector++], MICROSEC_FORMAT);
}And here is my HAL code (I get 2.3us error).
void TIM1_UP_TIM16_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_UP_TIM16_IRQn 0 */
HAL_GPIO_TogglePin(RF_Pin_GPIO_Port, RF_Pin_Pin);
if (vector_run == vector_max){
vector_run = 0;
if (repeat_id == repeat){
// Repetimos o sinal o suficiente
// Desabilitamos a contagem
HAL_TIM_Base_Stop(&htim16);
HAL_GPIO_WritePin(RF_Pin_GPIO_Port, RF_Pin_Pin, GPIO_PIN_RESET);
// Apagamos o sinal
for (int i = 0; i < vector_max; i++){
rf_sig[i] = 0;
}
// Apagamos o tamanho do vetor
vector_max = 0;
//vector_run = 0;
repeat_id = 0;
repeat = 0;
}else{
repeat_id++;
}
}
__HAL_TIM_SET_AUTORELOAD(&htim16, rf_sig[vector_run]-1);
vector_run++;
/* USER CODE END TIM1_UP_TIM16_IRQn 0 */
HAL_TIM_IRQHandler(&htim16);
/* USER CODE BEGIN TIM1_UP_TIM16_IRQn 1 */
/* USER CODE END TIM1_UP_TIM16_IRQn 1 */
}Other important features:
htim16.Instance = TIM16;
htim16.Init.Prescaler = 64-1;
htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
htim16.Init.Period = 10;
htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim16.Init.RepetitionCounter = 0;
htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;I've already decreased Prescaler, but the result is the same. I was hoping to get the complete opposite. More performance for HAL lib.
I'm clearly doing something wrong.
