STM32 PWM驱动舵机(标准库)

代码演示

PWM.c

#include "stm32f10x.h"                  // Device header



void PWM_Init(void){
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//舵机
	GPIO_InitTypeDef GPIO_Initstructure;
	GPIO_Initstructure.GPIO_Mode= GPIO_Mode_AF_PP;
	GPIO_Initstructure.GPIO_Pin=GPIO_Pin_1;
	GPIO_Initstructure.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Initstructure);
	//定时器
	TIM_InternalClockConfig(TIM2);
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStructure;  
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period=20000-1;//ARR  //舵机要求的周期是20ms,电平为0.5ms-2.5ms高电平 所以频率是1/20ms=50HZ
	TIM_TimeBaseInitStructure.TIM_Prescaler=72-1;//PSC // Freq(50)=72M/(psc+1)/(ARR+1);这里设PSC=72-1,ARR=20000-1;
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;  //CCR设置2500 就是2.5ms 设置500 就是0.5ms
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);  
	//输出通道                                            //注:CCR寄存器的值决定PWM的占空比,占空比:高电平所占时间比例
	TIM_OCInitTypeDef TIM_OCInitStructure;                 //CCR代表高电平的时间
	TIM_OCStructInit(&TIM_OCInitStructure);//这里没有给所有结构体成员赋初始值,所以直接给结构体赋值
	TIM_OCInitStructure.TIM_OCMode= TIM_OCMode_PWM1;  //设计输出比较的模式
	TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;  //设计输出比较的极性
  TIM_OCInitStructure.TIM_OutputState= TIM_OutputState_Enable ; //设计输出使能
	TIM_OCInitStructure.TIM_Pulse=0;   //设计CCR
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);//这里因为用的是PA1引脚,所以要用通道2
	
	TIM_Cmd(TIM2,ENABLE);//启动定时器
}

void PWM_SetCompare2(uint16_t Compare){
	
	
	
	TIM_SetCompare2(TIM2,Compare);//单独改变ccr通道值
	
	
}

servo.c

#include "stm32f10x.h"                  // Device header
#include "PWM.h"


void Servo_Init(void)
{
	PWM_Init();
	
}
//0    500
//180  2500
//映射   ax+b=Y;
void Servo_SetAngle(float Angle)//设置舵机角度
	{
	PWM_SetCompare2(Angle/180*2000+500);
	
	
	
}

main.c

​

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "Sound.h"
#include "OLED.h"
#include "Timer.h"
float num; 
float val=0;
uint8_t a=0;
int main()
	
{
    OLED_Init();
	
	  Sound_Init();
	OLED_ShowString(1,1,"DISTANCE:");
	OLED_ShowString(2,6,"  .  cm");
	
	while(1){
		num=GetLength(); 
		a=(int)num;
		val=num-val;
    OLED_ShowNum(2,6,num,2);
		OLED_ShowNum(2,9,val,2);
		Delay_ms(400);
	}
	
}

[点击并拖拽以移动]
​

以下是使用STM32F407定时器11控制360度舵机标准库代码: 首先,需要配置定时器11,使其产生PWM信号,具体代码如下: ```c // 配置TIM11定时器产生PWM信号 void TIM11_PWM_Init(uint16_t arr, uint16_t psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; // 使能TIM11和GPIOF时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); // 配置PF7引脚为复用功能,用于TIM11_CH1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // 上下拉电阻不使能 GPIO_Init(GPIOF, &GPIO_InitStructure); // 将PF7引脚复用到TIM11_CH1 GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_TIM11); // 配置TIM11基本定时器 TIM_TimeBaseStructure.TIM_Prescaler = psc; // 预分频器 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式 TIM_TimeBaseStructure.TIM_Period = arr; // 自动重装载值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频 TIM_TimeBaseInit(TIM11, &TIM_TimeBaseStructure); // 配置TIM11的PWM输出模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // PWM模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 输出使能 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // 正极性 TIM_OC1Init(TIM11, &TIM_OCInitStructure); // 使能TIM11 TIM_Cmd(TIM11, ENABLE); } ``` 接下来,需要编写控制舵机转动的函数,具体代码如下: ```c // 控制舵机转动到指定角度 void Servo_SetAngle(uint16_t angle) { uint16_t pwm = 0; pwm = (angle * (2000 - 1000) / 180) + 1000; // 根据角度计算PWM值,范围为1000-2000 TIM_SetCompare1(TIM11, pwm); // 设置TIM11_CH1的比较值,控制PWM占空比 } ``` 最后,在main函数中调用上述函数即可控制舵机转动到指定角度,示例代码如下: ```c int main(void) { // 初始化定时器11,设置PWM周期为20ms,预分频系数为16800-1 TIM11_PWM_Init(20000, 16800-1); // 控制舵机转动到90度 Servo_SetAngle(90); while (1) { // 等待 } } ``` 注意:此代码仅供参考,实际应用中需要根据具体舵机型号和特性进行调整。同时,需要注意舵机电源的电压和电流,避免损坏舵机
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值