此程序使了tim3定时器的pwm功能,驱动信号频率为250hz,已注明配置好的频率大致对应的角度,可同时产生4路pwm信号驱动4个舵机,pwm初始化程序参考了网上的大佬,转角用TIM_SetComparex函数可以实时控制,已测试可用。
单片机源程序如下:
- #include "public.h"
- //中值为650,250为90度多一点
- //4个舵机TIM3 4个通道
- int main()
- {
- PWM_Init();
- TIM_Config(288,1000); //频率为250
- while(1)
- {
- Timer3_PWM_SetDutyCycle(1,675);
- delay_ms(2000);
- Timer3_PWM_SetDutyCycle(1,800);
- delay_ms(2000);
- }
- }
复制代码
- #include "pwm.h"
- void PWM_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
-
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- void TIM_Config(u8 arr,u8 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- //定时器配置
- TIM_TimeBaseStructure.TIM_Prescaler = arr-1;//设置分频
- TIM_TimeBaseStructure.TIM_Period = psc-1;//设置计数溢出大小,每计psc个数就产生一个更新事件
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;//设置时钟分割
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//设置计数器模式为向上计数模式
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);//将配置应用到TIM3中
- //PWM配置ch1
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //配置为PWM模式2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High ; //有效电平为高电平
-
- TIM_OC1Init(TIM3, &TIM_OCInitStructure);//使能通道1
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- //PWM配置ch2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
-
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);//使能通道2
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- //PWM配置ch3
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
-
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);//使能通道3
- TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
- //PWM配置ch4
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
-
- TIM_OC4Init(TIM3, &TIM_OCInitStructure);//使能通道4
- TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
-
- //使能
- TIM_ARRPreloadConfig(TIM3, ENABLE);//使能TIM3重载寄存器ARR
- TIM_Cmd(TIM3, ENABLE);//使能定时器3
- }
- void Timer3_PWM_SetDutyCycle(u8 ch, u16 Timer3_PWM_DutyCycle) //配置4个通道占空比
- {
- switch (ch)
- {
- case 1:
- TIM_SetCompare1(TIM3, Timer3_PWM_DutyCycle );//通道1占空比
- break;
- case 2:
- TIM_SetCompare2(TIM3, Timer3_PWM_DutyCycle );//通道2占空比
- break;
- case 3:
- TIM_SetCompare3(TIM3, Timer3_PWM_DutyCycle );//通道3占空比
- break;
- case 4:
- TIM_SetCompare4(TIM3, Timer3_PWM_DutyCycle );//通道4占空比
- break;
- }
- }
复制代码
所有资料51hei提供下载:
sg90.7z
(197.05 KB, 下载次数: 116)
|