当前位置:网站首页>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°所以按键应该是有锁存的情况,与之前机械按键有锁存的原理一样。但是触摸按键不需要延时消抖。
边栏推荐
- Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- Mysql database mysqldump why there is no statement to create a database
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- 数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
- Yyds dry inventory KVM new inventory to expand space for home
- day4
- (practice C language every day) the sum of the nearest three numbers
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
猜你喜欢

理想之光不灭

mysql数据库mysqldump为啥没有创建数据库的语句

SQL solves the problem of continuous login deformation holiday filtering

Everyone Xinfu builds: a one-stop intelligent business credit service platform

做机器视觉哪个软件好?
![[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](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[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

数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙

忆当年高考|成为程序员的你,后悔了吗?

PCL 点云镜像变换

Where can I open computer administrator permissions
随机推荐
Practice of traffic recording and playback in vivo
Data security industry series Salon (III) | data security industry standard system construction theme Salon
学生选课系统(山东农业大学课程设计)
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月份大事一览
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
[fluent] dart data type string type (string definition | string splicing | string API call)
JS learning notes - data types
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
PCL 点云镜像变换
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
Library management system (Shandong Agricultural University Curriculum Design)
隐私计算技术创新及产业实践研讨会:学习
Leetcode -- number of palindromes
Cloud native cicd framework: Tekton
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
忆当年高考|成为程序员的你,后悔了吗?
⌈ 2022 ⌋ how to use webp gracefully in projects