Design of serial communication between STM8S microcontroller and smart phone via Bluetooth

Publisher:skyshoucangLatest update time:2020-08-13 Source: elecfansKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial communication has a long history and is the most basic and simplest communication method. Even in the current era of mobile and wireless interconnection, serial communication is indispensable. For example, for wireless transmission of small data volumes, although the physical link uses Bluetooth or WIFI for transmission, serial communication may still be used for data interaction with the microcontroller.


The STM8S series of microcontrollers have 1 to 3 universal asynchronous receivers and transmitters (UARTs) to support serial communication. In this section, we will use the STM8S microcontroller to communicate with a smartphone via Bluetooth to implement an echo function (the microcontroller returns the data received from the mobile phone to the mobile phone as is) to explain how to use the serial port function of the microcontroller.

Design of serial communication between STM8S microcontroller and smart phone via Bluetooth

1. Circuit design

The circuit uses a Bluetooth to serial port module to realize Bluetooth communication. This Bluetooth module has four pins, VCC, GND, TXD, and RXD. Just connect the VCC and GND pins of the module to the power supply, and connect the TXD and RXD pins to the RXD (i.e. PD6) and TXD (i.e. PD5) pins of the microcontroller. Note that TXD and RXD must be cross-connected, that is, the TXD of the Bluetooth module is connected to the RXD of the microcontroller, and the RXD of the Bluetooth module is connected to the TXD of the microcontroller.

The circuit diagram is as follows. M1 is the Bluetooth module. The circuit principle is very simple, so I won’t explain it in detail:

2. Software Design

The Bluetooth module has implemented Bluetooth connection, authentication and other operations, and converted the application data on the Bluetooth protocol into the serial port protocol. In the microcontroller software, it only needs to implement data reading and writing on the serial port.


That is, the function to be realized is to receive serial port data and send the received data out through the serial port.


1) Serial port settings

The STM8S standard library provides three settings related functions: UART1_DeInit(), UART1_Init(), UART1_ITConfig(). The first UART1_DeInit() is to reset UART1, without input parameters. The focus is on the second UART1_DeInit() to set the serial port working status. Let's take a look at the function interface definition first:

void UART1_Init(uint32_t BaudRa te , UART1_WordLength_TypeDef WordLength,

UART1_StopBits_TypeDef StopBits, UART1_Parity_TypeDef Parity,

UART1_SyncMode_TypeDef SyncMode, UART1_Mode_TypeDef Mode)

The function of each parameter is as follows:

BaudRate: Baud rate

WordLength: Byte length, value is UART1_WORDLENGTH_8D or UART1_WORDLENGTH_9D

StopBits: Stop bits, supports 1/0.5/2/1.5 stop bits, and the values ​​are UART1_STOPBITS_1/UART1_STOPBITS_0_5/UART1_STOPBITS_2/UART1_STOPBITS_1_5.

Parity: parity bit, UART1_PARITY_NO/UART1_PARITY_EVEN/UART1_PARITY_ODD

SyncMode: The setting of the microcontroller UART_CK output synchronous clock . If UART_CK output is not required, select UART1_SYNCMODE_CLOCK_DISABLE.

Mode: Serial communication mode, UART1_MODE_RX_ENABLE/UART1_MODE_TX_ENABLE/UART1_MODE_TX_DISABLE/UART1_MODE_RX_DISABLE/UART1_MODE_TXRX_ENABLE

The default baud rate of the Bluetooth module used here and the serial port communication with the microcontroller is 9600, 1 stop bit, no parity bit, and no need for synchronous clock. The echo function to be implemented requires serial port sending and receiving, so the specific parameters here should be:

UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);

The third function UART1_ITConfig() is to set the interrupt procedure for serial communication:

void UART1_ITConfig(UART1_IT_TypeDef UART1_IT, Funct ionalState NewState)

UART1_IT: specifies the interrupt type. There are many types of serial port interrupts, and the main one is UART1_IT_RXNE_OR: the interrupt after receiving data.

NewState: Indicates whether the specified interrupt is enabled or disabled.

Here you only need to set the interrupt to enable receiving data, so the specific parameters here should be:

UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);

Note that after setting the interrupt mode, you also need to enable interrupts using the enableInterrupts() function.


2) Data transmission and reception

Data sending is relatively simple, you can use the following two functions to send it directly.

void UART1_SendData8(uint8_t Data);

void UART1_SendData9(uint16_t Data);

Data reception is a little more complicated, and it is necessary to set up a serial port interrupt processing function. After a serial port reception interrupt occurs, call the following two functions to obtain the data received by the serial port.

uint8_t UART1_ReceiveData8(void);

uint16_t UART1_ReceiveData9(void);

The serial port receive interrupt vector is in the UART1_RX_IRQHandler interrupt, so the interrupt handling code should be inserted at the following position in stm8s_it.c:

INTERRUPT_HANDLER (UART1_RX_IRQHandler, 18)

{

/ * In order to detect unexpected events during development,

it is  recommended to set a breakpoint on the following instruc TI on.

* /

//Insert the received data processing here, for example, use UART1_ReceiveData8() to read the data received by the serial port

}


3) Source code

The entire echo function, the software needs to achieve is a) the main function main ( ) to set the serial port, and then enter the empty loop, b) the serial port receive interrupt processing function to read the serial port data, and repeatedly send it back from the serial port. The complete source code is as follows:

main.c

void main(void)

{

UART1_DeInit();

UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE); //UART1_MODE_TX_ENABLE);

UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);

while (1);

}

stm8s_it.c

INTERRUPT_HANDLER (UART1_RX_IRQHandler, 18)

{

UART1_SendData8(UART1_ReceiveData8());

}


3. Connect to a smartphone for serial port data transmission and reception test

Compile the above program and download it to the STM8S microcontroller. First, pair the Bluetooth module with a smart phone, then use the Bluetooth serial port debugging app (there are many such tool apps, and the one used here is called "Bluetooth Serial Port") to connect to the paired Bluetooth module, and finally send data (send "test string"). You can see that the microcontroller has received the data and sent it back as is.

4. Summary

This article mainly explains the setting of the STM8S microcontroller serial port and the data transmission and reception processing, involving the following knowledge points:

1) Setting the serial port working mode

2) Setting up serial port interrupts and writing interrupt handling functions

3) Serial port data sending and receiving.

Keywords:STM8S Reference address:Design of serial communication between STM8S microcontroller and smart phone via Bluetooth

Previous article:Introduction to the main features of the STM32F030C8T6 microcontroller
Next article:Can stm8 pins be used as touch input

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号