Smart car based on 51 single chip microcomputer (tracking, obstacle avoidance, bluetooth control)

Article directory

  • foreword
  • 1. Function
  • 2. Main modules
    • 1. L298N module
    • 2. Tracking module
    • 3. Infrared obstacle avoidance module
    • 4. Ultrasonic obstacle avoidance module
    • 5. Bluetooth module
    • the code
  • 3. Other modules
  • Four. Summary

 


foreword

This project can realize the switching of car infrared tracking, L298N motor drive, infrared obstacle avoidance, ultrasonic obstacle avoidance, Bluetooth control and other functions.

 

1. Function

Infrared tracking, L298N motor drive, PWM motor speed regulation, infrared obstacle avoidance, ultrasonic obstacle avoidance, Bluetooth control,

2. Module introduction

1.L298N

L298N is a commonly used DC motor driver board. OUT1~4 are connected to four motors. Bloggers use the method of connecting two motors in parallel.

The two left and right motors are connected in parallel), IN1~4 are connected to the I/O port of the microcontroller, and there are enable ENA and ENB interfaces on both sides of IN1~4 for PWM speed change.

 code:

#include <REGX52.H>

sbit M1A=P0^6;                            //定义左侧电机驱动A端
sbit M1B=P0^5;                            //定义左侧电机驱动B端

sbit M2A=P0^4;                            //定义右侧电机驱动A端
sbit M2B=P0^3;                            //定义右侧电机驱动B端

sbit M3A=P3^6;                            //定义后左侧电机驱动A端
sbit M3B=P3^5;                            //定义后左侧电机驱动B端

sbit M4A=P3^4;                            //定义后右侧电机驱动A端
sbit M4B=P3^3;                             //定义后右侧电机驱动B端

sbit EN1A = P0^7;
sbit EN1B = P0^2;

sbit EN2A = P3^7;
sbit EN2B = P3^2;

unsigned char coutern,compare;

void yundong()  //  
{
    M1A = 0;
    M1B = 1;
    M2A = 0;
    M2B = 1;
    
    M3A = 1;
    M3B = 0;
    M4A = 1;
    M4B = 0;
    
}

void Timer0_Init(void)        //100微秒@11.0592MHz
{
//    AUXR &= 0x7F;            //定时器时钟12T模式
    TMOD &= 0xF0;            //设置定时器模式
    TMOD |= 0x01;            //设置定时器模式
    TL0 = 0xA4;                //设置定时初始值
    TH0 = 0xFF;                //设置定时初始值
    TF0 = 0;                //清除TF0标志
    TR0 = 1;            //定时器0开始计时
    ET0 = 1;
    EA = 1;    
    PT0=0;
}


void Timer0_Serve() interrupt 1
{
    TL0 = 0xA4;                //设置定时初始值
    TH0 = 0xFF;           //设置定时初始值 
    compare = 10;
    coutern ++;
    if(coutern > 100)
    {
        coutern = 0;
    
    }
    if(coutern < compare)
    {
        EN1A = 1;
        EN1B = 1;
        EN2A = 1;
        EN2B = 1;
    }
    else
    {
        EN1A = 0;
        EN1B = 0;
        EN2A = 0;
        EN2B = 0;
        
    }
}

void main()
{
    
    Timer0_Init();
    while(1)
    {
        yundong();
    }        
}

 

2. Tracking module

 The four-way tracking module used here

Port introduction: OUT1-4 ports of the motherboard are respectively connected to the IO port of the MCU for detecting the output level. The VCC GND IN1-4 ports on the other side of the motherboard are respectively connected to the IN port of the probe and connected to the OUT port.

Module principle: When the infrared emission tube of the probe emits light, it is reflected by the ground and enters the receiving tube. The output terminal outputs a low level, and the corresponding LED light on the main board does not light up. If there is a black area on the ground that will absorb the light, the receiving tube will output a high level  , and the LED on the main board will be lit.

 

 

code: 

#include <REGX52.H>
#include <intrins.H>
#include <Delay.h>
#include <xingshi.h>

sbit  D1 = P2^7;  //D1,D2为右边循迹模块                    
sbit  D2 = P2^6; 
sbit  D3 = P2^5;  //D3,D4为左边循迹模块
sbit  D4 = P2^4; 

void xunji()         //高电平检测到,低电平为检测
{
    if(D1==1&&D2==1&&D3==1&&D4==1)  //检测到黑线,无返回
    {
        qianji();
    }
//*****************************************
    
    if(D1==0&&D2==1&&D3==0&&D4==0)   //右边检测到黑线,小车偏左,让车向右移动
    {        
        youzhuan();
            if(D1==0&&D2==0&&D3==0&&D4==0)   //无黑线,检测到白线
            {            
                    qianji();
            }
    }
    
//******************************************
    
    if(D1==0&&D2==0&&D3==1&&D4==0)   //左边检测到黑线,小车偏右,让车向左移动
    {        
        zuozhuan();
            if(D1==0&&D2==0&&D3==0&&D4==0)   //无黑线,检测到白线
            {            
                    qianji();
            }
    }    
    
//***********************************************
    
  if(D1==0&&D2==0&&D3==1&&D4==1)    //左边检测到黑线,直角左拐
    {        
        qianji();
        Delay(50);   //直角左拐前延时50ms
            if(D1==0&&D2==0&&D3==0&&D4==0)   //无黑线,检测到白线
            {            
                    tingzhi();
                    Delay(50);
                  zuozhuan();
            }
    }    

//*********************************************
     if(D1==0&&D2==0&&D3==1&&D4==1)   //右边检测到黑线,直角右拐
    {        
        qianji();
        Delay(50);   //直角右拐前延时50ms
            if(D1==0&&D2==0&&D3==0&&D4==0)   //无黑线,检测到白线
            {            
                    tingzhi();
                    Delay(50);
                  youzhuan();
            }
    }    
    
//***************************************************    
}

 

3. Infrared obstacle avoidance module

The sensor module has a strong ability to adapt to ambient light. It has a pair of infrared emitting and receiving tubes. The emitting tube emits infrared rays of a certain frequency. When the detection direction encounters an obstacle (reflecting surface), the infrared rays are reflected back and received by the receiving tube. After being processed by the comparator circuit, the green indicator light will light up, and the signal output will be connected back to the output digital signal (a low-level signal). The detection distance can be adjusted through the potentiometer knob. The effective distance range is 2-30cm, and the working voltage is 3.3v -5v. The detection distance of the sensor can be adjusted by the potentiometer. It has the characteristics of small interference, easy assembly, and convenient use. It can be widely used in many occasions such as robot obstacle avoidance, obstacle avoidance car, assembly line counting, and black and white line tracking. \n\nModule Parameters\nWhen the module detects the obstacle signal ahead, the green indicator light on the circuit board will light up, and the OUT port will continuously output a low-level signal\n\nThe module measures a distance of 2~~30cm, and the detection The angle is 35°, the detection distance can be adjusted by the potentiometer, the detection distance increases when the potentiometer is adjusted clockwise; the detection distance decreases when the potentiometer is adjusted counterclockwise. \n\nThe sensor is active infrared reflection detection, so the reflectivity and shape of the target is the key to the detection distance. Among them, the detection distance of black is small, while that of white is large; the distance of small-area objects is small, and the distance of large-area objects is large. \n\nThe output port OUT of the sensor module can be directly connected to the IO port of the microcontroller, or it can directly drive a 5v relay\n\nThe comparator adopts LM393, which works stably;\n\nA 3-5v DC power supply can be used to The module supplies power. When the power is turned on, the red power indicator light is on;\n\nWith 3mm screw holes, it is easy to fix and install;\n\nThe size of the circuit board: 3.2CM*1.4CM\n\nEach module is released The goods have already adjusted the threshold comparison voltage through the potentiometer, please do not adjust the potentiometer arbitrarily except for special circumstances. \n\nInterface description\n1. VCC is externally connected to 3.3v-5v voltage (can be directly connected to 5v single-chip microcomputer and 3.3v single-chip microcomputer)\n\n2, GND is externally connected to GND\n\n3, OUT small board digital output interface (0 and 1).

 

 code:

 

#include <REGX52.H>
#include <Delay.H>
#include <xingshi.H>


sbit out1 = P1^1;  //左边的红外探头
sbit out2 = P1^2;  //右边的红外探头
   

void HWbizhang()
{
    if(out1 == 0)     //左边检测到障碍物
    {
        tingzhi() ;     //小车停止
        Delay(500);            //停止500ms
    houtui();              //小车后退
        Delay(1000);    //后退1000ms
        youzhuan();     //小车右转
        Delay(2000);
    qianji();         
    }
    
    if(out2 == 0)     //右边检测到障碍物
    {
        tingzhi() ;     //小车停止
        Delay(500);            //停止500ms
    houtui();              //小车后退
        Delay(1000);    //后退1000ms
        zuozhuan();     //小车左转
        Delay(2000);
    qianji();         
    }
    
    if((out1 == 1)&&(out2 == 1))
    {
        qianji();     
    }
    
    if((out1 == 0)&&(out1 == 0))
    {
        tingzhi() ;     //小车停止
        Delay(500);            //停止500ms
    houtui();              //小车后退
        Delay(1000);    //后退1000ms
        youzhuan();     //小车右转
        Delay(2000);
    qianji();         
    }
}

 4. Ultrasonic obstacle avoidance module

(1) Connect power and ground to the ultrasonic module

(2) Input a 20us high-level square wave to the pulse trigger pin (trig)

(3) After the square wave is input, the module will automatically emit 8 40KHz sound waves, and at the same time the level of the echo pin echo will change from 0 to 1; (the timer should be started at this time)

(4) When the ultrasonic return is received by the module, the level of the echo pin will change from 1 to 0; (the timer counting should be stopped at this time), and the time recorded by the timer is the ultrasonic wave from emission to return total duration of

According to the speed of sound in the air is 344 m/s, the measured distance can be calculated.

 

 

the code 

#include <REGX52.H>
#include <intrins.H>
#include <xingshi.H>
#include <Timer.H>
sbit Trig = P2^1;
sbit Echo = P2^2;


unsigned char sum; //超声波测距
unsigned char stence; //与障碍物的限制距离

//***************************************************
void Delay1000ms()		//@11.0592MHz 延时一秒
{
	unsigned char i, j, k;

	_nop_();
	i = 8;
	j = 1;
	k = 243;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

//**********************************************
void Delay20us()		//@11.0592MHz  延时20us
{
	unsigned char i;

	_nop_();
	i = 6;
	while (--i);
}

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

void chaoshengbo()
{
	
	TMOD &= 0x0F;			//设置定时器模式
	TL1 = 0;				//设置定时初始值
	TH1 = 0;				//设置定时初始值
				

	Trig = 1; //发射20US的脉冲
	Delay20us();
	Trig = 0;  //关闭
	
	while(!Echo);//等待返回脉冲
		TR1 = 1;							//打开定时器1
	while(!Echo);//返回脉冲结束
		TR1 = 0;							//关闭定时器1
	
	sum = ((TH1*256+TL1)*0.034)/2+1;					//计算距离公式
	
	if( stence > sum)
	{
		tingzhi();
		Delay1000ms();
	}
	
}






 

5. Bluetooth module

Introduction of Bluetooth module:

        The Bluetooth module can be connected with the serial port of the single-chip microcomputer, and realize asynchronous full-duplex communication with the single-chip microcomputer by means of the bluetooth of the computer or mobile phone. Common Bluetooth modules include HC-05 master-slave integrated Bluetooth module, HC-06 slave Bluetooth module, and low-power BLE Bluetooth module (cc2540 or cc2541). The AT command sets of different Bluetooth modules are not exactly the same.

        Working mode: AT mode, at this time we can send AT commands to the module, so that we can query the parameters of the module or set the module. At this time, the indicator light is blinking slowly. \nTransparent mode, that is, when the Bluetooth module is connected, we can directly send data to the Bluetooth module, and the Bluetooth module will directly transmit the data to the other party no matter what we send. (It is invalid to send AT command at this time.)

        Use of Bluetooth module: The first step in setting up and using the Bluetooth module is to set parameters, such as baud rate, etc. The method is to connect the USB-to-TTL module with the Bluetooth module. The connection method is\n\n and insert the USB-to-TTL module into the computer, and check whether the serial port is displayed in the device manager. Then use the serial port debugging assistant to write AT commands to query or operate commands. The response of the Bluetooth module will be displayed in the receiving area of ​​the serial debugging assistant.

 

 

 

code: 

#include <REGX52.H>
#include <intrins.H>
#include <xingshi.H>


#define left     'C'
#define right    'D'
#define up       'A'
#define down     'B'
#define stop     'F'

unsigned char U_data;     //接受数据存放
unsigned char Way; 


void UartInit(void)        //[email protected]
{
    PCON &= 0x7F;            //波特率不倍速
    SCON = 0x50;            //8位数据,可变波特率
//AUXR &= 0xBF;            //定时器时钟12T模式
//AUXR &= 0xFE;            //串口1选择定时器1为波特率发生器
    TMOD &= 0x0F;             //设置定时器模式
    TMOD |= 0x20;             //设置定时器模式
    TL1 = 0xFD;                //设置定时初始值
    TH1 = 0xFD;                //设置定时重载值
    ET1 = 0;                     //禁止定时器中断
    TR1 = 1;                  //定时器1开始计时
    EA  = 1;
    ES  = 1;
}

void receive(unsigned char m)
{
    
    switch(m)
    {
        
        case  'A':      //前进
            qianji();
        break;
        
        case  'C':
            zuozhuan();    // 左转
        break;
        
        case  'D':
            youzhuan();     // 右转
        break;
        
        case 'B':
            houtui();   //  后退
        break;
        
        case 'F':      // 停止
             tingzhi();
        break;
        
    }
        
}
//***************************************************************************
    
void UartInit_seve() interrupt 4
{
    
    tingzhi();
    RI = 0;                  //清除接受中断标志位
    U_data = SBUF;                    //接受数据
    receive(U_data);
    
}

 

3. Other modules 

1. sg90 steering gear

Introduction to SG90 Servo

       SG90 steering gear is a position (angle) servo drive, suitable for those control systems that require constant changes in angle and can be maintained. In the robot electromechanical control system, the control effect of the steering gear is an important factor affecting the performance. The steering gear can be used as the basic output actuator in micro-electromechanical systems and aircraft models, and its simple control and output make it very easy to interface with the single-chip microcomputer system.

SG90 servo application

      SG90 steering gear is currently widely used in high-end remote control toys, such as aircraft models, including airplane models, submarine models, and remote control robots.

SG90 servo wire

      There are three wires on the SG90 servo, namely GND (brown wire), VCC (red wire) and SIG (yellow wire), which are ground wire, power wire and signal wire.

Working Principle of SG90 Servo

      The control signal enters the signal modulation chip from the channel of the receiver to obtain the DC bias voltage. It has a reference circuit inside, which generates a reference signal with a period of 20ms and a width of 1.5ms, and compares the obtained DC bias voltage with the voltage of the potentiometer to obtain a voltage difference output. Finally, the positive and negative output of the voltage difference is sent to the motor driver chip to determine the positive and negative rotation of the motor. When the motor speed is constant, the potentiometer is driven to rotate through the cascaded reduction gear, so that the voltage difference is 0, and the motor stops rotating. Of course, we don't need to understand its specific working principle, it is enough to know its control principle. Just like we use a transistor, we just need to know that it can be used as a switch tube or an amplifier tube. As for how the electrons in the tube flow, we don't need to think about it at all.
 

 

#include <REGX52.H>
#include <intrins.H>
#include <Delay.h>
#include <xingshi.h>
#include  <Timer1.h>

sbit PWM = P1^0;   //型号输入点

unsigned char counter,angle;       //计数值和旋转角度


void Timer1_Init()		//500微秒@11.0592MHz
{
//	AUXR &= 0xBF;			//定时器时钟12T模式
	TMOD &= 0x0F;			//设置定时器模式
	TL1 = 0x33;				//设置定时初始值
	TH1 = 0xFE;				//设置定时初始值
	TF1 = 0;				//清除TF1标志
	TR1 = 1;	
	ET1 = 1;
	EA  = 1;
  PT1 = 1;	
	
}

//**************************************************
void Timer1_Sever() interrupt 3
{
	
	TL1 = 0x33;				//设置定时初始值
	TH1 = 0xFE;				//设置定时初始值
	counter++;
	
	if(counter >= 40)  //设置周期为2毫秒
	{
	  counter=0;
	}
	
	if(counter < angle)
	{
	  PWM = 1;	
	}
	else
	{
		PWM = 0;
	}
}

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

void Duoji()    //定时器定时500微妙
{

	counter = 0; 
	angle = 1;     //   0度
	Delay(500);

	counter = 0;     
	angle = 2;     //   45度  (-45)
	Delay(500);
	
	counter = 0;
	angle = 3;     //  90度(正)归中
	Delay(500);
	Delay(500);
	
	counter = 0;   //  135度  (+45)
	angle = 4;
	Delay(500);
	
	counter = 0;
	angle = 5;      //  180度
	Delay(500);

	
	
}












2. Voltage regulator module

LM2596 is a commonly used switching power supply chip with a maximum input voltage of 40V and a maximum output voltage of 37V. There are 4 versions of LM2596, 3 fixed output versions 3.3V, 5V, 12V, and an ADJ adjustable version with a maximum output current of 3A , the conversion efficiency can reach about 80%~90%. Using the DCDC module 1205 module to realize 12V to 5V output, the principle of voltage conversion using the 1205 module is also simple. Another advantage is that the input and output are isolated. The output power of the chip is 2W, the maximum The output current is 400mA.

 Four. Summary

There are many types of smart cars based on the 51 single-chip microcomputer, and the blogger is also a beginner. Please correct me if there are any mistakes in the article. I hope this article will help everyone, and everyone will work together! ! !

Guess you like

Origin blog.csdn.net/dxt258013/article/details/129900598