当前位置:网站首页>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°所以按键应该是有锁存的情况,与之前机械按键有锁存的原理一样。但是触摸按键不需要延时消抖。
边栏推荐
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
- Yyds dry inventory KVM new inventory to expand space for home
- Ranger (I) preliminary perception
- 618深度复盘:海尔智家的制胜方法论
- Summary of monthly report | list of major events of moonbeam in June
- PCL least median square method fitting plane
- Mobile web development learning notes - Layout
- 流批一体在京东的探索与实践
- 月报总结|Moonbeam6月份大事一览
猜你喜欢

Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus

理想之光不灭

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...

Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)

unity Hub 登錄框變得很窄 無法登錄

Effectively use keywords to increase Amazon sales

触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加

电脑管理员权限在哪里可以打开

Recalling the college entrance examination and becoming a programmer, do you regret it?

⌈ 2022 ⌋ how to use webp gracefully in projects
随机推荐
[fluent] dart data type string type (string definition | string splicing | string API call)
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...
Where can I open computer administrator permissions
Summary | three coordinate systems in machine vision and their relationships
LeetCode 1. 两数之和
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
mysql min() 求某条件下最小的值出现多个结果
Take you ten days to easily complete the go micro service series (I)
618深度複盤:海爾智家的制勝方法論
JS learning notes - variables
机器学习-感知机模型
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
去除router-link中的下划线
ROW_NUMBER()、RANK()、DENSE_RANK区别
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
The difference and usage of calloc, malloc and realloc functions
Ranger (I) preliminary perception