当前位置:网站首页>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.参考视频
资料已上传,需要自取
边栏推荐
- Bugku's Ah Da
- Redis distributed lock principle and its implementation with PHP (1)
- MySQL5.7的JSON基本操作
- sql server学习笔记
- Good article inventory
- OSI 七层模型
- Data communication foundation ACL access control list
- Data communication foundation - route republication
- OSI seven layer model
- Appium automation test foundation - appium basic operation API (I)
猜你喜欢

lv_ font_ Conv offline conversion

D-snow halo solution

Thymeleaf uses background custom tool classes to process text

Bugku's eyes are not real

超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜

Number protection AXB function! (essence)

Explanation report of the explosion

Surpass palm! Peking University Master proposed diverse to comprehensively refresh the NLP reasoning ranking

Fundamentals of data communication - Principles of IP routing

Your childhood happiness was contracted by it
随机推荐
D-snow halo solution
Aike AI frontier promotion (7.5)
JS knowledge points-01
I'm fat, huh
keep-alive
mapper.xml文件中的注释
First PR notes
I spring and autumn blasting-2
How to introduce devsecops into enterprises?
【 note 】 résoudre l'erreur de code IDE golang
MySQL giant pit: update updates should be judged with caution by affecting the number of rows!!!
Data communication foundation NAT network address translation
Huawei Hubble incarnation hard technology IPO harvester
基于OpenHarmony的智能金属探测器
Bugku easy_ nbt
Common redis data types and application scenarios
把 ”中台“ 的思想迁移到代码中去
Memo 00
No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
修改pyunit_time使得其支持‘xx~xx月’的时间文本