🎀 Article Author: Ertu Electronics🌸 Follow our public account for more resources!🐸 Looking forward to learning and exchanging ideas together!

1. Project Overview
This project designs a dual-end communication system for mobile phones and computers based on STM32 and a Bluetooth module, with the following main functions:
- • The computer can send messages to the microcontroller via a serial assistant. After receiving the message, the microcontroller performs a CRC8 check, and if the check passes, it forwards the message to the mobile phone;
- • The mobile phone can send messages to the microcontroller via a Bluetooth assistant app. After receiving the message, the microcontroller performs a CRC8 check, and if the check passes, it forwards the message to the computer;
- • The OLED can display the CRC8 check result and the content of the message;
- • The sent message can consist of any ASCII characters;
2. Hardware Selection
- • The main control unit is the STM32F103C8T6 core board;
- • The OLED is a 0.96-inch four-pin IIC screen;
- • The Bluetooth module used is HC-05;
- • USB-TTL module;
3. Program Design
The key parts of the program design actually consist of only two business functions and one CRC8 check calculation function. One business function is to receive messages sent from the computer, perform a CRC8 check, display the check result on the OLED, and if the check passes, forward the message to the mobile phone. The other business function is the corresponding one that forwards messages from the mobile phone to the computer, and the process is the same.
3.1 Function to Receive Messages from the Computer
This function is responsible for receiving messages sent from the computer, performing a CRC8 check, displaying the check result on the OLED, and if the check passes, forwarding the message to the mobile phone. The program design is as follows:
/*
*==============================================================================
*Function Name: App_ReceComputerData_Task
*Function Purpose: Receive messages sent from the computer and forward them to the mobile phone
*Input Parameters: None
*Return Value: None
*Remarks: None
*==============================================================================
*/
void App_ReceComputerData_Task (void)
{
u8 crc = 0;
if (gUsart1EndFlag == 1) // Serial port 1 reception completed
{
// Calculate CRC8 check code
crc = Med_Crc8Calculate(USART1_RECEFIFO,gReceCunt1 - 1);
if (crc == USART1_RECEFIFO[gReceCunt1 - 1]) // CRC check passed
{
Med_Oled_ShowString(10,3,"CRC8 success",8);
USART1_RECEFIFO[gReceCunt1 - 1] = 0x00;
Med_Oled_ShowString(10,7,USART1_RECEFIFO,8);
printf ("%s",USART1_RECEFIFO); // Forward the message to the mobile phone
Usart_ClearFifo(UART1,gReceCunt1);
}
else
{
Med_Oled_ShowString(10,3,"CRC8 failed ",8);
}
gUsart1EndFlag = 0; // Clear reception completed flag
gReceCunt1 = 0; // Reset reception count variable
}
}
3.2 Function to Receive Messages from the Mobile Phone
This function is responsible for receiving messages sent from the mobile phone, performing a CRC8 check, displaying the check result on the OLED, and if the check passes, forwarding the message to the computer. The program design is as follows:
/*
*==============================================================================
*Function Name: App_RecePhoneData_Task
*Function Purpose: Receive messages sent from the mobile phone and forward them to the computer
*Input Parameters: None
*Return Value: None
*Remarks: None
*==============================================================================
*/
void App_RecePhoneData_Task (void)
{
u8 crc = 0;
if (gUsart2EndFlag == 1) // Serial port 2 reception completed
{
// Calculate CRC8 check code
crc = Med_Crc8Calculate(USART2_RECEFIFO,gReceCunt2 - 1);
if (crc == USART2_RECEFIFO[gReceCunt2 - 1]) // CRC check passed
{
Med_Oled_ShowString(10,3,"CRC8 success",8);
USART2_RECEFIFO[gReceCunt2 - 1] = 0x00;
Med_Oled_ShowString(10,7,USART2_RECEFIFO,8);
USART1_Send (USART2_RECEFIFO,gReceCunt2); // Forward the message to the computer
Usart_ClearFifo(UART2,gReceCunt2);
}
else
{
Med_Oled_ShowString(10,3,"CRC8 failed ",8);
}
gUsart2EndFlag = 0; // Clear reception completed flag
gReceCunt2 = 0; // Reset reception count variable
}
}
3.3 CRC8 Check Code Calculation Function
/*
*==============================================================================
*Function Name: Med_Crc8Calculate
*Function Purpose: Calculate CRC8 check code
*Input Parameters: *data: Pointer to the data to be sent; length: Length of the data in bytes
*Return Value: CRC8 check code
*Remarks: None
*==============================================================================
*/
u8 Med_Crc8Calculate (u8 *data, u16 length)
{
u8 crc = 0x00; // Initialize CRC value
u16 i = 0,j = 0;
for (i = 0; i < length; i++)
{
crc ^= data[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x80)
{
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
}
else
{
crc <<= 1;
}
}
}
return crc;
}
4. Usage Instructions
Since we need to perform a CRC8 check, it is necessary to include the check bit when sending messages from either the computer or the mobile phone. This design does not have a dedicated upper computer to calculate the CRC8 check code and automatically add the check code to the sent message, so this step needs to be done manually.
4.1 CRC8 Check Code Calculation
We can search for an online CRC8 check code calculator. An example calculation is shown below:

Using the same polynomial as shown in the image above, input the information to be sent, and click calculate to obtain the corresponding CRC8 check code.
4.2 String and Hexadecimal Conversion
When sending messages from either the mobile phone or the computer, it is necessary to include the check bit, and sometimes the calculated check bit may not be included in the ASCII table. Therefore, it must be sent in hexadecimal format. Before sending, we need to convert the content to be sent into HEX format, and then send it out in HEX format along with the check bit.
4.3 Bluetooth Module Configuration
Before use, the Bluetooth module’s name, pairing password, and baud rate need to be configured. The baud rate used in this design is 115200. The configuration method can be found in the article introducing the HC-05 peripheral series.

About Us
WeChat Public Account | QQ | Learning Group

Scan to FollowDiscover more unique content
Share
Collect
View
Like