[Super detailed] C51 MCU and HC-05 Bluetooth module realize mobile phone lighting

This article records the learning process of the HC05 Bluetooth module on the C51 microcontroller


1. Hardware connection

HC05 has a total of 6 pins, this experiment generally only uses the middle four, namely RXD, TXD, GND, VCC
insert image description here
HC05 has two modes, one is normal mode, the other is configuration mode,一定注意两种模式的接线方法是不同的

Regardless of the mode, the wiring method of VCC and GND is the same, that is, VCC is connected to the 5V port of the microcontroller (the power supply voltage is 3.6V~6V, and it is not lit when connected to 3.3V), and GND is connected to the GND of the microcontroller, as follows Show
insert image description here


(1) Wiring in configuration mode

insert image description here
In the configuration mode, the MCU is connected to the computer through the serial port, and the computer can complete the parameter configuration of the HC05 module through the AT command. At this point the wiring is:

In the configuration mode, the RXD of HC05 is connected to the RXD of the microcontroller, and the TXD is connected to TXD.

Since the single-chip microcomputer model used by the blogger is STC89C52RC, the RXD of the single-chip microcomputer is p30 port, and the TXD is p31 port.

insert image description here

Then connect as shown in the figure below
insert image description here


(2) Wiring in normal mode

insert image description here

In normal mode, the RXD of HC05 is connected to the TXD of the microcontroller, and TXD is connected to RXD.


Two, configuration mode operation

After wiring in configuration mode, connect the MCU to the computer through the serial port. If the MCU is powered on at this time, and the connection is correct, the light on the HC05 module will flash rapidly, about twice in 1 second, and the configuration mode has not yet been entered.
Power off the MCU, keep pressing the small button on the HC05 module, as shown in the figure below:
insert image description here
power on the MCU while pressing the button, release the button after power on, the HC05 will flash quickly at first, and enter the slow mode after 1s. Flashing state, flashing about 2s once, at this time successfully enter the configuration mode.
Open the serial port assistant, the subject here uses sscom5.13.1. After opening, configure the serial port assistant, as shown in the figure below,
(1) First, pay attention to whether the serial port number is correct
(2) Then be sure to set 波特率设置为38400(this baud rate is the baud rate of the configuration mode, regardless of whether the baud rate is changed to How much is the adjusted baud rate of normal mode, the baud rate of configuration mode is 38400 is unchanged)
(3) Check the option of adding carriage return and line feed (if the serial port assistant used does not have this option, manually add it after the code plus \r\n, e.g. AT\r\n)
insert image description here

(4) Open the serial port, enter AT in the input box, and click send. If everything is OK, you will receive OK, as shown in the figure below.
If you do not receive it, please check the wiring, or if you have won a prize, there is a problem with the MCU/module .
insert image description here

Next, you can use AT commands to configure the module, some AT commands are as follows

AT+ROLE=0 (蓝牙模块设为从机,此时只可以被搜索)
AT+CMODE=1 (蓝牙模块可以和任意设备连接)
AT+UART=9600,0,0 (设置波特率9600)
AT+NAME=HC05 (设置蓝牙的名字为HC05,手机蓝牙搜索时就找这个名字)
AT+PSWD=1234 (设置密码,手机蓝牙串口APP连接蓝牙模块,需要输入密码)
以上,蓝牙串口通通返回OK 。

For example to change the baud rate to 9600:

insert image description here


3. Normal mode

Connect the wiring according to the normal mode, that is, RXD to TXD, TXD to RXD. Power on the MCU, HC05 will flash quickly. Download a mobile phone Bluetooth debugging app (the subject uses Bluetooth spp pro, and most other searched in the application market can also be used), search for your own device and click to connect, enter the password (usually the default is 1234, you can use it in the configuration
insert image description here
mode Next, modify your own connection password through AT commands), and then the connection is successful.


4. Use bluetooth to light up through the bluetooth debugging app

1. MCU code burning and Bluetooth connection

The MCU code is as follows:
UART_init () configuration file

#include <REGX52.H>

void UART_init()		//[email protected]
{
    
    
	PCON |= 0x80;		//使能波特率倍速位SMOD
	SCON = 0x50;		//8位数据,可变波特率  
	TMOD &= 0x0F;		//清除定时器1模式位
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式
	TL1 = 0xFA;		//设定定时初值
	TH1 = 0xFA;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
	ES=1;
	EA=1;
}

.h file

#ifndef __UART_H__
#define __UART_H__
void UART_init();
#endif

main.c file

#include <REGX52.H>

void main()
{
    
    
	UART_init();
	P2=0x00;
	while(1)
	{
    
    
	
	}
}

	
void UART_Routine() interrupt 4
	{
    
    
		if (RI==1)
		{
    
    
			P1=SBUF;//将SBUF值赋值给P1
			
			if (P1==0x01)
			{
    
    
				P2=~0x80;
				RI=0;
			}
			else if (P1==0x02)
			{
    
    
				P2=~0x40;
				RI=0;
			}
		}
	}


After burning the code into the MCU, power on the MCU. At this time, the HC05 flashes quickly. Open the mobile app to connect to the Bluetooth. After the connection is successful, the HC05 enters into a slow flash (about 5s and two quick flashes).

insert image description here


2. Bluetooth debugging app configuration

After the connection is successful, select the keyboard mode, select configure keyboard value
insert image description here
to configure the keyboard value of the two keys
insert image description here
insert image description here

!!! 别忘记选择IO模式,选择为hex模式, if the wrong mode is selected, the experiment will have no response
(if the ascii mode is selected, the code needs to be changed, for example, the keyboard sends 01 to the microcontroller, the value received by the microcontroller in hex mode is 0x01, and the value received in ascii mode is 0x0031, please refer to the acii code table for the specific conversion method)
insert image description here
After the configuration is complete, click to save the keyboard configuration.
Press the 1 key, the single-chip led D8 will flicker; press the 2 key, the single-chip led D7 will flicker, and the experiment is successful.

Guess you like

Origin blog.csdn.net/shanhehaoda_/article/details/128740661