STM32L051定时器,修改定时参数,不起作用,什么原因?<已解决>

2019-12-08 14:41发布

本帖最后由 zhcj66 于 2019-12-4 08:26 编辑

如题<STM32L051定时器,修改定时参数,不起作用,什么原因?>  输出频率固定在551KHz  

无论怎么修改
  TimHandle.Init.Period = 5000 - 1;
  TimHandle.Init.Prescaler = (uint32_t) ((SystemCoreClock / 10000) - 1);/* Compute the prescaler value to have TIMx counter clock equal to 10 KHz */
参数都无法更改定时频率,

  1.                        
  2. #define TIMx_CLK_ENABLE              __HAL_RCC_TIM6_CLK_ENABLE

  3. /* Definition for TIMx's NVIC */
  4. #define TIMx_IRQn                    TIM6_DAC_IRQn
  5. #define TIMx_IRQHandler              TIM6_DAC_IRQHandler
  6. TIM_HandleTypeDef    TimHandle;
  7. static void ErrorHandler(void)
  8. {
  9.   /* Infinite loop */
  10.   while(1)
  11.   {
  12.   }
  13. }
  14. /* Prescaler declaration */
  15. uint32_t uwPrescalerValue = 0;
  16. void TIM6_Init(void)//0.5s-2Hz
  17. {
  18.   /*##-1- Configure the TIM peripheral #######################################*/
  19.   /* Set TIMx instance */
  20.   TimHandle.Instance = TIM6;
  21.    
  22.   /* Initialize TIMx peripheral as follow:
  23.        + Period = 10000 - 1
  24.        + Prescaler = SystemCoreClock/10000 Note that APB clock = TIMx clock if
  25.                      APB prescaler = 1.
  26.        + ClockDivision = 0
  27.        + Counter direction = Up
  28.   */
  29.   TimHandle.Init.Period = 5000 - 1;
  30.   TimHandle.Init.Prescaler = (uint32_t) ((SystemCoreClock / 10000) - 1);/* Compute the prescaler value to have TIMx counter clock equal to 10 KHz */
  31.   TimHandle.Init.ClockDivision = 0;
  32.   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  33.   if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)
  34.   {
  35.     /* Initialization Error */
  36.     ErrorHandler();
  37.   }
  38.   
  39.   /*##-2- Start the TIM Base generation in interrupt mode ####################*/
  40.   /* Start Channel1 */
  41.   if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK)
  42.   {
  43.     /* Starting Error */
  44.     ErrorHandler();
  45.   }
  46. }


  47. #define TIMx_CLK_ENABLE              __HAL_RCC_TIM6_CLK_ENABLE
  48. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim)
  49. {
  50.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  51.   /* TIMx Peripheral clock enable */
  52.   TIMx_CLK_ENABLE();
  53.   
  54.   /* The used GPIO (LED2 port) will be configured in the main program through
  55.   LED2 initialization method */

  56.   /*##-2- Configure the NVIC for TIMx ########################################*/
  57.   /* Set the TIMx priority */
  58.   HAL_NVIC_SetPriority(TIMx_IRQn, 0, 0);
  59.   
  60.   /* Enable the TIMx global Interrupt */
  61.   HAL_NVIC_EnableIRQ(TIMx_IRQn);
  62. }

  63. void TIMx_IRQHandler(void)
  64. {
  65.         HAL_GPIO_TogglePin(GPIOH, GPIO_PIN_1);
  66. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。