Based on the 51 tracking car (the first project for beginners!!!)

       Preparation

1) Control chip:

Since I have just completed the 51 basic course, I will use the 51 learning board (STC89C52RC) directly here.

2) Body:

You can buy it directly from Taobao, and it usually comes with everything (acrylic body, motor, tires and fixing screws).

3) L298N motor drive module:

The L298N module can drive the motor to rotate forward and reverse, and can directly power the microcontroller (L298N is connected to 12V).

Output A/B: Connect to the left/right motor respectively (the motor connection is as shown in the picture above)

Channel A/B enable: When the cap is removed, the motor speed can be adjusted through PWM

Logic input: IN1/2 controls the forward and reverse rotation of the motor at output A.

IN3/4 controls the forward and reverse rotation of the motor at output B end.

The forward and reverse rotation of the motor is related to the wiring between the motor and output A/B. After wiring, you should first debug to determine whether the direction is correct. If the direction is incorrect, change the order of the wiring between the motor and output A/B.

Wiring: Wire according to the figure above, output A/B is connected to the motor, and the logic input is connected to the I/O port of the microcontroller.

4) Infrared tracking module

It is recommended to use the TCR5000 tracking module (a single tracking module can adjust the scanning position according to your actual situation)

       

 

 

     

Tracking principle: Utilize the characteristics of infrared rays having different reflection properties for different colors. When the car is driving, the infrared emitting diode of the sensor continuously emits infrared light. When the infrared light encounters the white ground, diffuse reflection occurs, and the infrared tube receiving tube receives the reflected light; if it encounters a black line, the infrared light is absorbed, and the infrared tube No signal received.
The signal collected by the infrared tube passes through the LM339 comparator in the 2-channel tracking sensor module and outputs a high or low level to achieve signal detection.

Wiring: (A0 can be left alone) D0 is the output signal connected to the I/O port of the microcontroller

Note: Some manufacturers set it so that the infrared tube cannot receive a signal and output a high level, while other manufacturers set it so that the infrared tube cannot receive a signal and output a low level. Therefore, debugging must be done after wiring (here is the output of high level)

5) Power supply

It is strongly recommended to use a 12V lithium battery. I used a 9V alkaline battery before. Although the voltage was about 9V, the current was seriously insufficient.

Therefore, you can directly use a 12V 2000ma lithium battery to directly drive the L298N and directly power the microcontroller through the L298N, and the lithium battery can be charged and used repeatedly.

Features

According to the working principle of the TCR5000 tracking module, use the microcontroller I/O port to receive the signal from the TCR5000 tracking module. When the microcontroller I/O port receives a low level/high level, it means that the track (black line) is detected, and then Give the logic input pin signal of L298N to let the motor make corresponding actions.

The car can turn by turning one tire forward and the other side not moving, but the angle of movement of the car will be very large, so it is generally not recommended; another method is to turn one tire forward and the other side reverse, so that The moving angle of the car will be much smaller, and it can even spin in place.

Source code (for reference only)

Main function (using modular programming, the subsequent header files will be added by yourself)

#include <REGX52.H>
#include "trac.h"
#include "pwm.h"


int main ()
{

	Timer0_PWM_Init();
	P0=0XFF;
	while(1)
	{
	
		Trac();
	}
}

PWM function

#include <REGX52.H>

sbit ENA=P2^0; //ENA command
sbit ENB=P2^5; //ENB command
unsigned char compare_ENA,compare_ENB;//PWM comparison value
unsigned char count_ENA,count_ENB; //PWM count value

/***********************************************************************************
  * @brief timer 0 initialization
  * @param  
  * @retval 
  */
void Timer0_PWM_Init(void) //100 [email protected]
{


	TMOD &= 0xF0; //Set timer mode
	TMOD |= 0x01; //Set timer mode
	TL0 = 0xA4; //Set the initial value of the timing
	TH0 = 0xFF; //Set the initial value of the timing
	TF0 = 0; //Clear the TF0 flag
	TR0 = 1; //Timer 0 starts timing
	ET0=1;
	EA=1;
	
}

/***********************************************************************************
  * @brief PWM comparison value assignment function
  * @param  	
  * @retval 
  */
void PWM_Compare( unsigned char Date1,Date2)
{
	compare_ENA=Date1;
	compare_ENB=Date2;
}



/***********************************************************************************
  * @brief timer 0 execution function
  * @param  
  * @retval 
  */


void Timer0_PWM_Run(void) interrupt 1
{
//	TL0 = 0xA4; //Set the initial value of the timing
//	TH0 = 0xFF; //Set the initial value of the timing
	TL0 = 0xE9; //Set the initial value of the timing
	TH0 = 0xFF; //Set the initial value of the timing
	
	count_ENA++;
	count_ENA%=100; //Control range 0~99
	if(count_ENA<compare_ENA)
	{
		ENA=1;		
	}
	else if(count_ENA>compare_ENA)
	{
		ENA=0;		
	}
	
	count_ENB++;
	count_ENB%=100; //Control range 0~99
	if(count_ENB<compare_ENB)
	{
		ENB=1;		
	}
	else if(count_ENB>compare_ENB)
	{
		ENB=0;		
	}
	
	
	
	
}

trace function

#include <REGX52.H>
#include "pwm.h"

/***********************************************************************************
  * @brief The infrared pair tube returns the signal pin from left to right

  */
sbit IR_1=P0^3;
sbit IR_2=P0^4;
sbit IR_3=P0^5;
sbit IR_4=P0^6;

/***********************************************************************************
  * @brief L298N signal output 10 forward, 01 reverse
 
  */
sbit IN1=P2^1;
sbit IN2=P2^2;
sbit IN3=P2^3;
sbit IN4=P2^4;

/***********************************************************************************
  * @brief Control the wheel rotation direction 10 forward 01 reverse
  * @param  	a,b,,c,d
  * @retval 
  */

void L298N_Init(unsigned char a,b,c,d)
{
		IN1=a;
		IN2=b;
		IN3=c;
		IN4=d;


}
/***********************************************************************************
  * @brief tracking module execution function
  * @param None
  * @retval None
  */
void Trac()
{
	if(IR_1==0 && IR_2==0 && IR_3==1 && IR_4==0 ) //A black line is detected in the middle. At this time, the car position is to the right of the center.
	{
		PWM_Compare(20,20); //Both left and right wheels are 20%
		L298N_Init(1,0,1,0); //Both left and right wheels are passed
	}
	
	else if(IR_1==1 && IR_2==0 && IR_3==0 && IR_4==0 ) //The black line is detected on the far left
	{
		PWM_Compare(30,40); //The left wheel is 30% and the right wheel is 40%
		L298N_Init(0,1,1,0); //Left wheel reverse transmission and right wheel forward rotation
	}
	
	else if(IR_1==0 && IR_2==1 && IR_3==0 && IR_4==0 ) //A black line is detected in the middle. At this time, the car is positioned to the left of the center.
	{
		PWM_Compare(23,23); //The left wheel is 23% and the right wheel is 23%
		L298N_Init(1,0,1,0); //The left wheel is forward and the right wheel is forward
	}
	

	
	else if(IR_1==0 && IR_2==0 && IR_3==0 && IR_4==1 ) //Black line detected on the far right
	{
		PWM_Compare(40,30); //The left wheel is 40% and the right wheel is 30%
		L298N_Init(1,0,0,1); //Left wheel forward and right wheel reverse
		
	}
	
	else if(IR_1==0 && IR_2==0 && IR_3==0 && IR_4==0 ) //No black line detected
	{
		PWM_Compare( 0,0);
		L298N_Init(0,0,0,0); //The tire does not move
	}
	
	else if(IR_1==1 && IR_2==1 && IR_3==1 && IR_4==1 ) //Black lines are detected in all four channels
	{
		PWM_Compare( 0,0);
		L298N_Init(0,0,0,0); //The tire does not move
	}
	
		else if(IR_1==0 && IR_2==1 && IR_3==1 && IR_4==0 ) //Black lines are detected in the middle two channels
	{
		PWM_Compare(17,17); //The left wheel is 17% and the right wheel is 17%
		L298N_Init(1,0,1,0); //The left wheel is forward and the right wheel is forward
	}
	
	


}

Related Posts

Robomaster wheel motion calculation

LeetCode 884 Uncommon words in two sentences [String] HERODING’s road to LeetCode

Music player based on STM32, using PWM to control the buzzer

Why are there so many people who want to be programmers even though programmers are young?

About the pitfalls of ARM development (AI)

GD32F4xx serial port transceiver, DMA+idle interrupt

Teach you step by step how to use it – commonly used modules – HC05 Bluetooth module, wireless Bluetooth serial port transparent transmission module, (example: mobile phone Bluetooth controls STM32 microcontroller to light up the LED light)

Using USB to TTL serial port burning program STM32F103C8T6 core board (CH340)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*