STM32定时器频率是不是等于PWM输出频率?

Posted

技术标签:

【中文标题】STM32定时器频率是不是等于PWM输出频率?【英文标题】:Does STM32 timer frequency equal the PWM output frequency?STM32定时器频率是否等于PWM输出频率? 【发布时间】:2019-03-31 03:21:40 【问题描述】:

我很容易理解问题。我确实将我的 STM32 定时器初始化为 1 sek 计数(TIM8,预分频器 = 16800-1,周期 = 10000-1)并想要调试它(测量引脚输出频率) - 它是一个与我可以观察到的频率相同的定时器频率它在示波器上? 它是 TIM8 定时器中断的正确配置吗?

void Second_timer_Init() 

    GPIO_InitTypeDef GPIO_InitStructure;                        //GPIO Init structure definition

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);       //Enables AHB1 peripheral clock for GPIOC
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);        //Enables AHB1 peripheral clock for TIM2

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;                   //Specifies the GPIO pins to be configured

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;              //Specifies the operating output type for the selected pins
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;                //GPIO Alternate function Mode
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;            //Specifies the operating Pull-up/Pull down for the selected pins
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;           //Specifies the speed for the selected pins

    GPIO_Init(GPIOC, &GPIO_InitStructure);                      //Initializes the GPIOA peripheral

    TIM_TimeBaseInitTypeDef Timer_InitStructure;                //TIM8 Time Base Init structure definition
    TIM_OCInitTypeDef Output_ChannelInit;                       //TIM8 Output Compare Init structure definition

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8);     // PC6 -> Connect TIM8 pins to AF3

    Timer_InitStructure.TIM_Period = 10000 - 1;                 //Specifies the period value (Orig 1 Sek: 10000-1)
    Timer_InitStructure.TIM_Prescaler = 16800-1;                //Specifies the prescaler value used to divide the TIM clock (Orig 1 Sek: 16800-1)
    Timer_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;       //Specifies the clock division (0)
    Timer_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;   //Specifies the counter mode

    TIM_TimeBaseInit(TIM8, &Timer_InitStructure);               //Initializes the TIM8 Time Base Unit peripheral

//  TIM_OCStructInit(&Output_ChannelInit);
    Output_ChannelInit.TIM_OCMode = TIM_OCMode_PWM1;            //Specifies the TIM8 PWM mode
    Output_ChannelInit.TIM_OutputState = TIM_OutputState_Enable;//Specifies the TIM8 Output Compare state
    Output_ChannelInit.TIM_Pulse = 0;                           //Specifies the pulse value to be loaded into the Capture Compare Register
    Output_ChannelInit.TIM_OCPolarity = TIM_OCPolarity_Low;     //Specifies the output polarity

    TIM_OC1Init(TIM8, &Output_ChannelInit);                     //Initializes the TIM8 Channel1
    TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);           //Enables the TIM2 peripheral Preload register on CCR1

    TIM_ARRPreloadConfig(TIM8, ENABLE);                         //Enables TIM2 peripheral Preload register on ARR

    TIM_Cmd(TIM8, ENABLE);                                      //Enables the specified TIM8 peripheral

    TIM_CtrlPWMOutputs(TIM8, ENABLE);                           //Enables the TIM peripheral Main Outputs ?

    TIM8->CCR1 = 5000;                                          //Set duty cycle to 50%

    TIM_ClearITPendingBit(TIM8, TIM_IT_Update);                 //Clears the TIM8 interrupt pending bits
    TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);                  //Enables the specified TIM8 interrupts

    NVIC_InitTypeDef NVIC_InitStructure;                        //NVIC Init Structure definition
    NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;          //Specifies the IRQ channel to be enabled
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel (0-15)
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;       //Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel (0-15)
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;             //Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled
    NVIC_Init(&NVIC_InitStructure);                             //Initializes the NVIC peripheral


IRQ 处理程序

void TIM8_CC_IRQHandler()
    if (TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)          //Checks whether the TIM8 interrupt has occurred
        
            TIM_ClearITPendingBit(TIM8, TIM_IT_Update);         //Clears the TIM8 interrupt pending bits
            TIM3->CCR1 = 500;                                   //Debug
        
    else
            TIM3->CCR1 = 0;                                         //Debug

    

【问题讨论】:

【参考方案1】:

正如我所见,您更改了TIM8 中断中的TIM3 寄存器。

其次,您检查中断例程中的错误事件(调用此例程,然后发生 CC 事件而不是 UG 事件。设置中断时也是如此。您启用 UG 中断但服务于 CC 中断。一些uC 模型有一些不共享的中断向量。

顺便说一句,如果更改寄存器,使用 HAL 库有什么意义?

【讨论】:

您好,感谢您的回答!首先,更改 TIM3(我的蜂鸣器声音)只是为了在那里调试。我只需要每 1 Sek 初始化一个事件,TIM8 没有正常的中断(就像在 TIM2、TIM3、TIM4 中一样——但这已经被使用了)。“顺便说一句,如果你更改寄存器,使用 HAL 库有什么意义?”你的意思是?我更改寄存器以发出调试声音)

以上是关于STM32定时器频率是不是等于PWM输出频率?的主要内容,如果未能解决你的问题,请参考以下文章

STM32定时器输出PWM频率和步进电机控制速度计算

STM32定时器输出PWM频率和步进电机控制速度计算

STM32如何设置PWM波的频率为10HZ

STM32怎么输出六路频率和个数可控的PWM波形?不要频繁中断的设置,因为还有其他程序要处理

stm32怎样实现不同定时器产生不同频率的PWM波?

STM32笔记之PWM输出,调节频率及占空比