目录
一、主要功能
本课题基于微控制器控制器, 设计一款汽车倒车防撞报警器系统。
要求:
要求:1.配有距离, 用于把车和障碍物之间的距离信号送入控制器。
2.配有报警系统, 用于车离障碍物太近时及时报警。
3.配有键盘,用于设置安全距离;
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
编辑
三、程序编程
#include <REGX52.H>#include<intrins.h>#include<stdio.h>#include "Delay.h"#include "LCD1602.h"#include "MatrixKey.h"#define uchar unsigned char#define uint unsigned inttypedef unsigned char u8;typedef unsigned int u16;typedef unsigned char uint8;typedef unsigned int uint16;typedef unsigned long uint32;
sbit Tr=P3^0;//触发信号sbit Ec=P3^1;//回响信号sbit led = P3^2;
sbit beep=P2^7;
uchar Recv_Buffer\[4\];
uint Voltage\[\]={'0','0','0','0'};
bit bdata IIC_ERROR;
unsigned int distance;static unsigned char num;unsigned char KeyNum;unsigned int Read\_value(void);//读值函数unsigned int password,count; //初始化阈值,一共三位unsigned int jlyz;static int flag = 0;void Delay10us(void);//10us延时函数void cslsz(void);void Time0\_Init() //定时器初始化{
TMOD = 0x01;
TH0 = 0x00;
TL0 = 0x00;
TR0 = 0;//先关闭定时器0}void Time0_Int() interrupt 1 //中断程序{
TH0 = 0xfe; //重新赋值
TL0 = 0x33;
}unsigned int Read_value(){
uint result;
Tr=1;//触发引脚发出11us的触发信号(至少10us)
Delay10us();
Tr=0; while(!Ec);//度过回响信号的低电平
TR0=1;//开启定时器0
while(Ec);//度过回响信号高电平
TR0=0;//关闭定时器0
result=((TH0\*256+TL0)\*0.034)/2; // 距离cm=(时间us * 速度cm/us)/2
return result + 2; //+2修正补偿 }void main() //主函数{
uchar u;
Tr=0;//出发引脚首先拉低
LCD_Init(); //显示屏初始化
Time0_Init(); //定时初始化
beep = 0; LCD\_ShowString(1,1,"-----kaiji-----"); Delay(1000); LCD\_Init(); LCD\_ShowString(1,1,"yuzhi:"); LCD\_ShowString(2,1,"distance:");
led = 0; while(1)
{ Delay(200);
cslsz();//按键判断
distance = Read_value();//读值
LCD_ShowNum(2,10,distance,3); if(flag == 1) //判断
{ if(distance < jlyz)
{
beep = 1;
led = 1;
} else
{
beep = 0;
led = 0;
}
}
}
}void cslsz()//按键判断{
KeyNum = MatrixKey();//键盘输入的值进行传递
if(KeyNum){
if(KeyNum <= 10) //把按键的范围定义在0~9
{
if(count < 3)
{
password*=10; //左移一位
password += KeyNum % 10;
count++; //计次++
} LCD_ShowNum(1,7,password,3); //LCD更新显示
}
//确认键
if(KeyNum == 11) //把11表示确认,对阈值进行确认
{ LCD_ShowNum(1,7,password,3); //LCD更新显示
jlyz = password;
flag = 1;
}
//取消键
if(KeyNum == 12)
{
password = 0;
flag = 0;
count = 0; LCD_ShowNum(1,7,password,3); //LCD更新显示
}
if(KeyNum == 13)
{
}
if(KeyNum == 14)
{
}
if(KeyNum == 15)
{
}
if(KeyNum == 16)
{
}
}
}void Delay10us(){
TL0=0xF5;
TH0=0xFF;
TR0=1; while (TF0==0);
TR0=0;
TF0=0;
}
四、实现现象
具体动态效果看B站演示视频:
B站演示视频
基于51单片机的汽车倒车防撞报警器系统
全部资料(源程序、仿真文件、安装包、演示视频):