当前位置:网站首页>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°所以按键应该是有锁存的情况,与之前机械按键有锁存的原理一样。但是触摸按键不需要延时消抖。
边栏推荐
- The difference and usage of calloc, malloc and realloc functions
- Some problems about MySQL installation
- 七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
- OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
- Remove the underline in router link
- JS learning notes - operators
- 数学分析_笔记_第5章:一元微分学
- Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
- Memory alignment of structure
- Vscode设置标签页多行显示
猜你喜欢
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
月报总结|Moonbeam6月份大事一览
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
The light of ideal never dies
渗透工具-内网权限维持-Cobalt strike
Practice of traffic recording and playback in vivo
Mobile web development learning notes - Layout
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
随机推荐
电脑管理员权限在哪里可以打开
总结|机器视觉中三大坐标系及其相互关系
Take you ten days to easily complete the go micro service series (I)
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
JS learning notes - variables
Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
电脑设备打印机驱动安装失败如何解决
AWS virtual machine expansion
[fluent] dart data type boolean type (boolean type definition | logical operation)
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
LeetCode 2. 两数相加
(practice C language every day) the sum of the nearest three numbers
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode --- longest public prefix
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
Ranger (I) preliminary perception
Mysql database mysqldump why there is no statement to create a database
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
sim2real环境配置教程