stm32 PWM驱动舵机

来源:互联网 发布:java 判断char为空 编辑:程序博客网 时间:2024/05/22 03:38

今天使用stm32控制舵机,调试了许久,总结如下:
注意事项:

1.PWM周期的设定:初值,20000-1,分频=72-1。周期是:72M/72*20000=20ms 频率=50hz;
2.这个型号的舵机应该是5VPWM信号输出。而STM32是0-3.3,引起无法驱动。
更新:原来我上次无法驱动舵机是因为我把舵机的三条线搞错了,大写的尴尬
VCC——–>红
GND——–>黑
DATA——–>白

幸亏我潜意识里吧黑色的当成GND。。。。。。。。不然。。。。

故:以下程序成立。

#include "pbdata.h"void RCC_Configuration(void);void GPIO_Configuration(void);void TIM3_Configuration(void);int main(void){   u8 led_fx=1;   u16 led_dt=18000;   RCC_Configuration(); //系统时钟初始化   GPIO_Configuration();//端口初始化   TIM3_Configuration();//定时器和pwm配置    while(1)   {//       delay_ms(100);//   if(led_fx==1)//   {//      led_dt++;//   }//   else//   {//     led_dt--;//   }//   if(led_dt>19000)  led_fx=0;//       if(led_dt==18000)   led_fx=1;     TIM_SetCompare2(TIM3,led_dt);   }    }void RCC_Configuration(void){    SystemInit();    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);}void GPIO_Configuration(void){    GPIO_InitTypeDef GPIO_InitStructure;        //LED    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;    GPIO_Init(GPIOB,&GPIO_InitStructure);    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;    GPIO_Init(GPIOA,&GPIO_InitStructure);}void TIM3_Configuration(void){    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;    TIM_OCInitTypeDef TIM_OCInitStructure;    GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);    //定时器初始化    TIM_TimeBaseStruct.TIM_Period=20000-1;//初值    TIM_TimeBaseStruct.TIM_Prescaler=71;//预分频    TIM_TimeBaseStruct.TIM_ClockDivision=0;    TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);    //pwm 初始化    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;    TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;    TIM_OC2Init(TIM3,&TIM_OCInitStructure);    TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);    TIM_Cmd(TIM3,ENABLE);}
1 0
原创粉丝点击