Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stm32 dynamic timer settings change

Im trying to make an device to receive the signal from external sensor (pressure sensor). I'm using HAL libraries on STM32F3Discovery and USB - USART adapter to communicate with computer through serial port. Im sending and receiving data to/from the device, but I need some way to change timer prescaler and autoreload value (to change a sampling rate) with a button on my gui application. How can I do that? I was trying do something like that:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
if (Recived == 0) {

    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_11);
    TIM7->PSC = 119;
    TIM7->ARR = 5999;

}

But it obviously didnt work :D Help.

EDIT:

I think im doing something wrong with syntax, cause firstly I initialize tim7 like this:

static void MX_TIM7_Init(void)
{

 TIM_MasterConfigTypeDef sMasterConfig;

  htim7.Instance = TIM7;
  htim7.Init.Prescaler = 9999;
  htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim7.Init.Period = 719;
  if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
  {
    Error_Handler();
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != 
HAL_OK)
  {
    Error_Handler();
  }
   }    

And then Im trying to reconfig tim7 in my main function like this

void HAL_UART_RxCpltCallback(UART_HandleTypeDef* huart) {
if (Recived == 0) {

    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_11);
    TIM7->PSC = 119;
    TIM7->ARR = 5999;
    TIM7->EGR = TIM_EGR_UG;

}
    MX_TIM7_Init();
if(Recived == 1){

    HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_12);
    TIM7->PSC = 9999;
    TIM7->ARR = 719;
    TIM7->EGR = TIM_EGR_UG;

}
}

Im sure that im sending data and receiving it on uC, because of leds.

like image 504
Natoll Avatar asked Oct 19 '25 03:10

Natoll


1 Answers

It definitely does work. You can reload the shadow PSC register by raising the UG event.

like image 185
0___________ Avatar answered Oct 22 '25 04:10

0___________