当前位置:网站首页>模拟摇杆控制舵机
模拟摇杆控制舵机
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); //延时
}
}
边栏推荐
- 【烹饪记录】--- 青椒炒千张
- LOGO特训营 第四节 字体设计的重要性
- How to reset the password of MySQL root account
- Postgresqlql advanced skills pivot table
- In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
- Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei
- Jvm-Sandbox-Repeater的部署
- odps 中 对表进行了一次备份,为什么在元数据库查m_table 时,两张表的逻辑大小不一致,但数
- NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
- Scala download and configuration
猜你喜欢

Tla+ introductory tutorial (1): introduction to formal methods

LOGO特训营 第三节 首字母创意手法

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

Machine learning notes mutual information

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

30余家机构联合发起数字藏品行业倡议,未来会如何前进?

安装人大金仓数据库

Logo Camp d'entraînement section 3 techniques créatives initiales

LOGO特训营 第二节 文字与图形的搭配关系

蓝队攻防演练中的三段作战
随机推荐
PostgreSQL服务端编程聚合和分组
Challenges faced by virtual human industry
面试必备 LeetCode 链表算法题汇总,全程干货!
Force buckle 3_ 383. Ransom letter
Logo special training camp section III initial creative techniques
leetcode 72. Edit distance edit distance (medium)
抖音实战~评论数量同步更新
LOGO特训营 第五节 字体结构与设计常用技法
POM in idea XML dependency cannot be imported
Why should garment enterprises talk about informatization?
Jvm-Sandbox-Repeater的部署
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
力扣_回文数
Logo special training camp Section V font structure and common design techniques
Shell script implements application service log warehousing MySQL
10 schemes to ensure interface data security
Detailed explanation of flask context
SPSS installation and activation tutorial (including network disk link)
Recommendation of mobile app for making barcode
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game