STM32一个定时器输出不同频率

1个定时器输出4路频率相同,占空比不同的PWM比较容易实现,只需要改变每个通道的比较值就可以了
要想输出频率不同,占空比相同,需要用到比较模式,当比较成功IO翻转。
【STM32一个定时器输出不同频率】STM32一个定时器输出不同频率
文章图片

同时还需要用到定时器中断去重新设置比较值。
稍微解释下面语句:
STM32一个定时器输出不同频率
文章图片

Period是计数值
prescaler是对总线时钟的分频
STM32一个定时器输出不同频率
文章图片

这个函数是设置比较值
基本要点是理解上面这些。没接触过定时器的会比较抽象,不能理解。
下面直接上代码吧
定时器初始化:

u16 capture = 0; vu16 CCR1_Val = 32768; vu16 CCR2_Val = 16384; vu16 CCR3_Val = 8192; vu16 CCR4_Val = 4096; void PWM1_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; TIM_OCInitTypeDefTIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //开启时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIO和服用功能时钟//初始化GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //PWM输出在PA8,9,10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化GPIO NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = 2; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //PWM模式 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //占空时间 TIM_OC1Init(TIM2,&TIM_OCInitStructure); //通道1 TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //占空时间 TIM_OC2Init(TIM2,&TIM_OCInitStructure); //通道2 TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //占空时间 TIM_OC3Init(TIM2,&TIM_OCInitStructure); //通道3 TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //占空时间 TIM_OC4Init(TIM2,&TIM_OCInitStructure); //通道4 TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_Cmd(TIM2,ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE); }

定时器中断:
extern "C" void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 ); capture = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture + CCR1_Val ); } if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); capture = TIM_GetCapture2(TIM2); TIM_SetCompare2(TIM2, capture + CCR2_Val); } if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); capture = TIM_GetCapture3(TIM2); TIM_SetCompare3(TIM2, capture + CCR3_Val); } if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); capture = TIM_GetCapture4(TIM2); TIM_SetCompare4(TIM2, capture + CCR4_Val); }}

主函数:
STM32一个定时器输出不同频率
文章图片

通道4先按10的比较值运行,具体频率未测,如果按1000的比较值运行,具体频率未测,实测效果
频率输出要做线性修正,没时间去研究,有空在完善。
STM32一个定时器输出不同频率
文章图片

附上仿真工程,所有代码都在主函数,其它的负略。
https://download.csdn.net/download/hes_c/11044736

    推荐阅读