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.
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.
Previous article:Introduction to the main features of the STM32F030C8T6 microcontroller
Next article:Can stm8 pins be used as touch input
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- FPGA-based cross-clock domain signal processing——MCU
- [Shanghai Hangxin ACM32F070 development board] The second part lights up the LCD screen and operates the touch button functions, serial port, ADC.
- EEWORLD University ---- TE Smart City
- Develop IoT in 10 minutes - Smoke sensor monitoring (Wifi version)
- Analysis of PDM technology and its application in automobile manufacturing industry 1
- The ultimate competition between PPM, PPL and PPS of online TV
- Hearing aid chips
- Installation of TI C2000 Embedded Code Support Package in Matlab
- IMX6 MfgTool Guide
- EEWORLD University Hall----Automatic Control System (Henan Polytechnic University)