stm32动态计时器设置更改

时间:2017-05-24 20:58:08

标签: timer serial-port stm32 hal

我试图让设备接收来自外部传感器(压力传感器)的信号。我在STM32F3Discovery和USB-USART适配器上使用HAL库通过串口与计算机通信。我向设备发送数据和从设备接收数据,但我需要一些方法来改变我的gui应用程序上的按钮来更改计时器预分频器和自动重载值(以更改采样率)。我怎样才能做到这一点?我正在尝试做类似的事情:

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

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

}

但它显然没有用:D帮助。

编辑:

我认为我在语法上做错了,首先我会像这样初始化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();
  }
   }    

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

}
}

我确定即时发送数据并在uC上接收它,因为LED。

2 个答案:

答案 0 :(得分:0)

绝对有效。您可以通过引发UG事件来重新加载阴影PSC寄存器。

答案 1 :(得分:0)

我发现了声明:

/* 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;
}

是否可以,我只能为TIM1和TIM8重新加载PSC和ARR值?