Let me say a few words first. I have never summarized the serial port application of STM8. I will summarize it recently. I developed it with the STM8 application library. My understanding of the basic serial port application is still inadequate. I thought that the serial port of STM8 would also have several methods such as query sending, interrupt sending, query receiving, and interrupt receiving~! But in fact, I still don’t understand the interrupt sending mode very well, and it is still different from the serial port work of 51 microcontroller.
STM8 provides sending interrupt and sending completion interrupt. My own understanding is that it enters interrupt when sending and interrupts when sending a byte. This is different from the serial port interrupt I understand in my brain. I will continue to figure it out later.
Generally speaking, the serial port of STM8 is mostly sent by query and received by interrupt. Before using the serial port, you should pay attention to the following points:
1. Configure the GPIO pin status of TX and RX
2. Initialize the serial port. Here you need to turn on the clock of the serial port device, configure the serial port working mode, and turn off the USART_IT_TXE and USART_IT_TC interrupts, and turn on the USART_IT_RXNE interrupt. Then you can use the serial port normally.
3. To send a byte, call the library function USART_SendData8(), but remember to query the flag bit after calling it. Write USART_SendByte() as follows
void USART_SendByte(uint8_t data)
{
USART_SendData8((unsigned char)data);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);
}
Pay attention to querying the USART_FLAG_TXE flag. When this flag is 0, wait until it is 1, the transmission is completed, and the function ends. The USART_FLAG_TXE flag is the "transmit data register empty flag".
-------------------------------------------------------------
The above is what I want to say. This log posts the program to query and send a byte, as follows:
/******************************Copyright (c)***********************************/
/* */
/* Lao Li's Electronic Work */
/* */
/*------------------------------File Info-------------------------------------*/
/* File name: main.c */
/* Last modified Date: 2014-06-19 */
/* Last Version: 1.0 */
/* Descriptions: STM8L103F3P6, internal clock, 16MHz, serial port query send */
/* */
/* Hardware connections: */
/* TX----PC3 */
/* RX----PC2 */
/* */
/*----------------------------------------------------------------------------*/
/* Created by: Li Xiang */
/* Created date: 2014-06-19 */
/* Version: 1.0 */
/* Descriptions: None */
/* */
/******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm8l10x.h"
#include "stm8l10x_usart.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define POWER_BD GPIO_Pin_0
#define POWER_BT GPIO_Pin_1
#define MSEL GPIO_Pin_2
#define NRESET GPIO_Pin_3
#define BD_NRESET GPIO_Pin_4
#define RESETB GPIO_Pin_5
#define SCL2 GPIO_Pin_6
#define SDA2 GPIO_Pin_7
#define SDA GPIO_Pin_0
#define SCL GPIO_Pin_1
#define SCREEN_CTRL GPIO_Pin_4
#define POWER_WIFI GPIO_Pin_0
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void BoardInit(void);
static void CLK_Init(void);
static void GPIO_Init_my(void);
static void USART_Config(void);
void USART_SendByte(uint8_t data);
void USART_SendString(uint8_t* Data,uint16_t len);
void Delay_ms(uint32_t nCount);
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Function name: main */
/* Descriptions: Main function */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
void main(void)
{
BoardInit();
while (1){
USART_SendString("Hello world!rn",14);
Delay_ms(1000);
USART_SendByte('H');Delay_ms(200);
USART_SendByte('e');Delay_ms(200);
USART_SendByte('l');Delay_ms(200);
USART_SendByte('l');Delay_ms(200);
USART_SendByte('o');Delay_ms(200);
USART_SendByte(' ');Delay_ms(200);
USART_SendByte('w');Delay_ms(200);
USART_SendByte('o');Delay_ms(200);
USART_SendByte('r');Delay_ms(200);
USART_SendByte('l');Delay_ms(200);
USART_SendByte('d');Delay_ms(200);
USART_SendByte('!');Delay_ms(200);
USART_SendString("rn",2);Delay_ms(200);
Delay_ms(1000);
}
}
/******************************************************************************/
/* Function name: BoardInit */
/* Descriptions: Main function */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
static void BoardInit(void)
{
CLK_Init();
GPIO_Init_my();
USART_Config();
}
/******************************************************************************/
/* Function name: CLK_Init */
/* Descriptions: Clock initialization function */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
static void CLK_Init(void)
{
CLK_DeInit();
CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);
}
/******************************************************************************/
/* Function name: GPIO_Init_my */
/* Descriptions: IO initialization function */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
static void GPIO_Init_my(void)
{
GPIO_Init(GPIOA,GPIO_Pin_2|GPIO_Pin_3,GPIO_Mode_Out_PP_Low_Slow); //悬空未用
GPIO_Init(GPIOB,POWER_BD,GPIO_Mode_Out_PP_Low_Slow); //Default power off
GPIO_Init(GPIOB,POWER_BT,GPIO_Mode_Out_PP_Low_Slow); // Cancel unused
GPIO_Init(GPIOB,MSEL,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, Wifi mode selection
GPIO_Init(GPIOB,NRESET,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, reset Wifi
GPIO_Init(GPIOB,BD_NRESET,GPIO_Mode_Out_PP_Low_Slow); //Beidou reset signal, default reset state
GPIO_Init(GPIOB,RESETB,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, reset Bluetooth
GPIO_Init(GPIOB,SDA2|SCL2,GPIO_Mode_Out_OD_HiZ_Slow); //Battery power usage
GPIO_Init(GPIOC,SDA|SCL,GPIO_Mode_Out_OD_HiZ_Slow); //Temperature sensor
GPIO_Init(GPIOC,GPIO_Pin_2,GPIO_Mode_In_PU_No_IT); //Serial port receiving
GPIO_Init(GPIOC,GPIO_Pin_3,GPIO_Mode_Out_PP_High_Slow); //串口发送
GPIO_Init(GPIOD,GPIO_Pin_All,GPIO_Mode_Out_PP_Low_Slow); //Cancel unused, Wifi power supply
}
/******************************************************************************/
/* Function name: USART_Config */
/* Descriptions: Serial port initialization function */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
static void USART_Config(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);
USART_DeInit();
USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,
USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));
USART_Cmd(ENABLE);
}
/******************************************************************************/
/* Function name: UART1_SendByte */
/* Descriptions: Send single byte */
/* input parameters: data: data to be sent */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
void USART_SendByte(uint8_t data)
{
USART_SendData8((unsigned char)data);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);
}
/******************************************************************************/
/* Function name: UART1_SendString */
/* Descriptions: Send string */
/* input parameters: None */
/* output parameters: 无 */
/* Returned value: None */
/******************************************************************************/
void USART_SendString(uint8_t* Data,uint16_t len)
{
uint16_t i=0;
for(;i } /******************************************************************************/ /* Function name: Delay_ms */ /* Descriptions: Delay function */ /* input parameters: delay time */ /* output parameters: 无 */ /* Returned value: None */ /******************************************************************************/ void Delay_ms(uint32_t nCount) { uint16_t i=0,j=0;; for(i=0;i ; } } } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number
Previous article:STM8 MCU STVD environment to view the instruction running time
Next article:STVD reported the problem of can't open file crtsi0.sm8
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Power supply overcurrent and overvoltage protection
- First make an adapter for the parallel interface of the first-generation development board
- Regarding the issue of modifying the BLUENRG-LP routine to realize the chip collecting internal voltage values and transmitting them to another Bluetooth serial port for printing via Bluetooth
- Op amp specification notes
- Variable frequency power supply components: What is the main function of the variable frequency power supply filter capacitor?
- Analysis of the application of TVS tube in the design of mobile phone surge protection
- ST FAE Answer: Reference circuit for replacing BlueNRG's balun BALF-NRG-02D3 with discrete devices
- Analysis of embedded real-time operating system uCOS II
- About atsaM E54 fuse position problem
- [RISC-V MCU CH32V103 Review] Using EXIT Interrupt