当前位置:网站首页>STM32F103C8T6 PWM驱动舵机(SG90)
STM32F103C8T6 PWM驱动舵机(SG90)
2022-07-07 13:02:00 【与太阳有关_】
小知识:同一个定时器,不同通道输出不同输出PWM的特点。
对于同一个定时器的不同通道输出PWM,因为它们是共用一个计数器的,所以频率
必须一样,而占空比
由各自的CCR
决定,可以各自设定。还有,相位
由于计数器更新,所有PWM会同时跳变,所以相位是同步的。
若是使用1个定时器控制多个舵机或者直流电机,则使用同一个定时器不同通道的PWM,就完全可以了。
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_OC4Init(TIM2,&TIM_OCInitStructure);
代码:
PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init();
void PWM_SetCompare2(uint16_t Compare);
#endif
PWM.c
#include "stm32f10x.h" // Device header
void PWM_Init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //开启定时器2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//通道2时钟使能函数
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置GPIO为推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度设置为 50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //按照以上参数进行 GPIO的初始化
TIM_InternalClockConfig(TIM2);//TIM的时基单元由内部时钟控制
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 自动重装器的值
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //PSC 预分频器的值 对72M(720000000)进行 7200分频 即10K的频率下 计10000个数 1s的时间
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器的值
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
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,即占空比为 10%
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare2(uint16_t Compare)
{
TIM_SetCompare2(TIM2, Compare);
}
Serco.h
#ifndef __Servo_H
#define __Servo_H
void Servo_Init(void);
void Servo_SetAngle(float Angle);
#endif
Servo.c
#include "stm32f10x.h" // Device header
#include "PWM.H"
void Servo_Init(void)
{
PWM_Init();
}
void Servo_SetAngle(float Angle)
{
PWM_SetCompare2(Angle / 180 * 2000 +500);
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "OLED.H"
#include "Servo.H"
uint8_t KeyNum;
float Angle; //舵机角度
int main(void)
{
OLED_Init();
Servo_Init();
Key_Init();
OLED_ShowString(1,1,"Angle:");
while (1)
{
KeyNum = Key_GetNum();
if(KeyNum == 1)
{
Angle +=30;
if(Angle > 180)
{
Angle = 0;
}
}
Servo_SetAngle(Angle);
OLED_ShowNum(1,7,Angle,3);//在屏幕上显示角度
}
}
出现的问题:
换了个舵机,然后用示波器测引脚上是
有波
的,但是舵机就是不动
。后来发现是电压
给得不够。
好好努力,慢慢成长
边栏推荐
- Guangzhou Development Zone enables geographical indication products to help rural revitalization
- ⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
- Niuke real problem programming - Day10
- PD virtual machine tutorial: how to set the available shortcut keys in the parallelsdesktop virtual machine?
- 在软件工程领域,搞科研的这十年!
- MySQL installation configuration 2021 in Windows Environment
- Why do we use UTF-8 encoding?
- 简述keepalived工作原理
- Bits and Information & integer notes
- 2022 cloud consulting technology series high availability special sharing meeting
猜你喜欢
CPU与chiplet技术杂谈
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
Ctfshow, information collection: web9
Niuke real problem programming - Day10
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
CTFshow,信息搜集:web13
[Yugong series] go teaching course 005 variables in July 2022
Niuke real problem programming - day14
随机推荐
PAT 甲级 1103 Integer Factorizatio
JSON解析实例(Qt含源码)
In the field of software engineering, we have been doing scientific research for ten years!
Ctfshow, information collection: web2
Integer learning
[Yugong series] go teaching course 005 variables in July 2022
Pytorch model trains practical skills and breaks through the bottleneck of speed
Protection strategy of server area based on Firewall
Ctfshow, information collection: web9
CPU与chiplet技术杂谈
15、文本编辑工具VIM使用
Bits and Information & integer notes
暑期安全很重要!应急安全教育走进幼儿园
Stream learning notes
Niuke real problem programming - Day9
众昂矿业:萤石继续引领新能源市场增长
Introduction and use of Kitti dataset
With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
Guangzhou Development Zone enables geographical indication products to help rural revitalization