51 MCU and Bluetooth module connection

Publisher:dst2015Latest update time:2019-07-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I started learning to use the Bluetooth module not long ago. There were many problems in the process of connecting the module with the 51 single-chip microcomputer. I think many novices will encounter such problems like me. Therefore, I wrote this article to share the problems encountered in the learning process and the solutions.


The module used in this study is the HC-06 Bluetooth module, as shown below:

This module is available on Taobao for about RMB 20. There are two types of HC-06 on Taobao, one with pins and one without pins. It is recommended that novices buy the one with pins. From the beginning of the experiment to the success, I used a total of four Bluetooth modules. The first one I bought was with pins, but the module itself was broken; the second one I bought was without pins, but due to my limited welding skills, the module was damaged and could not be used; the third one was a Bluetooth 4.0 given by a friend, but it could not be used for some reason. I would like to thank my friend for giving me the Bluetooth; the fourth one I bought was the Bluetooth shown in the picture above, and I finally completed the experiment.


Summary: When buying on Taobao, it is best to compare prices from different stores. Although the modules are not expensive, problems encountered during the purchase process will waste time, affect development, and be very troublesome.


Two microcontrollers are used, one is the development board commonly used by novices and the other is the smallest module of the microcontroller. I will explain the difference between the two later.


Development Board:

The smallest module of the microcontroller:



I specifically marked the crystals of the two, which are 12MHZ and 11.0594MHZ respectively. It was the difference in crystals that caused me problems in my study. The following is the process of the study experiment.


Debugging of Bluetooth module:


Wiring: connect the RX of the Bluetooth module to the TX of the conversion module, and connect the TX of the Bluetooth module to the RX of the conversion module, as shown in the figure below:



Connect to the computer and download the serial port debugging assistant on the PC. The software can be found by searching by itself and will not be described here.


Attached is the driver that may be used: Link: https://pan.baidu.com/s/1bpYLfCr Password: yabv


Entering the debugging assistant, in fact, there is basically no need to adjust parameters. The Bluetooth module is basically set to a baud rate of 9600 by default, so you can directly start software debugging. For specific parameter adjustment methods, you can search other articles on Baidu, which have very detailed introductions.


Start the serial port, and success will be displayed in the lower left corner:



Send AT command and return OK:



This indicates that the serial port is normal. Now use your mobile phone to connect to the Bluetooth module. The mobile phone also uses the debugging assistant, please download it yourself.


Search for Bluetooth module:


Note: I have renamed my Bluetooth module to Ezio. Before the name change, the default name was HC06.



connection succeeded:



Try sending the message hello:



At this time, on the serial port assistant on the PC, you can receive the message sent from the mobile phone:



Here is one thing to explain: after the Bluetooth module is powered on, the LED light on the module is flashing, and it is in slave mode. After successfully connecting to the mobile phone, the LED light will turn on. From then on, the Bluetooth module is successfully debugged and can be connected to the microcontroller for testing.




Bluetooth module and 51 single chip microcomputer wiring:


Just like connecting the conversion module, the RX of the Bluetooth module is connected to the TX of the microcontroller, and the TX of the Bluetooth module is connected to the RX of the microcontroller. Here, the RX and TX pins of the microcontroller are P3.0 and P3.1 respectively, as shown in the figure (picture from the Internet):



After correct wiring, write the program into the microcontroller. The program is as follows:


#include


sbit P1_0 = P1^0; //Test port, optional

sbit P1_3 = P1^3; //output port


unsigned char tempbuf; //Store received information


/*Initialize the serial port*/

void BlueteethInit()

{

SCON = 0x50; //Serial port mode 1, allow receiving

TMOD = 0x20; //T1 working mode is 2, automatic reload

PCON = 0x00; //Baud rate is not doubled


REN = 1;


TH1 = 0xfd; //Set the baud rate to 9600

TL1 = 0xfd;


RI = 0;


EA = 1;

ES = 1;


TR1 = 1;

}


void main()

{

BlueteethInit();

P1_0 = 0;

P1_3 = 0;

TI = 0;

while(1)

{

if(tempbuf == 0x31) // can be used

P1_3 = 1;

if(tempbuf == 0) //Cannot be used

P1_3 = 0;

if(tempbuf == 'A') // can be used

P1_3 = 1;

if(tempbuf == 'B') // can be used

P1_3 = 0;

}

}


void Serial(void) interrupt 4

{

tempbuf = SBUF;

RI = 0; //Read flag clear

SBUF = tempbuf; //Return the content to the mobile phone, and you can view the sent content on the mobile phone

while(!TI);

TI = 0; // write flag clear

}

This program is the simplest test program. It uses Bluetooth to receive information from a mobile phone and controls the P1.3 port to output a high or low level to test whether the information is received correctly.


The first step is to connect the Bluetooth module to the development board, and successfully connect the mobile phone to the Bluetooth module and try to send information. The process is shown in the figure:



Whether sending numbers or other characters, you can see that the returned code is garbled, so you can know that the MCU also receives garbled code, so the judgment in the program is:


while(1)

{

if(tempbuf == 0x31) // can be used

P1_3 = 1;

if(tempbuf == 0) //Cannot be used

P1_3 = 0;

if(tempbuf == 'A') // can be used

P1_3 = 1;

if(tempbuf == 'B') // can be used

P1_3 = 0;

}

It cannot be executed correctly, and the P1.3 port naturally cannot output high or low levels as required.


The second step is to connect the Bluetooth module to the MCU's smallest module and successfully connect the mobile phone to try to send information, as shown in the following figure:



It can be seen that the content returned at this time is the same as the content sent. After testing, the program can also be executed correctly at this time. The change of the output level of the P1.3 port can be checked using a multimeter, indicating that the Bluetooth module can be used normally at this time.


Special Note:


if(tempbuf == 0x31) // can be used

P1_3 = 1;

if(tempbuf == 0) //Cannot be used

P1_3 = 0;

When sending digital messages, they should be in hexadecimal. Therefore, when judging, if 1 is received, it should be judged whether it is equal to 0x31, rather than whether it is equal to 1. After testing here, when 1 is sent, tempbuf == 0x31 is judged, and the judgment is valid; when 0 is sent, tempbuf == 0 is judged, and the judgment is invalid. Just add single quotes to the judgment character.


Step 3, why does using two identical MCUs lead to different results? This is a question that has troubled me for a long time. After checking, I found out that it was the crystal oscillator problem. Here is the explanation of the crystal oscillator by the master, which I don’t understand yet: https://www.zhihu.com/question/30930577


But the conclusion that can be drawn is that if serial communication is used, the crystal oscillator to be used should be 11.0594MHZ, otherwise garbled characters may appear.


Attached: The crystal oscillator on the development board is shown in the figure:



It is replaceable and is also available on Taobao. You can buy the crystal oscillator you need.



Reference address:51 MCU and Bluetooth module connection

Previous article:General 1602, 12232, 12864 LCD operation method
Next article:Detailed explanation of the principle of microcontroller reset circuit

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号