51单片机学习--倒车报警、超声波测距

本文介绍了如何使用MCU设计一个倒车报警系统,包括超声波测距功能,数码管显示距离,LED灯状态指示,以及蜂鸣器报警。当距离小于1米时,系统可以通过按钮关闭蜂鸣器,并在3盏LED灯闪烁3次后停止。此外,系统还实现了通过ESP8266_01s模块将距离信息发送到PC或手机,并提供了电路板的设计资源链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.任务
    用MCU(型号不限)做一个倒车报警系统。
二.要求
1.基本要求
(1)测量距离,并用数码管显示。
(2)设置3盏LED灯表示,设置如下:
    a)大于3m,点亮第1盏LED,不闪烁;
    b)大于1m,小于3m,保持第一盏LED不闪烁,同时点亮第2盏LED闪烁,时间间隔为600ms;
    c)小于1m,3盏LED以200ms闪烁,同时驱动蜂鸣器发声。
(3)距离小于1m,按下按钮后蜂鸣器停止,3盏LED灯闪烁3次后停止,时间间隔为1000ms。
(4)    循环(1)(2)(3),程序无跑丢;
(5)    最终成品,不能在开发板上实现。
2.发挥部分
   (1)通过串口与按下按钮,把当前距离,以文本的形式传递给PC或手机;(用了Esp8266_01s模块)
   (2)使用画图工具,来制作电路板。

解:

注意:代码中的esp8266_01s已经提前用串口助手配置好了wifi名字和密码,wifi应用模式为ap模式或ap+station模式都可以。ESP8266_01S的VCC接3.3V,GND与单片机共地,CH_PD端接3.3V(高电平使能),RXD接单片机的TXD,TXD接单片机的RXD。

下载链接:https://download.youkuaiyun.com/download/chenger_32123/10577310   里面包含keil工程和代码、Altium Designer软件画的原理图和PCB,可以直接做单层板。

软件:keil 4、stc-isp

1.原理图(ESP8266_01S没有在里面)

 

2.层次图

3.代码

/*******main.h****/

/*******main.h****/

#ifndef MAIN_H
#define MAIN_H

#ifndef _uchar_
#define _uchar_
typedef unsigned char uchar;
#endif

#ifndef _uint_
#define _uint_
typedef unsigned int uint;
#endif

sbit yiwei=P2^0;
sbit erwei=P2^1;
sbit sanwei=P2^2;
sbit siwei=P2^3;

sbit green_led=P1^0;
sbit yellow_led=P1^1;
sbit red_led=P1^2;

sbit fengmingqi=P1^3;
sbit K1=P1^6;
sbit K2=P1^7;

code uchar duan[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x7f,0xff};
                    //  0     1       2    3     4     5       6   7    8     9      点  关闭

void display(int);
void delay100us();
void delay1s(void);
void delay600ms(void);
void delay200ms(void);
void comint();
uchar read_key();
void key_pro(uchar);
void timer_0(void);

#endif 

 

/**main.c**/

/**main.c**/

/*******************************************

//晶振:12MHz  1T模式:1个时钟周期做一个机器周期
//串口波特率:2400bps@12MHz
*******************************************/

#include<reg51.h>
#include<intrins.h>
#include"main.h"
#include"hc_sr04_p.h"

uchar i=0;
uchar key_press;
uchar key_value;
bit key_re;
bit en_flag;
bit k1_flag;
bit k2_flag;
bit flag_1;
int temp_distance;
uchar str_1[]="AT+CIPMUX=1\r\n";  //ESP8266_01S启动多连接
uchar str_2[]="AT+CIPSERVER=1,8080\r\n";  //开启server模式,端口为8080
uchar str_3[]="AT+CIPSEND=0,10\r\n";        //向连接序号为0的客户,发送长度为10个字节数据
uchar str[]="X.XXX米      ";
uchar *p;

/********************************************************/
/*延时函数,延时1S*/   
/********************************************************/
void delay1s(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=46;c>0;c--)
        for(b=152;b>0;b--)
            for(a=70;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
}
/********************************************************/
/*延时函数,延时600mS*/   
/********************************************************/
void delay600ms(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=55;c>0;c--)
        for(b=82;b>0;b--)
            for(a=65;a>0;a--);
}
/********************************************************/
/*延时函数,延时200mS*/   
/********************************************************/
void delay200ms(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=67;c>0;c--)
        for(b=142;b>0;b--)
            for(a=9;a>0;a--);
}
/********************************************************/
/*延时函数*/         //100微秒  
/********************************************************/
void delay100us()
{
    _nop_();    _nop_();    _nop_();    _nop_();    _nop_();
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值