Project small combat: design of mobile phone Bluetooth remote control car controlled by 51 single-chip computer (physical picture of program component list)

In the past few days, I have time to make a Bluetooth remote control car controlled by a mobile phone, and finally realized it today. Share the process and experience with you.
Without further ado, go to the physical map
Insert picture description here
Insert picture description here
Insert picture description here
first. The project component list
1. A treasure buys a smart car chassis (my car is a four-wheel drive, so there are four motors, two acrylic plates and some accessory screws)
2. L298n module Two (one 298 controls two motors)
3. HC-06 Bluetooth module
4, battery box and 18650 battery 3
5, minimum system of single chip computer
6, Dupont line at least 12, of course, the more the better
7, several wires For connecting the motor, and the battery box is connected to the L298N and the microcontroller)

Second, the production process
First of all, let's first understand the wiring of L298N, I was also forced for a long time, anyway, it will be groping, general stores have information. Then use the Bluetooth module. After getting the Bluetooth module, if there is a development board (if not, use the microcontroller's burner), first connect TXD to TXD, RXD to RXD, open the computer's serial assistant to communicate with Bluetooth, open AT , Send AT command set, such as setting the baud rate, changing the name, changing the pairing password. But when connecting to the microcontroller via Bluetooth, it is TXD connected to RXD. The last one is that I often hit the wall when I was wiring. For example, if you think that the connected cable is actually not connected, it doesn't work at all. You still have to take the multimeter B to know.
Because I forgot to buy the battery box, I connected it with 12V DC, which caused the remote control car to drag the wire.
Third, the physical picture
(towing the car powered by the wire and the charging treasure, but I did not have power)

The information includes some information of L298N, as well as Bluetooth manual (just look at the AT instruction set) and source program.

/ Mobile phone Bluetooth remote control car APP can be found in the application market Bluetooth serial port assistant
Zuozhuanyouzhuan about 90 degrees is my four-wheel drive, if it is only a l298N then put the belt deleted 2
pwm has ten speed
/

#include <reg52.h>

#define Left_moto2_pwm P0_4 // Connect to drive module ENA enable terminal, input PWM signal to adjust speed left rear wheel
#define Right_moto2_pwm P0_5 // Connect to drive module ENB right rear wheel
#define Left_moto_pwm P1_4 // Connect to drive module ENA enable terminal, input PWM signal adjustment speed left front wheel
#define Right_moto_pwm P1_5 // Connect to drive module ENB right front wheel
#define uchar unsigned char
#define uint unsigned int

sbit P0_4 = P0 ^ 4; // define P0_4
sbit P0_5 = P0 ^ 5; // define P0_5
sbit P1_4 = P1 ^ 4; // define P1_4
sbit P1_5 = P1 ^ 5; // define P1_5

/ Motor drive IO definition /
sbit IN1 = P1 ^ 2; // is 1 left motor reverse rotation front wheel
sbit IN2 = P1 ^ 3; // is 1 left motor forward rotation front wheel
sbit IN3 = P1 ^ 6; // is 1 Right motor rotates front wheel
sbit IN4 = P1 ^ 7; // is 1 Right motor rotates front wheel
/ * sbit EN1 = P1 ^ 4; // is 1 Left motor enables
sbit EN2 = P1 ^ 5; // Enable for 1 right motor * /

sbit IN5 = P0 ^ 2; // is 1 left motor reverse rotation
sbit IN6 = P0 ^ 3; // is 1 left motor forward rotation
sbit IN7 = P0 ^ 6; // is 1 right motor forward rotation wheel
sbit IN8 = P0 ^ 7; // 1 is a right rear wheel motor reversal
/ * sbit EN3 = P0 ^ 4 ; // enable the motor to a left rear wheel
sbit EN4 = P0 ^ 5; // 1 is a right motor Enable rear wheel * /

bit Right_moto_stop=1;
bit Left_moto_stop =1;
unsigned int time=0;
int pwm=1;

#define left_motor_en EN1 = 1 // Left motor enable
#define left_motor_stops EN1 = 0 // Left motor stop
#define right_motor_en EN2 = 1 // Right motor enable
#define right_motor_stops EN2 = 0 // Right motor stop

#define left_motor2_en EN3 = 1 // After the left motor is enabled
#define left_motor2_stops EN3 = 0 // After the left motor is stopped
#define right_motor2_en EN4 = 1 // After the right motor is enabled
#define right_motor2_stops EN4 = 0 // After the right motor is stopped

#define left_motor_go IN1 = 0, IN2 = 1 // left motor forward
#define left_motor_back IN1 = 1, IN2 = 0 // left motor reverse
#define right_motor_go IN3 = 1, IN4 = 0 // right motor forward
#define right_motor_back IN3 = 0, IN4 = 1 // Right motor reverse

#define left_motor2_go IN5 = 0, IN6 = 1 // left motor forward
#define left_motor2_back IN5 = 1, IN6 = 0 // left motor reverse
#define right_motor2_go IN7 = 1, IN8 = 0 // right motor forward
#define right_motor2_back IN7 = 0, IN8 = 1 // Right motor reverse

unsigned char pwm_val_left = 0; // Variable definition
unsigned char push_val_left = 0; // Left motor duty cycle N / 10
unsigned char pwm_val_right = 0;
unsigned char push_val_right = 0; // Right motor duty cycle N / 10

void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x–)
for(y = 114; y > 0 ; y–);
}

// Bluetooth initialization
void UART_INIT ()
{
SM0 = 0;
SM1 = 1; // Serial port working mode 1
REN = 1; // Allow serial port to receive
EA = 1; // Open the total interrupt
ES = 1; // Open the serial port interrupt
TMOD = 0x20; /// 8-bit auto reload mode
TH1 = 0xfd;
TL1 = 0xfd; // 9600 baud rate
TR1 = 1; // start timer 1
}

/ ************************************************* *********************** /
void run (void) // pwm speed regulation function
{
push_val_left = pwm; // PWM regulation parameter 1-10 1 is The slowest, 10 is the fastest. Changing this value can change its speed
push_val_right = pwm; // PWM adjustment parameters 1-10 1 is the slowest, 10 is the fastest. Changing this value can change its speed
if (pwm10) pwm=0;
if(pwm
0&&pwm<0) pwm=0;

}

/ ************************************************* ************************************************************ /
/
the PWM modulated motor speed /
/
********************************************************* ************************************************** * /

/ * Left side motor speed regulation * /
/ * Adjust the value of push_val_left to change the motor speed, duty cycle * /
void pwm_out_left_moto (void)
{
if (Left_moto_stop)
{
if (pwm_val_left <= push_val_left)
{Left_moto_pwm = 1;
Left_moto2_pwm = 1 ;}
else
{Left_moto_pwm = 0; Left_moto2_pwm = 0;}

if(pwm_val_left>=10)
pwm_val_left=0;
}
else { Left_moto_pwm=0;Left_moto2_pwm=0; }
}
/*****************************************************************/
/
右侧电机调速 /
void pwm_out_right_moto(void)
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
{ Right_moto_pwm=1;
Right_moto2_pwm=1; }
else
{Right_moto_pwm=0;
Right_moto2_pwm=0;}
if(pwm_val_right>=10)
pwm_val_right=0;
}
else {Right_moto_pwm=0;Right_moto2_pwm=0; }
}
/
**************************************************/
///TIMER0 interrupt service subfunction generates PWM signal /
void timer0 () interrupt 1 using 2
{
TH0 = 0XF8; // 1Ms timing
TL0 = 0X30 ;
time ++;
pwm_val_left ++;
pwm_val_right ++;
pwm_out_left_moto ();
pwm_out_right_moto ();
}

// car forward
void forward ()
{
ET0 = 1;
run (); // pwm program
left_motor_go; // left motor forward
right_motor_go; // right motor forward
left_motor2_go; // left motor forward rear wheel
right_motor2_go; // right motor Forward rear wheel
}

void left_go () // Turn left
{
ET0 = 1;
run ();
left_motor_back;
right_motor_go;
left_motor2_back;
right_motor2_go;
delay (700);
forward ();
}
// right turn
void right_go ()
{
ET0 = 1;
run ( );
delay (50);
right_motor_back;
left_motor_go;
right_motor2_back;
left_motor2_go;
delay (700);
forward ();
}
// car left turn circle
void left ()
{
ET0 = 1;
run ();
delay (50);
right_motor_go ; // right motor forward
left_motor_back; // left motor back
right_motor2_go; // right motor forward
left_motor2_back; // left motor back
}

// Car right turn
void right ()
{
ET0 = 1;
run ();
left_motor_go;
right_motor_back;
left_motor2_go;
right_motor2_back;
}

//小车后退
void back()
{
ET0 = 1;
run();
left_motor_back;
right_motor_back;
left_motor2_back;
right_motor2_back;
}

// car stop
void stop ()
{
ET0 = 0;
P1 = 0;
P0 = 0;
}

// Serial port interrupt
void UART_SER () interrupt 4
{
if (RI)
{
RI = 0; // Clear receive flag
switch (SBUF)
{
case 'g': forward (); break; // forward
case 'b': back (); break; // backward
case 'l': left (); break; // left turn circle
case 'r': right (); break; // right turn circle
case 's': stop (); break ; // Stop
case 'z': left_go (); break; // Left turn driving
case 'y': right_go (); break; // Right turn driving
case 'p': pwm ++; break; // Accelerate
case ' c ': pwm–; break; // Slow down
}

    }

}

void main ()
{
TMOD =
0X01 ; TH0 = 0XF8; // 1ms timing
TL0 = 0X30;
TR0 = 1;
ET0 = 1;
EA = 1;
UART_INIT (); // Serial port initialization
while (1);
}

Finally, if you have any comments or suggestions, you are welcome to leave a message to me, let us learn together and progress together.
If you need the complete code or design file, please leave a message or private message me below.

Thank you!

Published 97 original articles · 200 praises · 80,000+ views

Guess you like

Origin blog.csdn.net/weixin_44212493/article/details/104335444