当前位置:网站首页>PWM控制舵机
PWM控制舵机
2022-07-02 13:22:00 【让一切都燃烧】
任务要求
使用PWM控制舵机进行舵机在舵机角度范围内任意角度旋转。注意事项:尽量不要用外力阻碍舵机旋转,避免对单片机造成伤害。验收要求:
初始位置0°,按一下按键(可自定任意一个按键)旋转30°,如果到头就转向,变成按一下按键反向转30°
反复按一个按键,角度从0变到最大角度再回到0
思路

需要用到30°
所以利用原有的
0° 1500 0.5ms
45° 3000 1ms
……
计算出比例
就可以估算求得
30.06 2502 0.834 1002
整个周期是20ms,溢出值是60000
0 0.5ms 1500
180 2.5ms 7500
溢出时间=(arr+1)(psc+1)/Tclk
20MS = (59999+1)*(23+1)/72000000
完成的代码
int main (void){
//定义变量必须放在最前面
u16 t = 1500;
u8 dir = 1;
delay_ms(500);
RCC_Configuration();
RELAY_Init();
I2C_Configuration();
TOUCH_KEY_Init();//初始化触摸按键
TIM3_PWM_Init(59999,23);
//使初始情况是0°
TIM_SetCompare3(TIM3,1500);
while(1){
delay_ms(10);
//正方向,每次按下按键走30°
if(dir){
for(t=1500;t<7500;){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B)){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B)){
t=t+1002;//需要先++再执行所以把代码写在这里
TIM_SetCompare3(TIM3,t);
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B));
}
}
}
}
if(t==7500)dir=0;
//负方向,每次按下按键走30°
else{
for(t=7500;t>1500;){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B)){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B)){
t=t-1002;
TIM_SetCompare3(TIM3,t);
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_B));
}
}
}
}
if(t==1500)dir=1;//与上一条对应的if语句需要写在if(dir)的外面,不然循环无法跳出
}
}
因为需要每次按键按下都走30°所以按键应该是有锁存的情况,与之前机械按键有锁存的原理一样。但是触摸按键不需要延时消抖。
边栏推荐
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
- Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- HMS core machine learning service helps zaful users to shop conveniently
- Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
- Yyds dry goods inventory student attendance system based on QT design
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
- Set the background picture in the idea (ultra detailed)
- [fluent] dart data type string type (string definition | string splicing | string API call)
- Summary of multithreading and thread synchronization knowledge
猜你喜欢

数学分析_笔记_第6章:一元函数的Riemann积分

PCL point cloud image transformation

Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation

流批一体在京东的探索与实践

Yyds dry inventory method of deleting expired documents in batch

Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function

外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生

Memory alignment of structure

Data security industry series Salon (III) | data security industry standard system construction theme Salon
![[fluent] dart data type string type (string definition | string splicing | string API call)](/img/7b/cc624aa33f45fbed0bbe354253158b.jpg)
[fluent] dart data type string type (string definition | string splicing | string API call)
随机推荐
LeetCode 6. Z 字形变换 (N字形变换)
618深度複盤:海爾智家的制勝方法論
Multi task prompt learning: how to train a large language model?
How to solve the failure of printer driver installation of computer equipment
Original God 2.6 server download and installation tutorial
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
机器学习-感知机模型
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
结构体的内存对齐
Data security industry series Salon (III) | data security industry standard system construction theme Salon
unity Hub 登錄框變得很窄 無法登錄
Typescript array out of order output
虚假的暑假
Effectively use keywords to increase Amazon sales
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Ranger (I) preliminary perception
Maui学习之路(三)--Winui3深入探讨
(practice C language every day) the sum of the nearest three numbers
PCL least median square method fitting plane