当前位置:网站首页>模拟摇杆控制舵机
模拟摇杆控制舵机
2022-07-04 22:17:00 【让一切都燃烧】
任务:
电位器和舵机组合应用,如果是180°舵机,电位器有效范围也是180°,如果是90°舵机,电位器有效范围为90°,电位器和舵机角度位置同步,电位器有效起点自定,终点为(起点+90°/180°)
思路
洋桃开发板上电位器是左边的全向摇杆,使用摇杆的x轴或者y轴控制舵机角度
例如摇杆在最左侧对应舵机0°,拉到最右侧舵机转到180°
舵机的角度在摇杆的起点和终点上均匀分布
摇杆本质是可调电阻,电阻分压后可以使用单片机的ADC获取位置状态
完成代码
int main (void){
//主程序
delay_ms(500); //上电时等待其他器件就绪
RCC_Configuration(); //系统时钟初始化
TOUCH_KEY_Init();//触摸按键初始化
RELAY_Init();//继电器初始化
TIM3_PWM_Init(59999,23);
ADC_Configuration(); //ADC初始化设置(模拟摇杆的ADC初始化)
JoyStick_Init(); //模拟摇杆的按键初始化
I2C_Configuration();//I2C初始化
OLED0561_Init(); //OLED初始化
OLED_DISPLAY_8x16_BUFFER(0," YoungTalk "); //显示字符串
OLED_DISPLAY_8x16_BUFFER(2," ADC TEST "); //显示字符串
OLED_DISPLAY_8x16_BUFFER(4," ADC_IN6: "); //显示字符串
OLED_DISPLAY_8x16_BUFFER(6," ADC_IN7: "); //显示字符串
while(1){
//将光敏电阻的ADC数据显示在OLED上
OLED_DISPLAY_8x16(4,10*8,ADC_DMA_IN[0]/1000+0x30);//
OLED_DISPLAY_8x16(4,11*8,ADC_DMA_IN[0]%1000/100+0x30);//
OLED_DISPLAY_8x16(4,12*8,ADC_DMA_IN[0]%100/10+0x30);//
OLED_DISPLAY_8x16(4,13*8,ADC_DMA_IN[0]%10+0x30);//
OLED_DISPLAY_8x16(6,10*8,ADC_DMA_IN[1]/1000+0x30);//
OLED_DISPLAY_8x16(6,11*8,ADC_DMA_IN[1]%1000/100+0x30);//
OLED_DISPLAY_8x16(6,12*8,ADC_DMA_IN[1]%100/10+0x30);//
OLED_DISPLAY_8x16(6,13*8,ADC_DMA_IN[1]%10+0x30);//
//方法一:分区间控制(简单但是麻烦)
// if(ADC_DMA_IN[0]<100)
// TIM_SetCompare3(TIM3,1500);
//
// if(ADC_DMA_IN[0]>=100&&ADC_DMA_IN[0]<1700)
// TIM_SetCompare3(TIM3,ADC_DMA_IN[0]+1500);
// if(ADC_DMA_IN[0]>=1700&&ADC_DMA_IN[0]<2000)
// TIM_SetCompare3(TIM3,ADC_DMA_IN[0]+2300);
//
// if( ADC_DMA_IN[0]>=2000&&ADC_DMA_IN[0]<3500)
// TIM_SetCompare3(TIM3,ADC_DMA_IN[0]+2500);
// if( ADC_DMA_IN[0]>=3500&&ADC_DMA_IN[0]<4000)
// TIM_SetCompare3(TIM3,ADC_DMA_IN[0]+3500);
//
// if( ADC_DMA_IN[0]>4000)
// TIM_SetCompare3(TIM3,7500);
// delay_ms(10);
//方法二:写公式转换(难但是方便)
TIM_SetCompare3(TIM3,ADC_DMA_IN[0]*(3.3/4096)*1820+1500);
if(GPIO_ReadInputDataBit(JoyStickPORT,JoyStick_KEY)==0){
OLED_DISPLAY_8x16(0,0,'Y');//
}else{
OLED_DISPLAY_8x16(0,0,' ');//
}
delay_ms(200); //延时
}
}
边栏推荐
- Easy to use app recommendation: scan QR code, scan barcode and view history
- 复数在数论、几何中的用途 - 曹则贤
- Service online governance
- More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
- Locust性能测试 —— 环境搭建及使用
- 业务太忙,真的是没时间搞自动化理由吗?
- High school physics: linear motion
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- Tiktok actual combat ~ the number of comments is updated synchronously
- UML图记忆技巧
猜你喜欢

Use blocconsumer to build responsive components and monitor status at the same time

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元

Business is too busy. Is there really no reason to have time for automation?

Common open source codeless testing tools

Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei

质量体系建设之路的分分合合

SPSS installation and activation tutorial (including network disk link)

Locust performance test - environment construction and use

凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer

SPSS安装激活教程(包含网盘链接)
随机推荐
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
Concurrent network modular reading notes transfer
You don't have to run away to delete the library! Detailed MySQL data recovery
leetcode 72. Edit distance edit distance (medium)
卷积神经网络模型之——LeNet网络结构与代码实现
ACM multimedia 2022 | counterfactual measurement and elimination of social prejudice in visual language pre training model
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
常用的开源无代码测试工具
Close system call analysis - Performance Optimization
并发优化总结
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
PostgreSQL server programming aggregation and grouping
力扣3_383. 赎金信
Shell script implements application service log warehousing MySQL
Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
Energy momentum: how to achieve carbon neutralization in the power industry?