laitimes

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

author:Embedded XFF

Design and implementation of intelligent balance bike based on PID control

  • Difficulty factor: 4 points
  • Workload: 4 points
  • Innovation: 3 points

1 Introduction

Based on the anti-rear-end system developed by 52 single-chip microcomputer, the HC-SR04 ultrasonic detection module is used in the system to detect the distance of obstacles behind the car, and identify whether there are obstacles in front of the vehicle through the photocell, if there are obstacles in front, it will display "there is an obstacle ahead" on the OLED display, and the distance data detected by the ranging module will also be displayed on the OLED screen. In addition, the system is also equipped with Hall speed module, and the real-time vehicle speed is displayed on the display, through the button can adjust the speed threshold, when the speed exceeds the threshold and the distance behind the obstacle is less than the threshold, the buzzer will alarm and control the opening relay (brake).

2 Main components

  • 52 microcontrollers
  • HC-SR04 ultrasonic testing
  • buzzer
  • OLED screen
  • Photocell

3 Achieve the effect

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)
Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

4 Design principles

4.1 HC-SR04 ultrasonic mold

Brief introduction

HC-SR04 ultrasonic module is commonly used in robot obstacle avoidance, object ranging, liquid level detection, public security, parking lot detection and other places. The HC-SR04 ultrasonic module is mainly composed of two general-purpose piezoelectric ceramic ultrasonic sensors and peripheral signal processing circuits. As shown in the picture:

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

Two piezoceramic ultrasonic sensors, one for emitting ultrasonic signals and one for receiving the reflected ultrasonic signals. Since the emitted signal and the received signal are relatively weak, it is necessary to increase the power of the emitted signal through the peripheral signal amplifier and amplify the reflected signal to transmit the signal to the single-chip microcomputer more stably. The overall circuit of the module is shown in the figure:

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

Module parameters

(1) Main electrical parameters of the module

  • Operating voltage: DC-5V
  • Quiescent current: less than 2mA
  • Level output: 5V high
  • Level output: bottom 0V
  • Sensing angle: not more than 15 degrees
  • Detection distance: 2cm-450cm
  • High accuracy up to 0.2cm

(2) Module pins

The ultrasonic module has 4 pins, namely Vcc, Trig (control end), Echo (receiving end), GND; Among them, VCC and GND are connected to the 5V power supply, Trig (control end) controls the ultrasonic signal emitted, and Echo (receiving end) receives the reflected ultrasonic signal. The module is shown in the figure

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

4.2 Diffuse mode sensor

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

Brief introduction

Photoelectric switch is a kind of displacement sensor with switching output, output NPN, PNP, normally open, normally closed and relay, etc., can detect metal (such as steel, iron, copper), plastic, glass, wood, water, paper, magnets and other transparent and opaque objects, can be connected with PLC, servo controller, inverter, calculator, controller to achieve the purpose of automatic input signal, widely used in machinery, textile, light industry papermaking, printing, packaging and other industries.

Photoelectric switches are non-contact measurement sensors, and their detection distance range is relatively wide, and they are widely used in many measurement and control systems such as counting, ranging and stroke control. Reflective photoelectric switches are divided into diffuse reflection and retro-reflective photoelectric switches

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)
Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

How it works

Diffuse reflection photoelectric switch is a set of transmitter and receiver in one sensor, when there is a detected object passing, the photoelectric switch transmitter emitted a sufficient amount of light reflected to the receiver, so the photoelectric switch produces a switching signal. When the surface of the object to be detected is bright or its reflectivity is extremely high, diffuse mode photoelectric switch is the preferred detection mode.

Adjustment of detection distance

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

5 parts of core code

//超声波测距
//晶振=8M
//MCU=STC10F04XE
//P0.0-P0.6共阳数码管引脚
//Trig  = P1^0
//Echo  = P3^2
#include <reg52.h>     //包括一个52标准内核的头文件
#define uchar unsigned char //定义一下方便使用
#define uint  unsigned int
#define ulong unsigned long
//***********************************************
sfr  CLK_DIV = 0x97; //为STC单片机定义,系统时钟分频
                     //为STC单片机的IO口设置地址定义
sfr   P0M1   = 0X93;
sfr   P0M0   = 0X94;
sfr   P1M1   = 0X91;
sfr   P1M0   = 0X92;
sfr	P2M1   = 0X95;
sfr	P2M0   = 0X96;
//***********************************************
sbit Trig  = P1^0; //产生脉冲引脚
sbit Echo  = P3^2; //回波引脚
sbit test  = P1^1; //测试用引脚

uchar code SEG7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//数码管0-9
uint distance[4];  //测距接收缓冲区
uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,i;  //自定义寄存器
bit succeed_flag;  //测量成功标志
//********函数声明
void conversion(uint temp_data);
void delay_20us();
//void pai_xu();

void main(void)   // 主程序
{  uint distance_data,a,b;
   uchar CONT_1;   
   CLK_DIV=0X03; //系统时钟为1/8晶振(pdf-45页) 
     P0M1 = 0;   //将io口设置为推挽输出
     P1M1 = 0;
     P2M1 = 0;
     P0M0 = 0XFF;
     P1M0 = 0XFF;
     P2M0 = 0XFF;
   i=0;
   flag=0;
	test =0;
	Trig=0;       //首先拉低脉冲输入引脚
	TMOD=0x11;    //定时器0,定时器1,16位工作方式
	TR0=1;	     //启动定时器0
   IT0=0;        //由高电平变低电平,触发外部中断
	ET0=1;        //打开定时器0中断
 //ET1=1;        //打开定时器1中断
	EX0=0;        //关闭外部中断
	EA=1;         //打开总中断0	
  
	
while(1)         //程序循环
	{
  EA=0;
	     Trig=1;
        delay_20us();
        Trig=0;         //产生一个20us的脉冲,在Trig引脚  
        while(Echo==0); //等待Echo回波引脚变高电平
	     succeed_flag=0; //清测量成功标志
	     EX0=1;          //打开外部中断
	 	  TH1=0;          //定时器1清零
        TL1=0;          //定时器1清零
	     TF1=0;          //
        TR1=1;          //启动定时器1
   EA=1;

      while(TH1 < 30);//等待测量的结果,周期65.535毫秒(可用中断实现)  
		  TR1=0;          //关闭定时器1
        EX0=0;          //关闭外部中断

    if(succeed_flag==1)
	     { 	
		   distance_data=outcomeH;                //测量结果的高8位
           distance_data<<=8;                   //放入16位的高8位
		     distance_data=distance_data|outcomeL;//与低8位合并成为16位结果数据
            distance_data*=12;                  //因为定时器默认为12分频
           distance_data/=58;                   //微秒的单位除以58等于厘米
         }                                      //为什么除以58等于厘米,  Y米=(X秒*344)/2
			                                       // X秒=( 2*Y米)/344 ==》X秒=0.0058*Y米 ==》厘米=微秒/58 
    if(succeed_flag==0)
		   {
            distance_data=0;                    //没有回波则清零
		   	test = !test;                       //测试灯变化
           }

     ///       distance[i]=distance_data; //将测量结果的数据放入缓冲区
     ///        i++;
  	  ///	 if(i==3)
	  ///	     {
	  ///	       distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4;
     ///        pai_xu();
     ///        distance_data=distance[1];

      
	   a=distance_data;
       if(b==a) CONT_1=0;
       if(b!=a) CONT_1++;
       if(CONT_1>=3)
		   { CONT_1=0;
			  b=a;
			  conversion(b);
			}       
	  ///		 i=0;
 	  ///		}	     
	 }
}
//***************************************************************
//外部中断0,用做判断回波电平
INTO_()  interrupt 0   // 外部中断是0号
 {    
     outcomeH =TH1;    //取出定时器的值
     outcomeL =TL1;    //取出定时器的值
     succeed_flag=1;   //至成功测量的标志
     EX0=0;            //关闭外部中断
  }
//****************************************************************
//定时器0中断,用做显示
timer0() interrupt 1  // 定时器0中断是1号
   {
 	 TH0=0xfd; //写入定时器0初始值
	 TL0=0x77;	 	
	 switch(flag)   
      {case 0x00:P0=ge; P2=0xfd;flag++;break;
	    case 0x01:P0=shi;P2=0xfe;flag++;break;
	    case 0x02:P0=bai;P2=0xfb;flag=0;break;
      }
   }
//*****************************************************************
/*
//定时器1中断,用做超声波测距计时
timer1() interrupt 3  // 定时器0中断是1号
    {
TH1=0;
TL1=0;
     }
*/
//******************************************************************
//显示数据转换程序
void conversion(uint temp_data)  
 {  
    uchar ge_data,shi_data,bai_data ;
    bai_data=temp_data/100 ;
    temp_data=temp_data%100;   //取余运算
    shi_data=temp_data/10 ;
    temp_data=temp_data%10;   //取余运算
    ge_data=temp_data;

    bai_data=SEG7[bai_data];
    shi_data=SEG7[shi_data];
    ge_data =SEG7[ge_data];

    EA=0;
    bai = bai_data;
    shi = shi_data;
    ge  = ge_data ; 
	 EA=1;
 }
//******************************************************************
void delay_20us()
 {  uchar bt ;
    for(bt=0;bt<100;bt++);
 }
/*
void pai_xu()
  {  uint t;
  if (distance[0]>distance[1])
    {t=distance[0];distance[0]=distance[1];distance[1]=t;} /*交换值
  if(distance[0]>distance[2])
    {t=distance[2];distance[2]=distance[0];distance[0]=t;} /*交换值
  if(distance[1]>distance[2])
    {t=distance[1];distance[1]=distance[2];distance[2]=t;} /*交换值	 
    }
*/

           

The full code can be collected for free by joining the group.

The learning road of embedded IoT is very long, and many people miss out on high-paying offers because the learning route is wrong or the learning content is not professional enough. But don't worry, I've put together a list of more than 150 GB of learning resources for you, basically covering everything about embedded IoT learning. Click the link below to receive learning resources for 0 yuan, making your learning road smoother! Remember to like, follow, favorite, and retweet!

Click here to find the assistant 0 yuan to receive: scan the code to enter the group to receive the information

Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)
Vehicle rear-end arrest system based on 51 microcontroller - Internet of Things embedded (with code)

Read on