当前位置:网站首页>模拟摇杆控制舵机
模拟摇杆控制舵机
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); //延时
}
}
边栏推荐
- MD5 tool class
- 我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
- Scala下载和配置
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
- [acwing] solution of the 58th weekly match
- 傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
- NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
- Sqlserver encrypts and decrypts data
- 10 schemes to ensure interface data security
- NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
猜你喜欢

虚拟人产业面临的挑战

Logo special training camp section 1 Identification logo and logo design ideas

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

Kdd2022 | what features are effective for interaction?

The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development

嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧

How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)

Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient

传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)

i. Mx6ull driver development | 24 - platform based driver model lights LED
随机推荐
面试必备 LeetCode 链表算法题汇总,全程干货!
复数在数论、几何中的用途 - 曹则贤
Logo special training camp Section V font structure and common design techniques
Force buckle 3_ 383. Ransom letter
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
LOGO特训营 第二节 文字与图形的搭配关系
Shell 脚本实现应用服务日志入库 Mysql
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
High school physics: linear motion
Practice and principle of PostgreSQL join
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
sqlserver对数据进行加密、解密
微服务--开篇
Shell script implements application service log warehousing MySQL
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Logo special training camp section II collocation relationship between words and graphics
Locust performance test - environment construction and use
PMO: compare the sample efficiency of 25 molecular optimization methods