[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>STM32CUBEMX配置PWM,控制舵机
<>舵机
<>简介
舵机(Servo),由直流电机、减速齿轮组、传感器和控制电路组成的一套自动控制系统。其控制方式为PWM控制,通过调节控制信号的占空比来实现舵机转动指定角度。驱动舵机的PWM信号周期需要为20ms(50HZ),而控制脉宽为0.5~2.5ms。
黄色为信号线,红色是5V电源线,褐色(或黑色)是GND。
<>舵机控制
<>PWM
pwm中文名叫脉冲宽度调制,
pwm的频率:(Hz)
是指1秒钟内信号从高电平到低电平再回到高电平的次数(一个周期);
也就是说一秒钟PWM有多少个周期
pwm的周期:(s)
T=1/f
周期=1/频率
占空比:
是一个脉冲周期内,高电平的时间与整个周期时间的比例
单位: % (0%-100%)
<>CubeMX配置
基础配置,选择高速外部时钟,配置时钟树(我这里芯片为F103ZET6,最高频率是72MHz)。
配置定时器TIME(PWM模式):
舵机的信号控制周期(T)为20ms,
T= 1 / (系统时钟频率 / Prescaler(分频系数)) * CounterPeriod(计数周期,计数次数) ;
T=1 / (72MHz / (719 +1) ) * (1999+1) = 20ms;
设置初始脉冲时间为0.5ms,不旋转。
可以看到PWM的通道1和2分别是通过PA0和PA1输出的。
<>代码
初始化PWM。
新建两个文件,一个 .c和一个 .h文件(名字自己命名),我这里是pwm.c和pwm.h;
pwm.c代码:
#include "pwm.h" void SG90_Rotate(uint16_t ch,uint16_t Degrees) { switch(ch) {
case 1: __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,Degrees);
//一个周期内(20ms)有0.5ms高电平 HAL_Delay(1000); break; case 2: __HAL_TIM_SET_COMPARE(&
htim2,TIM_CHANNEL_2,Degrees); //一个周期内(20ms)有0.5ms高电平 HAL_Delay(1000); break;
default: break; } }
pwm.h代码:
#ifndef __PWM_H #define __PWM_H #include "main.h" #include "tim.h" #include
"gpio.h" #define Degrees_0 50 //原点位置 #define Degrees_45 100 //45度位置 #define
Degrees_90 150 //90度位置 #define Degrees_135 200 //135度位置 #define Degrees_180 250
//180度位置 void SG90_Rotate(uint16_t ch,uint16_t Degrees); #endif
主函数里的代码,main.c
好了,以上就是我本次分享的STM32通过PWM进行控制舵机旋转。