目录
一、主要功能
基于51单片机,搞L298N驱动两个电机转动,然后搞LCD1602显示屏,弄个超声波传感器实时检测距离 通过LCD1602显示距离,如果距离小于阈值,则两个电机停止,蜂鸣器报警,灯光亮起。如果大于阈值,说明没有障碍物,电机正常运作。有两个按键,一个是自动按键,就是按下后靠超声波来避障。一个是手动按键,按下后,由电脑云端也就模拟语音来控制小车的运转,比如发现0x01,让小车停止运行,发送0x02,让小车正常运行。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
三、程序编程
cpp
#include <REGX52.H>
#include<intrins.h>
#include<stdio.h>
#include "Delay.h"
#include "LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
sbit CS=P1^0; //51单片机引脚设置
sbit CLK=P1^1;
sbit DIO=P1^2;
sbit Motor1_IN1 = P3^3; //电机IN1口
sbit Motor1_IN2 = P3^4;
sbit Motor2_IN1 = P1^0; //电机IN2口
sbit Motor2_IN2 = P1^1;
sbit LED = P1^5; //LED灯定义
sbit key1 = P1^4;
sbit key2 = P1^6;
sbit BEEP = P1^3;
sbit Tr=P3^5;//触发信号
sbit Ec=P3^6;//回响信号
uchar Recv_Buffer[4];
uint Voltage[]={'0','0','0','0'};
bit bdata IIC_ERROR;
static int flag = 0;
unsigned int distance;
static char Dat_rxd;
unsigned int Read_value(void);//读值函数
void Delay10us(void);//10us延时函数
void Time0_Init() //定时器初始化
{
TMOD = 0x21;
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 Com0_Init(void)
{
EA=1; //开总中断
ES=1; //允许串口中断
TMOD=0x21; //定时器T1,在方式3中断产生波特率
SCON=0x50;
TH1=0xfd; //波特率设置为9600(晶振12M)
TL1=0xfd;
TR1=1; //开定时器T1运行控制位
// TI=1;
}
void Com0(void) interrupt 4 using 1
{
if(RI)
{
Dat_rxd=SBUF; //串口接收
Dat_rxd=Dat_rxd-0x30;
RI=0;
}
}
void main() //主函数
{
Tr=0; //出发引脚首先拉低
LCD_Init(); //显示屏初始化
LCD_ShowString(1,1,"Distance:"); //显示字符串Distance
Time0_Init(); //定时初始化
Com0_Init();//串口初始化
LED = 0;
BEEP = 0;
while(1)
{
distance = Read_value();//读值
distance+=3;
LCD_ShowNum(1,10,distance,3); //显示距离
if(flag == 1)
{
if(distance>=50)
{
Motor1_IN1 = 1;
Motor1_IN2 = 0;
Motor2_IN1=1;
Motor2_IN2=0;
LED = 0; //LED灯熄灭
BEEP = 0; //蜂鸣器关掉
}
else if(distance < 50) //超声波距离判断
{
Motor1_IN1 = 0;
Motor1_IN2 = 0;
Motor2_IN1 = 0;
Motor2_IN2 = 0;
LED = 1; //LED灯亮起
BEEP = 1; //蜂鸣器响起
}
}
if(flag == 2)
{
if(Dat_rxd==0x01)
{
Motor1_IN1 = 0;
Motor1_IN2 = 0;
Motor2_IN1 = 0;
Motor2_IN2 = 0;
}
else if(Dat_rxd==0x02)
{
Motor1_IN1 = 1;
Motor1_IN2 = 0;
Motor2_IN1=1;
Motor2_IN2=0;
}
else if(Dat_rxd==0x03)
{
Motor1_IN1 = 1;
Motor1_IN2 = 0;
Motor2_IN1=0;
Motor2_IN2=0;
}
else if(Dat_rxd==0x04)
{
Motor1_IN1 = 0;
Motor1_IN2 = 0;
Motor2_IN1=1;
Motor2_IN2=0;
}
}
if(key1 == 0)
{
Delay(100); //消除抖动
if(key1 == 0)
{
flag = 1;
}
}
if(key2 == 0)
{
Delay(100);//消除抖动
if(key2 == 0)
{
flag = 2;
}
}
}
}
void Delay10us()
{
TL0=0xF5;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
四、实现现象
具体动态效果看B站演示视频:
基于单片机的智能语音识别与处理系统设计_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、演示视频):