目录
舵机接线:
class="table-box">红色线 | 电源线(VCC) |
黑色 / 棕色 | 地线(GND) |
白色 / 黄色 | 信号线(pwm输出) |
目录
红色线 | 电源线(VCC) |
黑色 / 棕色 | 地线(GND) |
白色 / 黄色 | 信号线(pwm输出) |
注意:如果外接电源,那么舵机和开发板、电源之间需要共GND,即开发板上的GND与电源的GND和舵机的GND三者相连接。
如果三者没有共GND,这时舵机无法接收到正常的pwm驱动信号,如图所示,开发板输出周期为20ms,脉冲宽度(高电平宽度)为1.5ms的pwm完全变形,无法正常驱动舵机。
数字舵机只需发送1次PWM信号就能运动到预定的角度,模拟舵机需要给它不停的发送PWM信号,才能让它保持在预定的角度。
数字舵机与模拟舵机的驱动控制方法是一样的。
转动角度 | 脉冲周期 | 脉冲宽度(高电平宽度) |
-45度 | 20ms | 0.5ms |
0度 | 20ms | 1.5ms |
45度 | 20ms | 2.5ms |
转动角度 | 脉冲周期 | 脉冲宽度(高电平宽度) |
-90度 | 20ms | 0.5ms |
-45度 | 20ms | 1ms |
0度 | 20ms | 1.5ms |
45度 | 20ms | 2ms |
90度 | 20ms | 2.5ms |
转动角度 | 脉冲周期 | 脉冲宽度(高电平宽度) |
-135度 | 20ms | 0.5ms |
0度 | 20ms | 1.5ms |
135度 | 20ms | 2.5ms |
转动角度 | 脉冲周期 | 脉冲宽度(高电平宽度) |
-180度 | 20ms | 0.5ms |
-90度 | 20ms | 1ms |
0度 | 20ms | 1.5ms |
90度 | 20ms | 2ms |
180度 | 20ms | 2.5ms |
所以90度、180度、270度、360度的舵机驱动方法是一样的。
1.驱动脉冲周期都是20ms
2.所有舵机在脉冲宽度为1.5ms驱动下,都位于0度。
设置arr(自动重装载值)为10000-1=9999,psc(定时器分频系数)为168-1=167
时钟周期计算公式:T=(arr+1)*(psc+1)/ Tck
=(9999+1)*(167+1)/ 84Mhz
= 20 ms
设置arr(自动重装载值为10000-1=9999),psc(定时器分频系数144-1=143)
时钟周期计算公式:T=(arr+1)*(psc+1)/ Tck
=(9999+1)*(143+1)/ 84Mhz
= 20 ms
*(stmf4的时钟频率为84Mhz,stm32f1时钟频率为72Mhz,Tck为时钟频率)*
*** 注意定时器通道的输出极性 ***
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高。
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性低。
1. TIM_OCPolarity_High:当定时器计数小于比较数值是输出高电平,大于比较数值是输出低电平。
输出极性:TIM_OCPolarity_High 比较极性高
设置 arr=9999,psc=167,TIM_SetCompare1(TIM14,500);
如图所示:周期20ms,频率50Hz,脉冲宽度(高电平宽度)= 1ms
2.TIM_OCPolarity_Low:当定时器计数小于比较数值是输出低电平,大于比较数值是输出高电平。
输出极性:TIM_OCPolarity_Low比较极性低
设置 arr=9999,psc=167,TIM_SetCompare1(TIM14,500);
如图所示:周期20ms,频率50Hz,脉冲宽度(高电平宽度)= 19 ms
输出极性:TIM_OCPolarity_High 比较极性高 TIM_SetCompare1(TIM14,number);
设置 arr=9999,psc=167
class="table-box">脉冲宽度(高电平宽度) | 比较数值, |
0.5ms | number = 250 |
1ms | number = 500 |
1.5ms | number = 750 |
2ms | number = 1000 |
2.5ms | number = 1250 |
输出极性:TIM_OCPolarity_Low比较极性低 TIM_SetCompare1(TIM14,number);
设置 arr=9999,psc=167
class="table-box">脉冲宽度(高电平宽度) | 比较数值,TIM_SetCompare1(TIM14,number); |
0.5ms | number = 9750 |
1ms | number = 9500 |
1.5ms | number = 9250 |
2ms | number = 9000 |
2.5ms | number = 8750 |
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="1"> class="hljs-ln-code"> class="hljs-ln-line">#include "sys.h"
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="2"> class="hljs-ln-code"> class="hljs-ln-line">#include "delay.h"
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="3"> class="hljs-ln-code"> class="hljs-ln-line">#include "usart.h"
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="4"> class="hljs-ln-code"> class="hljs-ln-line">#include "pwm.h"
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="5"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="6"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="7"> class="hljs-ln-code"> class="hljs-ln-line">int main(void)
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="8"> class="hljs-ln-code"> class="hljs-ln-line">{
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="9"> class="hljs-ln-code"> class="hljs-ln-line"> NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="10"> class="hljs-ln-code"> class="hljs-ln-line"> delay_init(168);
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="11"> class="hljs-ln-code"> class="hljs-ln-line"> TIM14_PWM_Init(10000-1,168-1);
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="12"> class="hljs-ln-code"> class="hljs-ln-line"> while(1)
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="13"> class="hljs-ln-code"> class="hljs-ln-line"> {
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="14"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="15"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_SetCompare1(TIM14,500);
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="16"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="17"> class="hljs-ln-code"> class="hljs-ln-line"> }
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="18"> class="hljs-ln-code"> class="hljs-ln-line">}
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="19"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="20"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="21"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="22"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="23"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="24"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="25"> class="hljs-ln-code"> class="hljs-ln-line">void TIM14_PWM_Init(u32 arr,u32 psc)
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="26"> class="hljs-ln-code"> class="hljs-ln-line">{
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="27"> class="hljs-ln-code"> class="hljs-ln-line"> //此部分需手动修改IO口设置
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="28"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="29"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitTypeDef GPIO_InitStructure;
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="30"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="31"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OCInitTypeDef TIM_OCInitStructure;
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="32"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="33"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14时钟使能
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="34"> class="hljs-ln-code"> class="hljs-ln-line"> RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //使能PORTF时钟
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="35"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="36"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9复用为定时器14
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="37"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="38"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="39"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="40"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="41"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="42"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="43"> class="hljs-ln-code"> class="hljs-ln-line"> GPIO_Init(GPIOF,&GPIO_InitStructure); //初始化PF9
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="44"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="45"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="46"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="47"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="48"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="49"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="50"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//初始化定时器14
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="51"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="52"> class="hljs-ln-code"> class="hljs-ln-line"> //初始化TIM14 Channel1 PWM模式
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="53"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="54"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="55"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="56"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OC1Init(TIM14, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 4OC1
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="57"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="58"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="59"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="60"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPE使能
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="61"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="62"> class="hljs-ln-code"> class="hljs-ln-line"> TIM_Cmd(TIM14, ENABLE); //使能TIM14
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="63"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="64"> class="hljs-ln-code"> class="hljs-ln-line">
- class="hljs-ln-numbers"> class="hljs-ln-line hljs-ln-n" data-line-number="65"> class="hljs-ln-code"> class="hljs-ln-line">}
class="hide-preCode-box"> class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}" onclick="hljs.signin(event)">
data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/dengn191020/article/details/136685355","extend1":"pc","ab":"new"}">>
评论记录:
回复评论: