简体   繁体   English

stm32动态计时器设置更改

[英]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. 我正在使用STM32F3Discovery和USB-USART适配器上的HAL库通过串行端口与计算机进行通信。 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. 我正在向设备发送数据或从设备接收数据,但是我需要通过gui应用程序上的按钮来更改计时器预分频器和自动重载值(更改采样率)的方法。 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. 但它显然没有用:D帮助。

EDIT: 编辑:

I think im doing something wrong with syntax, cause firstly I initialize tim7 like this: 我认为我在语法上做错了,因为首先我像这样初始化tim7:

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 然后我试图像这样在我的主要功能中重新配置tim7

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. 由于LED,我确定即时通讯会在uC上发送和接收数据。

It definitely does work. 它确实可以工作。 You can reload the shadow PSC register by raising the UG event. 您可以通过引发UG事件来重新加载影子PSC寄存器。

I found that statement: 我发现该声明:

/* Set the Autoreload value */
 TIMx->ARR = (uint32_t)Structure->Period ;

/* Set the Prescaler value */
TIMx->PSC = (uint32_t)Structure->Prescaler;

if (IS_TIM_REPETITION_COUNTER_INSTANCE(TIMx))  
   {
    /* Set the Repetition Counter value */
  TIMx->RCR = Structure->RepetitionCounter;
}

/* Generate an update event to reload the Prescaler 
 and the repetition counter(only for TIM1 and TIM8) value immediatly */
TIMx->EGR = TIM_EGR_UG;
}

Is it possible, that I can reload PSC and ARR value only for TIM1 and TIM8? 是否有可能我只能为TIM1和TIM8重新加载PSC和ARR值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM