当前位置:网站首页>20.[STM32]利用超声波模块和舵机实现智能垃圾桶功能
20.[STM32]利用超声波模块和舵机实现智能垃圾桶功能
2022-07-05 15:18:00 【依点_DW】
作者简介:大家好啊,我叫DW,每天分享一些我新学到的知识,期待和大家一起进步
系列专栏:STM32
小实验目标:智能垃圾桶
如有写得不好的地方欢迎大家指正开发板:STM32F103
创作时间:2022年6月27日
在之前的文章中,我们已经学习了如何使用超声波测距模块,同时也知道了如何在oled上显示测量的数据,那么今天我会把之前学过的知识进行的整合,实现智能垃圾桶的功能。
具体功能如下:
1.可以在oled上 显示测量的距离;
2.当有人靠近倒垃圾时,舵机控制垃圾桶打开;当倒完垃圾后,舵机控制垃圾桶关闭。
在上一篇文章中已经详细介绍了超声波模块的原理和使用方法,在上一篇文章的基础上,添加一个舵机程序,实现控制垃圾桶的打开和关闭。
硬件连接
OLED:
CS----PB5
DC----PB6
D0----PB7
D1----PB8
RST----PC13
GND---地
VCC---5V
HC_SR04:
Trig-----PA6
Echo----PA7
VCC-----5V
GND----地
舵机:
信号线(黄线):PB0 红线:5V 褐色线:GND
使用TIM3定时器的通道3,其对应PB0引脚
在之前的第12篇网文中已经对舵机的使用的过程进行了详细的讲解 ,该文章使用的是TIM3的CH1,本文使用的是TIM3的CH3,在之前的文章的基础上修改几个参数即可。
//配置定时器3
TIM_DeInit(TIM3);
TIM_InitStructure.TIM_Period = 200-1;
TIM_InitStructure.TIM_Prescaler = 7200-1;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_RepetitionCounter = DISABLE;
TIM_TimeBaseInit(TIM3,&TIM_InitStructure);
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
PWM_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1
PWM_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
PWM_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3,&PWM_InitStructure);//CH3
TIM_OC3PreloadConfig(TIM3,ENABLE);
TIM_SetCompare3(TIM3,0);
TIM_Cmd(TIM3,ENABLE);在第19篇文章的void HC_SR04_Debolan(u8 mode) 函数中做一下修改,当距离垃圾桶3~10cm时,垃圾桶缓慢打开,否则垃圾桶关闭。具体实现过程如下:
void HC_SR04_Debolan(u8 mode){
u16 data1 = 0,data2 = 0; u32 data = 0;
static u8 flag;
HC_SR04_Ranging(&data1,&data2);
if(mode){
for(u8 i=0;i<5;i++){
data = data + data1;
}
}
else{
for(u8 i=0;i<5;i++){
data = data + data2;
}
}
data/=5;
if((data >= 3) && (data) <= 10){
if(flag == 0){
flag=1;
for(u16 i=15;i>5;i--){
TIM_SetCompare3(TIM3,i);
delay_ms(1);
}
delay_ms(1500);//1.5s 1.86s
}
}
else{
if(flag == 1){
flag=0;
for(u16 i=5;i<15;i++){
TIM_SetCompare3(TIM3,i);
delay_ms(1);
}
}
}
OLED_Write_Number(0,30,data);
}
至此,程序全部编写完毕,编译烧录代码,成功实现了我们需要的功能。
为了方便下次查找,记得点点关注哦。
本章结束,我们下一章见
参考资料:
1.STM32固件库手册
2.正点原子STM32不完全手册_库函数版本
3.参考视频
资料已上传,需要自取
边栏推荐
- Definition of episodic and batch
- Example of lvgl display picture
- Data communication foundation smart_ Link_&_ Monitor_ Link
- Bugku's eyes are not real
- keep-alive
- Bugku easy_ nbt
- RepLKNet:不是大卷积不好,而是卷积不够大,31x31卷积了解一下 | CVPR 2022
- Ctfshow web entry information collection
- Advanced level of static and extern
- OSI 七层模型
猜你喜欢

Ctfshow web entry explosion

Detailed explanation of C language branch statements

Appium自动化测试基础 — APPium基础操作API(二)

Creation and optimization of MySQL index

Example of lvgl display picture

Misc Basic test method and knowledge points of CTF
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Bugku telnet

SQL Server learning notes

Data communication foundation - routing communication between VLANs
随机推荐
Nine hours, nine people, nine doors problem solving Report
机械臂速成小指南(九):正运动学分析
Value series solution report
Bubble sort, insert sort
Aike AI frontier promotion (7.5)
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Bugku's Ah Da
2.3 learning content
ICML 2022 | 探索语言模型的最佳架构和训练方法
mapper. Comments in XML files
Au - delà du PARM! La maîtrise de l'Université de Pékin propose diverse pour actualiser complètement le classement du raisonnement du NLP
六种常用事务解决方案,你方唱罢,我登场(没有最好只有更好)
"Sequelae" of the withdrawal of community group purchase from the city
go学习 ------jwt的相关知识
Appium自动化测试基础 — APPium基础操作API(一)
[brief notes] solve the problem of IDE golang code red and error reporting
Basic JSON operations of MySQL 5.7
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Lesson 4 knowledge summary