STM32:|STM32: TIMER门控模式控制PWM输出长度
搞了两天单脉冲没搞定,无意中发现,这个利用主从模式的门控方式来控制一路PWM的输出长度很有效。
文章图片
文章图片
1 //TIM2 PWM输出,由TIM4来控制其输出与停止 2 //frequency_tim2:TIM2 PWM输出周期:KHz 3 //duty_tim2:TIM2 PWM占空比0-100 4 //period_tim4: TIM4控制TIM2总周期,单位0.1ms 5 //duty_tim4: TIM4控制TIM2输出时间,单位0.1ms 6 7 void TIM2_CH3_PA2_PWM_TIM4_CH4_GATE(unsigned short int frequency_tim2,char duty_tim2,unsigned short int period_tim4,unsigned short int duty_tim4) 8 { 9 10GPIO_InitTypeDefGPIO_InitStructure; 11TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure; 12TIM_OCInitTypeDefTIM_OCInitStructure; 13 14RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM4, ENABLE); //使能定时器2时钟 15RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟 16 17//设置该引脚为复用输出功能,输出TIM2 CH3的PWM脉冲波形GPIOA.2 18GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //TIM2_CH3 19GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 20GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 21GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO 22 23//初始化TIM2 24TIM_TimeBaseStructure.TIM_Period = (1000/frequency_tim2); //设置在下一个更新事件装入活动的自动重装载寄存器周期的值,本身频率为1MHz,又分频器设置的下面一行 25TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //设置用来作为TIMx时钟频率除数的预分频值,72MHz/72=1MHz 26TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim 27TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 28TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 29 30//初始化TIM2 Channel3 PWM模式 31TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 32TIM_OCInitStructure.TIM_Pulse=(10*duty_tim2/frequency_tim2); //(TIM_Period)*(duty/100) --> (1000/period)*(duty/100) 33TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 34TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高 35TIM_OC3Init(TIM2, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM2 OC3 36 37TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR3上的预装载寄存器 38TIM_Cmd(TIM2, ENABLE); //使能TIM2 39 40//初始化TIM4 41TIM_TimeBaseStructure.TIM_Period = period_tim4; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值,本身频率为1MHz,又分频器设置的下面一行 42TIM_TimeBaseStructure.TIM_Prescaler =(7200-1); //设置用来作为TIMx时钟频率除数的预分频值,72MHz/72=1MHz 43TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim 44TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 45TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 46 47//初始化TIM4 Channel4 PWM模式 48TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2 49TIM_OCInitStructure.TIM_Pulse=duty_tim4; //(TIM_Period)*(duty/100) --> (1000/period)*(duty/100) 50TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 51TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高 52TIM_OC4Init(TIM4, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4 OC4 53 54TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CCR4上的预装载寄存器 55TIM_Cmd(TIM4, ENABLE); //使能TIM4 56 57 58TIM_SelectInputTrigger(TIM2, TIM_TS_ITR3); //选择有效输入端 ,查表得知TIM4对于TIM2是ITR3 59TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Gated); //配置为主从复位模式 60TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC4Ref ); 61/* Enable timer's master/slave work mode */ 62TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable); //Slave 63TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable); //Master 64 65 }
TIM2_CH3_PA2_PWM_TIM4_CH4_GATE 【STM32:|STM32: TIMER门控模式控制PWM输出长度】参照网上朋友代码加了一点修改直接看函数就OK了,蛮简单的
转载于:https://www.cnblogs.com/wwjdwy/p/3441334.html
推荐阅读
- 基于stm32智能风扇|基于stm32智能风扇_一款基于STM32的智能灭火机器人设计
- stm32|基于STM32和freeRTOS智能门锁设计方案
- STM32F4|STM32F4 TIM6 TIM7 基本定时器
- Python+树莓派制作IoT(物联网)门控设备
- LUA|单片机脚本语言移植lua到stm32MDK
- STM32 CUbeIDE 定时器中断使用
- STM32F1系列ADC电压采集
- ASIO deadline_timer 的使用
- system_stm32f0xx.h(基于v1.2.1)
- boost asio deadline_timer 取消测试