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.
Previous article:General 1602, 12232, 12864 LCD operation method
Next article:Detailed explanation of the principle of microcontroller reset circuit
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Tiva TM4C123GXL MCU software simulation IIC problem
- Classification of power amplifiers
- C2000GPRS connection failure
- An Active Filter Experiment
- Must-see! Illustrated guide to the Internet of Things
- #国芯经验有奖作文# Come and share your days of being addicted to "playing with Guoxin and looking for alternatives"
- The role of the diode at the input point of the comparator LM393
- Please help! The ADC sampling fluctuation in stm32h743 is very large, and the 16-bit resolution exceeds the 8-bit fluctuation
- Apply for ufun learning board, introductory & deep learning simulation, engineering skills
- I downloaded a source code from the Internet. Please help me find out what IDE this project was developed in. Thanks