Basic usage method of HC-06 Slave Module
I. How to connect Bluetooth and Single Chip Microcomputer
, Gnd and VCC are grounded and powered
Rxd and txd are connected to the MCU rxd and txd respectively.
Rxd: receive data receive (RX) data
Txd: send data transmit (TX) data
Where are the single chip microcomputer rxd and txd? Open the schematic diagram of the Microcontroller
P3.0 and p3.1 are the rxd and txd interfaces. Connect the Bluetooth interfaces with them.
At this time, the indicator on the Bluetooth is flashing (as long as it is powered on, it will flash)
1. Use a serial assistant to debug Bluetooth (AT command)
Open the Device Manager and check that the serial port number connecting the microcontroller to the computer is com6.
Open the serial assistant, find the serial port number com6, and open the serial port (also pay attention to the baud rate. The default value is 9600. If there are changes, you can only try them one by one ~)
Enter "at", click "send", and receive "OK"
I. Common AT commands
1. Test Communication
Send:
Return: OK
2. Change the Bluetooth serial communication baud rate (after this change, you must adjust the baud rate in the serial assistant immediately !)
Send: At + baud1
Return Value: ok1200
Send: At + baud2
Return Value: ok2400
1----1200
2----2400
3----4800
4----9600
5----19200
...... (The number is + 1, the baud rate is * 2)
8----115200
......
C----1382400
Let's use the default 9600. This is also related to the Code sent and received by the serial port, so we will not adjust it.
3. Change the Bluetooth name (the name can be changed to the name assigned to the bluetooth device)
Send: At + namename
Return Value: okname
4. Change the Bluetooth pairing Password
Send: At + pinxxxx
Return: oksetpin
2. Bluetooth and computers do not use code pairing
Open Control Panel-> View devices and printers-> right-click to add Devices
Select and click Next (I changed the Bluetooth name. The default value is linvor, but the name of the purchased Bluetooth is different. I believe you can find it ......)
After a while, you will jump out of the following interface and select "Enter the pairing code of the device ".
Enter the matching code you just set. I set it to 1234
After a moment, the matching is successful. Right-click the device to view its properties.
My serial port number is com12. Everyone may be different. Remember this serial port number!
I. Download the communication code
Install the RS232 serial communication code written in the previous article into the single-chip microcomputer. The function implemented by the code is to send 'A' to return 'B'; otherwise, 'C' is returned '. I wrote this part of the code very easily and can adjust it as needed.
Note: During installation, you must unplug the Bluetooth, because the Bluetooth connection will interfere with the data line communication!
In my code, the baud rate is 9600, and the crystal oscillator is 11.0592mhz. If you have other features, adjust the code!
2. Test communication using Bluetooth
1. Connect the bluetooth device to the microcontroller again.
Pay special attention to this time when the Bluetooth rxd is connected to the txd of the microcontroller, And the Bluetooth txd is connected to the rxd of the microcontroller! This is because when we used the AT command, we used the data line on the single chip microcomputer to communicate with the computer, and what data the single chip computer received was what data Bluetooth received, therefore, the connection ports of Bluetooth and single chip microcomputer are the same. When we use Bluetooth for communication, after Bluetooth receives the data, it sends the received data to the MCU, that is, the sender of the Bluetooth is connected to the receiver of the MCU, and the receiver of the Bluetooth is connected to the sender of the MCU. (I hope I can understand it in this way ......)
2. Open the serial assistant, select the serial port number com12 (the serial port number of the previous Bluetooth), and open the serial port
At this time, the indicator on the Bluetooth is always on and will not flash, indicating that the Bluetooth is successfully connected to the computer ~
3. Send 'A'. 'B' is returned successfully '!
"51 Single Chip Microcomputer" basic usage of Bluetooth slave