这边对SG90舵机进行一个简短的文字介绍:
舵机的控制一般需要一个20ms左右的时基脉冲,该脉冲的高电平部分一般为0.5ms-2.5ms范围,总间隔为2ms。脉冲的宽度将决定马达转动的距离。例如:1.5毫秒的脉冲,电机将转向90度的位置(通常称为中立位置,对于180°舵机来说,就是90°位置)。如果脉冲宽度小于1.5毫秒,那么电机轴向朝向0度方向。如果脉冲宽度大于1.5毫秒,轴向就朝向180度方向。以180度舵机为例,对应的控制关系是这样的:
0.5ms————-0度;
1.0ms————45度;
1.5ms————90度;
2.0ms———–135度;
2.5ms———–180度;
程序由bsp_config.h/c文件,bsp_advancetime1.h/c文件,main.c构成
bsp_config文件包含一个头文件与延时和位操作等程序。
bsp_advancetime1文件就是对高级定时器1的定义与PWM波产生程序的书写还有对GPIO引脚的初始化。
main.c函数不用多说
#ifndef BSP_CONFIG_H
#define BSP_CONFIG_H
#include "stm32f10x.h"
//位带宏定义
#define BITBAND(addr, bitnum) ((addr&0xF0000000) + 0x2000000 + ((addr&0xFFFFF)<<5) + (bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址位带映射
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //GPIOA输出数据寄存器地址0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //GPIOB输出数据寄存器地址0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //GPIOC输出数据寄存器地址0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //GPIOD输出数据寄存器地址0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //GPIOE输出数据寄存器地址0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //GPIOF输出数据寄存器地址0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //GPIOG输出数据寄存器地址0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //GPIOA输入数据寄存器地址0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //GPIOB输入数据寄存器地址0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //GPIOC输入数据寄存器地址0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //GPIOD输入数据寄存器地址0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //GPIOE输入数据寄存器地址0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //GPIOF输入数据寄存器地址0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //GPIOG输入数据寄存器地址0x40011E08
//单个IO口位带操作
#define PA_OUT(n) BIT_ADDR(GPIOA_ODR_Addr,n) //PAx输出
#define PA_IN(n) BIT_ADDR(GPIOA_IDR_Addr,n) //PAx输入
#define PB_OUT(n) BIT_ADDR(GPIOB_ODR_Addr,n) //PBx输出
#define PB_IN(n) BIT_ADDR(GPIOB_IDR_Addr,n) //PBx输入
#define PC_OUT(n) BIT_ADDR(GPIOC_ODR_Addr,n) //PCx输出
#define PC_IN(n) BIT_ADDR(GPIOC_IDR_Addr,n) //PCx输入
#define PD_OUT(n) BIT_ADDR(GPIOD_ODR_Addr,n) //PDx输出
#define PD_IN(n) BIT_ADDR(GPIOD_IDR_Addr,n) //PDx输入
#define PE_OUT(n) BIT_ADDR(GPIOE_ODR_Addr,n) //PEx输出
#define PE_IN(n) BIT_ADDR(GPIOE_IDR_Addr,n) //PEx输入
#define PF_OUT(n) BIT_ADDR(GPIOF_ODR_Addr,n) //PFx输出
#define PF_IN(n) BIT_ADDR(GPIOF_IDR_Addr,n) //PFx输入
#define PG_OUT(n) BIT_ADDR(GPIOG_ODR_Addr,n) //PGx输出
#define PG_IN(n) BIT_ADDR(GPIOG_IDR_Addr,n) //PGx输入
void delay_us(uint16_t us);
void delay_ms(uint16_t ms);
#endif
#include "bsp_config.h"
void delay_Quality_us(void)
{
__ASM("nop");
__ASM("nop");
__ASM("nop");
__ASM("nop");
__ASM("nop");
__ASM("nop");
__ASM("nop");
__ASM("nop");
}
void delay_us(uint16_t us)
{
while(us--)
{
delay_Quality_us();
delay_Quality_us();
delay_Quality_us();
delay_Quality_us();
}
}
void delay_ms(uint16_t ms)
{
while(ms--)
{
delay_us(1000);
}
}
#ifndef BSP_ADVANCETIMER_H
#define BSP_ADVANCETIMER_H
#include "bsp_config.h"
void TIME1Init(uint16_t arr, uint16_t psc);
void servo_angle(uint16_t angle);
#endif
#include "bsp_advancetimer1.h"
void TIME1Init(uint16_t arr, uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr-1;
TIM_TimeBaseStructure.TIM_Prescaler = psc-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
void servo_angle(uint16_t angle)
{
uint16_t pluse;
if(angle <= 5) angle = 5;
if(angle >= 175) angle = 175;
//为90°时pluse等于1500us=1.5ms即转90度
pluse = (uint16_t)((50 + angle * 100/90.0)*10);
TIM_SetCompare1(TIM1, pluse);
}
#include "bsp_config.h"
#include "bsp_advancetimer1.h"
int main(void)
{
uint8_t i;
//20000/(72M/72)=20ms
TIME1Init(20000,72);//(72M/72)=1us加一次,这个需要细品一下和舵机计算角度有关,一个周期为20ms
while(1)
{
servo_angle(170);
delay_ms(1000);
servo_angle(30);
delay_ms(1000);
}
}
如果要使用PWM2来产生PWM信号的话
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
需要对这两行程序进行修改
PWM1->PWM2
HIGN->LOW