当前位置:网站首页>模拟摇杆控制舵机
模拟摇杆控制舵机
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); //延时
}
}
边栏推荐
- 传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
- leetcode 72. Edit Distance 编辑距离(中等)
- # 2156. Find the substring of the given hash value - post order traversal
- Huawei Nova 10 series released Huawei application market to build a solid application security firewall
- How can the advertising system of large factories be upgraded without the presence of large models
- Concurrent network modular reading notes transfer
- 嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
- LOGO特训营 第三节 首字母创意手法
- Use blocconsumer to build responsive components and monitor status at the same time
- Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
猜你喜欢

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

Energy momentum: how to achieve carbon neutralization in the power industry?

Concurrent optimization summary

Locust性能测试 —— 环境搭建及使用

It is said that software testing is very simple, but why are there so many dissuasions?

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

Convolutional neural network model -- lenet network structure and code implementation

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

Scala download and configuration

Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
随机推荐
力扣3_383. 赎金信
复数在数论、几何中的用途 - 曹则贤
面试必备 LeetCode 链表算法题汇总,全程干货!
不同环境相同配置项的内容如何diff差异?
How to reset the password of MySQL root account
Logo special training camp section 1 Identification logo and logo design ideas
BigFilter全局交易防重组件的介绍与应用
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
Logo special training camp section II collocation relationship between words and graphics
# 2156. Find the substring of the given hash value - post order traversal
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
Shell script implements application service log warehousing MySQL
md5工具类
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
LOGO special training camp section I identification logo and Logo Design Ideas
Play with grpc - go deep into concepts and principles
PostgreSQLSQL高级技巧透视表