简体   繁体   English

STM32F4 PWM和具有相同定时器的中断

[英]STM32F4 PWM and interrupt with the same timer

I have a STM32F407x. 我有一个STM32F407x。 Is it possible to have a PWM Signal on a Pin and at the same time getting a timer interrupt if the UPPER value is reached? 如果达到UPPER值,引脚上是否可以有PWM信号并同时获得定时器中断? I tried the following code, but I only get once an interrupt (count stays at 1 forever if I use the debugger), but the PWM Signal is still available at PB6: 我尝试了以下代码,但仅得到一次中断(如果使用调试器,计数将永远保持为1),但PB6上PWM信号仍然可用:

volatile int count=0;


void TM_LEDS_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;

    /* Clock for GPIOB */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    /* Alternating functions for pins */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4);

    /* Set pins */
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);


}

void TM_TIMER_Init(void) {
    TIM_TimeBaseInitTypeDef TIM_BaseStruct;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

    TIM_BaseStruct.TIM_Prescaler = 100;
    TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_BaseStruct.TIM_Period = 256; 
    TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM4, &TIM_BaseStruct);

}

void TM_PWM_Init(void) {
    TIM_OCInitTypeDef TIM_OCStruct;

    TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;

    TIM_OCStruct.TIM_Pulse = 150; /* 25% duty cycle */
    TIM_OC1Init(TIM4, &TIM_OCStruct);
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);

    /* Clear Interrupt */
    for(int i=0; i<3; i++)
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
      TIM_ClearITPendingBit(TIM4, TIM_IT_Update);

    /* Enable Interrupt */
    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);

    TIM_Cmd(TIM4, ENABLE);

}

void configNVIC(void)
{
    NVIC_InitTypeDef initNVICStruct;

    /* Enable the TIM2 global Interrupt */
    initNVICStruct.NVIC_IRQChannel = TIM4_IRQn;
    initNVICStruct.NVIC_IRQChannelPreemptionPriority = 1;
    initNVICStruct.NVIC_IRQChannelSubPriority = 3;
    initNVICStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&initNVICStruct);

}

void TIM4_IRQHandler(void) //This Interrupt changes changes state from x to x+-1
{
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
        count++;

    }
}



int main(void) {
    /* Initialize system */
    SystemInit();
    configNVIC();
    /* Init leds */
    TM_LEDS_Init();
    /* Init timer */
    TM_TIMER_Init();
    /* Init PWM */
    TM_PWM_Init();

    int i=0;
    while (1) {
        i=count;
    }
}

If you're trying to count pulses or stop at a certain pulse this post may help 如果您尝试计数脉冲或停止在某个脉冲上,则此帖子可能会有所帮助

Stop PWM output after N steps N步后停止PWM输出

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

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