The problems encountered by the HC-05 Bluetooth module and the solutions and the realization of communication with mobile phones

1. Debugging of the Bluetooth module-to solve the problem that the AT command cannot be set

Connection between USB to TTL module and HC-05 Bluetooth module:

The two modules share the ground, and the two modules share VCC (VCC is 5V); the RX of the Bluetooth module is connected to the TX of the conversion module, and the TX of the Bluetooth module is connected to the RX of the conversion module. As shown in the figure below: At
Insert picture description here
this time, connect the conversion module to the computer, and then use the serial port debugging assistant to debug the Bluetooth module.

1) Question 1: No serial port is found

First of all, we need to try to set the AT command, but when connecting to the serial port, you may encounter the following problem: the serial port is not searched, but after ensuring the correct connection, it has been powered on correctly, but there is no serial port searched. The reason may be that the driver is not installed
Insert picture description here

Method 1: Download the driver online

Find a driver on the Internet, then install it and try again
Insert picture description here
Insert picture description here
Insert picture description here

Method 2: Update the driver yourself

Insert picture description here
Insert picture description here
Need to restart after driver installation is complete

  • Compared

Before
Insert picture description here
installing the driver After installing the driver
Insert picture description here

2) Question 2: Failed to open the serial port-PL2303HXA has been discontinued since 2012, please contact the supplier

After successfully installing the driver, another problem occurred. You can see that the serial port can be identified, but it fails to open and cannot be used normally. It
Insert picture description here
still fails. Now we have to solve the problem of discontinuation — "PL2303HXA has been discontinued since 2012, please Contact supplier"

Solution

Reference link The
solution is as follows:
Link: https://pan.baidu.com/s/1jw1nsLGd23CagQ_dbkqu0w
Extraction code: vc0c
Download the driver here and unzip it,
Insert picture description here
then install the driver,
Insert picture description here
Insert picture description here
Insert picture description here
then right-click to update the driver
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
and click Next to
Insert picture description here
Insert picture description here
complete

Serial test

Open the serial port to test it, it
Insert picture description here
can be paired normally, and then continue to debug the Bluetooth module
Insert picture description here

3) Debugging of Bluetooth module

HC-05 Bluetooth serial communication module has two working modes: command response working mode and automatic connection working mode. In the automatic connection working mode, the module can be divided into three working roles: Master, Slave and Loopback.

  • When the module is in the automatic connection mode, it will automatically connect according to the pre-set data transmission;
  • When the module is in the command response mode, AT commands can be executed. The user can send various AT commands to the module to set control parameters or issue control commands for the module.
    How to enter the command response mode?

There are two ways to enter the command response mode:

  • When the module is powered on, it is in AT mode when it is not paired, and the baud rate is the baud rate of the module itself, default: 9600, PIO11 needs to be set high once when sending an AT command;
  • After PIO11 is set to high level, power on the module again. At this time, the module enters the AT mode, the baud rate is fixed at 38400, and AT commands can be sent directly.
    What is PIO11 high once?

There is a small button in the Bluetooth module, press once to set PIO11 high once. That is to say, the first method needs to be pressed every time the AT command is sent; and the second method is to power on during the long press, and then there is no need to manage it anymore, just send the AT command directly.

It should be noted that the two methods of entering the command response working mode use different baud rates. It is recommended to use the second method.

How to differentiate into the command response mode?

There is a light on the Bluetooth module. When the light flashes quickly, it is in the automatic connection mode; when the light flashes slowly, it is the command response working mode. When the light flashes slowly twice, it is the normal connection mode with the mobile phone.
Insert picture description here

4) AT command

After entering the command response mode, you can use the serial port debugging assistant to perform Bluetooth debugging.

First of all, AT commands are not case sensitive, and all end with carriage return and line feed. The following describes the commonly used AT commands:
Insert picture description here
For AT commands, there are a few points to note:

  • AT+NAME?: Get the device name. There is a high possibility that this AT command will not be returned, because I have seen many examples..., but other commands are no problem, just set the device name directly;
  • AT+UART?: Get serial port parameters. There are three serial port parameters, baud rate, stop bit, and check bit. Its value is as follows:

Insert picture description here
The default value is: 9600, 0, 0.

Set Bluetooth parameters
Insert picture description here
Check the setting result, the setting is successful, the
Insert picture description here
specific AT command set scientific research check the data manual, you can private message or comment if you need it

2. The transmission between the mobile phone and the Bluetooth module-solve the problem of not being able to connect

Directly connect the Bluetooth module with the conversion module, and then connect it to the computer, the Bluetooth module directly enters the automatic connection working mode.

At this time, the phone opens the Bluetooth serial port debugging application and uses it to connect to the Bluetooth module.

Under normal circumstances, first pair the mobile phone, and then directly enter the string in the Bluetooth serial port debugging application, and it can be successfully received on the computer's serial port.

But when I was testing, I could pair this time, but I couldn’t connect normally.
Insert picture description here
Insert picture description here

1) Scenario 1: The problem of mobile phones

Bluetooth can be paired, but cannot be connected

I tried to connect a mobile phone with a computer and other mobile phones, and found that none of them were possible,
but using another Huawei mobile phone can successfully pair with the computer. (The pairing of the Xiaomi phone failed, but the Huawei phone was successfully paired) It
Insert picture description here
can be seen that it is the Bluetooth pairing problem of my Xiaomi phone.

Then I used the Huawei mobile phone for testing, and found that Huawei's mobile phone could not be paired with the Bluetooth module. I came to a conclusion that it has nothing to do with the mobile phone. It should be that the Bluetooth module needs some other settings to be searched.

2) Assumption 2: The configuration problem of the Bluetooth module

After checking the information, I found that the Bluetooth module needs to be set as a slave to be searched. Then I tried a few times. First, I checked the AT command set (provided by Punctual Atom) and
Insert picture description here
wrote the AT command to set it. OK indicates that the setting is successful.
Insert picture description here
The query is indeed set to slave mode,
Insert picture description here
but the result still fails.
Insert picture description here
This shows that the bluetooth mode I set is not wrong 1, it should be other problems

3) Assumption 3: Wiring problem

For this problem, I think it is impossible, because I can configure AT commands normally

4) Assumption 4: The problem of serial debugging assistant

Constantly replace the Bluetooth debugging assistant, the name is called the Bluetooth serial port, and then it can be successfully connected.
Insert picture description here
You can send information between the mobile phone and the serial port.
Insert picture description here
Finally, if it is successfully solved, the problem lies in the serial debugging assistant. The conclusion is that, If you can pair normally, but you can't connect normally, then change your mobile phone or serial debugging assistant, because Bluetooth compatibility issues make pairing more troublesome.

If you need the apk of the serial debugging assistant of the mobile phone, you can private message or comment

3. STM8 MCU controlled by mobile phone Bluetooth

The previous two examples are relatively simple, this example will involve the content of the program.

Realization function: The mobile phone sends a message to the STM8 MCU via Bluetooth, and the STM8 returns the message to the mobile phone unchanged after receiving the message. Of course, if you have mastered this example, you can also modify it to be that the mobile phone sends a specific message, and then the STM8 microcontroller makes the corresponding action. For example: lighting LEDs, etc., starting motors, etc.
Insert picture description here
The principle is: the mobile phone is transmitted to the HC-05 via Bluetooth, and then communicates with the STM8 through the serial port; and before, it usually communicates with the STM8 through the serial port on the computer through the USB cable to the serial port. There is essentially no difference.
At this time, you should have a more profound understanding of the sentence at the beginning of this article: To put it bluntly, it is just a Bluetooth-to-serial device, as long as you know how to program the serial port, you can achieve the so-called transparent transmission. Everything related to Bluetooth is encapsulated and there is no need to touch it.

Knowing that it is just a simple serial port communication, then the code is easy to implement

The control interface of the mobile app is designed as follows:
Insert picture description here

Reference Code

/****************************************************************/
//接收单字符函数UART1_ReceiveByte(),无形参data,有返回值,返回接收到的数据
//采用查询的方法实现
/****************************************************************/
u8 UART1_ReceiveByte(void)
{
 // printf("进入UART1_ReceiveByte函数...\r\n");
  
  u8 UART1_RX_BUF;                //定义接收数据暂存变量
  while (!(UART1_SR & 0x20));    //等待接收标志
  UART1_SR&=0xDF;                //清零接收标志位RXNE
  UART1_RX_BUF=UART1_DR;         //取回接收到的数据
 // printf("取回接收到的数据\r\n");
  
  return  UART1_RX_BUF;         //返回暂存变量中的数据给调用者
}


/****************************************************************/
//初始化函数UART1_Init(),无形参和返回值
/****************************************************************/
void UART1_Init(void)
{
  //1.设定通信数据位数,此处设定为8位数据位,无校验位
  UART1_CR1 = 0x00;
  //**************************************************
  //展开UART1_CR1赋值二进制数值为:0000 0000 
  //含义:R8=0;    接收数据位不存在第9位
  //      T8=0;    发送数据位不存在第9位
  //      UARTD=0; 使能UART功能
  //      M=0;     一个起始位,8个数据位,n个停止位
  //                n取决于UART1_CR3中的STOP[1:0]位
  //      WAKE=0;   UART被空闲总线唤醒
  //      PCEN=0: (UART模式)奇偶校验控制被禁止
  //      PS=0;    偶校验(校验功能未启用)
  //      PIEN=0;  校验中断被禁止
  //*************************************************
  //2.设定通信停止位位数,此处设定为1位停止位
  UART1_CR3 = 0x00;
  //**************************************************
  //展开UART1_CR3赋值二进制数值为:0000 0000 
  //含义:保留位=0;必须保持清零
  //      LINEN=0;LIN模式被禁止
  //      STOP=00;配置为“00”,1个停止位
  //      CLKEN、CPOL、CPHA、LBCL这几位在UART3上不存在
  //*************************************************
  //3.配置通信波特率参数,此处配置为9600bps(16MHz频率下)
  UART1_BRR2 = 0x03;
  UART1_BRR1 = 0x68;
  //4.使能发送和接收功能
//  UART1_CR2 = 0x0C;
  UART1_CR2 = 0x2C;
  //**************************************************
  //展开UART1_CR2赋值二进制数值为:0010 1100 
  //含义:TIEN=0;发送中断被禁止
  //     TCIEN=0;发送中断完成被禁止
  //      RIEN=1;接收中断开启
  //      ILIEN=0;IDLE中断被禁止
  //      TEN=1;发送功能使能
  //      REN=1;接收功能使能
  //      RWU=0;(UART模式)正常工作模式
  //      PIEN=0;未发送断开字符
  //*************************************************
}


/****************************************************************/
//发送字符重定向函数putchar(),有形参ch,有返回值
/****************************************************************/
int putchar(int ch) 
{  
  UART3_SendByte((u8)ch);
 // UART1_SendByte((u8)ch);//将Printf内容发往串口
  return (ch); 
}

//接收中断服务函数
//**************************中断函数区域**************************
#pragma vector =  UART1_R_RXNE_vector         //0x14
__interrupt void UART1_RX_IRQHandler(void)
{ 
  
  if(UART1_SR&0x20){     //若UART1确实接收到了数据
     UART1_SR&=0xDF;       //清零接收标志位RXNE
  }
  cmd=UART1_DR;         //取回串口数据
  
 //  printf("进入中断服务函数...\r\n");
//   cmd = UART1_ReceiveString(1);
  
      switch(cmd)           //判断串口接收命令值
      {
        case 'a':
            Motor_TurnLeft();                   //左转
            break;
        case 'd':
            Motor_TurnRight();                  //右转
            break;
        case 'w':
            Motor_GoForward();                  //直走
            break;
        case 's':                               
            Motor_GetBack();                    //后退
            break;
        case 'p':                       
            Motor_BeParking();                  //停车
            break;
        case 'n':    //功能二:实现跟随模式              
            //printf("模式二:实现跟随模式   \r\n");
            LED1 = 1;  //熄灯
            Motor_BeParking();
            InfraredFollow_Module();
            break;
        case 'b':    //功能三:实现避障模式
            //printf("模式三:实现避障模式   \r\n");
            LED1 = 1;  //熄灯
            Motor_BeParking();
            InfraredAvoidance_Module();
            break
        default:
            printf("【ERROR】,没有此功能\r\n");
            break;
      }

}

For inquiries about interrupt vector numbers, please check here: IAR interrupt vector table and interrupt service function writing

Possible problems

After setting some buttons through the mobile phone, you may see that the interrupt function will be called twice on the serial debugging assistant of the computer, and the execution will be repeated.
Insert picture description here
However, I changed a serial debugging assistant and there is no problem...It's really a headache
Insert picture description here

Reference link: [Common modules] HC-05 Bluetooth serial communication module use detailed explanation (example: mobile phone Bluetooth control STM32 single-chip microcomputer)

Guess you like

Origin blog.csdn.net/weixin_44751294/article/details/111588963