主控,淘宝黑色STM32F407板子
编译器 IAR
目标要求 输出四路PWM波
这里把写好的H和C文件给大家了,只需要在main里面简单的调用一下函数即可实现四路PWM的输出
在main里面加入
PWM_Init();
Updata_PWM_(20,200,500,600);
//即可得到2%,20%,50%,60%的PWM波
#include "PWM.h" // 输出四路PWM波 //----------------------------2016/11/26--陈勃瑄 // void PWM_Init(void) // void Updata_PWM_(float DC1,float DC2,float DC3,float DC4) //---------------------------------------------- // 定时器:TIM4 // 输出频率:50MHz // 占空比 m/1000 x100% // 自动重装载值 2000000 // PWM管脚配置 B6~B9 void PWM_GPIO(void){ GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_TIM4); } // PWM 定时器配置 void Pwm_Init(unsigned long Frequency, uint16_t TimerPeriod, float DC1,float DC2,float DC3,float DC4) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; uint16_t Channel1Pulse = 0,Channel2Pulse = 0,Channel3Pulse = 0,Channel4Pulse = 0; //((1+0 )/(60*1000000))*(1+TimerPeriod )=1/f //TimerPeriod = (120*1000000 / (Frequency*8)) - 1; Channel1Pulse = (uint16_t) (((uint32_t) DC1 * (TimerPeriod - 1)) / 1000); Channel2Pulse = (uint16_t) (((uint32_t) DC2 * (TimerPeriod - 1)) / 1000); Channel3Pulse = (uint16_t) (((uint32_t) DC3 * (TimerPeriod - 1)) / 1000); Channel4Pulse = (uint16_t) (((uint32_t) DC4 * (TimerPeriod - 1)) / 1000); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE); /* Time Base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = (84*1000000)/(TimerPeriod*Frequency)-1; //// 时钟预分频数 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; ////向上计数 TIM_TimeBaseStructure.TIM_Period = TimerPeriod; // 自动重装载寄存器的值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; ////重复寄存器,用于自动更新pwm占空比 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; ////使能该通道输出 //第五步,死区和刹车功能配置,高级定时器才有的,通用定时器不用配置 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // //设置互补端输出极性 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; ////使能互补端输出 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; // //死区后输出状态?? TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //死区后互补端输出状态?? /* TIM1 counter enable */ TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; // //设置占空比时间 TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; // //设置占空比时间 TIM_OC2Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; // //设置占空比时间 TIM_OC3Init(TIM4, &TIM_OCInitStructure); TIM_OCInitStructure.TIM_Pulse = Channel4Pulse; // //设置占空比时间 TIM_OC4Init(TIM4, &TIM_OCInitStructure); TIM_Cmd(TIM4, ENABLE); /* TIM1 Main Output Enable */ TIM_CtrlPWMOutputs(TIM4, ENABLE); } //PWM占空比设置 void UpdataDuty(unsigned long Frequency,uint16_t TimerPeriod,float DC1,float DC2,float DC3,float DC4) { TIM4->CCR1 =(int)(DC1*(TimerPeriod - 1) / 1000); TIM4->CCR2 =(int)(DC2*(TimerPeriod - 1) / 1000); TIM4->CCR3 =(int)(DC3*(TimerPeriod - 1) / 1000); TIM4->CCR4 =(int)(DC4*(TimerPeriod - 1) / 1000); } //PWM初始化 void PWM_Init(void) { PWM_GPIO(); Pwm_Init(50,1000,0,0,0,0); } //PWM占空比设置 void Updata_PWM_(float DC1,float DC2,float DC3,float DC4) { UpdataDuty(50,1000,DC1,DC2,DC3,DC4); } #ifdef __cplusplus extern "C" { #endif //....................................... #include "stm32f4xx.h" void PWM_Init(void); void Updata_PWM_(float DC1,float DC2,float DC3,float DC4); //....................................... #endif
上一篇:分享PWM输入模式捕捉4路PWM波形的周期和占空比
下一篇:STM32F407的定时器1之7路PWM输出
推荐阅读最新更新时间:2024-03-16 16:07