51单片机,智能垃圾桶,超声破测距,语音提示
Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)
单片机源程序如下:
- #include <STC89C5xRC.H>
- #include <intrins.h>
- #include <stdio.h>
- #include "HC_SR04.h"
- #include "usart1.h"
- #include "WT588D.h"
- #include "StepMotor.h"
- void Delay50ms() //@11.0592MHz
- {
- unsigned char i, j;
- i = 90;
- j = 163;
- do
- {
- while (--j);
- } while (--i);
- }
- char xdata buff[128];
- unsigned int dis=420;
- char flg=0,flg2=0,flg3=0;
- unsigned int cnt=0;
- // may everything go well for you who will graduate in front of the screen.
- // but how about learning more in the future, tough girl?
- //Have bought a ticket to xiangtan, you look at it, I changed the code in the train station。
- void main()
- {
- HC_SR04_Init();//超声波初始化
- GoBackSM();//步进电机初始化,寻找垃圾箱盖子关闭的地方
- while(1)
- {
-
- dis=getdis();//超声波测距把距离赋给dis
- if(dis<20)//设置垃圾箱盖子打开时的距离
- {
- if(flg==1)//判断标志位
- {
- cnt=0;
- P25=1;//关消毒灯
- flg=0;
- flg2=0;
- if(P20==1)Send_threelines(0);//播放垃圾放置请分类
- Go90A();//转90°
- while(getdis()<30)//如果上面的距离改了,这里也要改
- {
- Delay50ms();//丢垃圾啊丢垃圾,屏幕前的你在丢垃圾
- }
- }
- }
- else//垃圾丢完,人走了
- {
- GoBackSM();//垃圾丢完了,垃圾盖归位
- if(P20==0)//检测垃圾桶满了没,满了进入if内执行任务
- {
- if(flg3==0)
- {
- flg3=1;
- Send_threelines(1);//播报垃圾已满,铲屎的,该倒垃圾了
- }
- }
- else//垃圾箱没满
- flg3=0;//复位标志位
-
- if(flg2==0)//判断第二个标志位是否为0
- {
- cnt++;//开始计算消毒时间
- P25=0;//消毒开始,只有在垃圾箱盒子合上才会动作
- }
- if(cnt>=1200)//消毒时间到
- {
- flg2=1;//置为第二个标志位
- P25=1;//关闭消毒指示
- }
-
- flg=1;//置为第一个标志位
- }
-
- Delay50ms();//单次循环50ms
- }
- }
复制代码
所有资料51hei提供下载:
资料合集垃圾箱.zip
(1.85 MB, 下载次数: 370)
|