STM32 USART serial communication

Publisher:谁与争锋1Latest update time:2017-09-21 Source: eefocusKeywords:stm32  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Simple demo 
communicates with the host computer through the 232 serial port. First, ensure that the MCU 232 serial port is connected to the computer's serial port. Of course, the computer is a USB interface, and the essence of the USB interface is also a serial port. We implement the host computer to send a hexadecimal data, and the MCU displays the data after receiving it, and sends it twice to the host computer. 
1. usart_init(); Serial port initialization

void usart_init()
{
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructrue;
   NVIC_InitTypeDef NVIC_InitStructure;
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE); //Turn on GPIOA and USART clocks
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //Function multiplexing IO clock
   //GPIO initialization configuration
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//TX PA9
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplexed push-pull output
   GPIO_Init(GPIOA,&GPIO_InitStructrue);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//RX PA10
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //Floating input
   GPIO_Init(GPIOA,&GPIO_InitStructure); //USART initialization configuration
   USART_InitStructure.USART_BaudRate = 9600; //Set the baud rate to 9600
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
   USART_InitStructure.USART_StopBits = USART_StopBits_1;
   USART_InitStructure.Parity = USART_Parity_No;
   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Serial port mode is send and receive mode
   USART_Init(USART1,&USART_InitStructure);
   USART_Cmd(USART1,ENABLE);
   USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //Turn on the transmit interrupt, and a data interrupt is generated in the buffer
   USART_ClearFlag(USART1,USART_FLAG_TC); //Clear the send completion flag   
   //NVIC interrupt configuration
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //Priority group 1, with 1 preemptive priority and 3 slave priorities
   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Open the global interrupt of USART1
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preemption priority is 0
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority is 0
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable
   NVIC_Init(&NVIC_InitStructure);
}12345678910111213141516171819202122232425262728293031323334

2. USART1_IRQHandler() serial port interrupt function

void USART1_IRQHandler(void)
{   static u8 d;
   USART_ClearFlag(USART1,USART_FLAG_TC); //When the receive buffer is not empty, receive data if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET){
   d = ReceiveData(USART1);
   d = 2*d;
   USART_Send(USART1,d); //Check if the send completion flag is set to 1 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) != SET);}
}12345678910111213

3. main() main function

int main()
{
   usart_init(); //Serial port 1 initialization
   while(1);   return 0;
}123456

The effect of this main function is to set the serial port baud rate to 9600, set HEX sending and 
display  ,
and  add 1 to the hexadecimal number for display.
Write the picture description here


Keywords:stm32  USART Reference address:STM32 USART serial communication

Previous article:STM32f10x series project creation demonstration
Next article:STM32 USART 232 serial communication <1>

Recommended ReadingLatest update time:2024-11-15 15:10

Precision control of irregular trajectory of two-dimensional slide based on STM32
In modern industrial control, the control of stepper motors is the actuator of slide control. The biggest feature that distinguishes it from other control motors is that it is controlled by input pulse signals, that is, the total rotation angle of the motor is determined by the number of input pulses, and the motor spe
[Microcontroller]
Precision control of irregular trajectory of two-dimensional slide based on STM32
Stm32 serial port sends string data
ps: Divide the string into bytes and send them cyclically   /* *illustrate: *PA0:KEY1;PA1:KEY2; *PA2:LED1;PA3:LED2; *PA9:USART1_TX;PA10:USART1_RX */ #include "stm32f10x.h" #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "stm32f10x_crc.h" #include "system_stm32f10x.h"
[Microcontroller]
Analysis of STM32 I2C bus occupancy problem
I have been solving the I2C bus occupancy (BUSY) problem of STM32 MCU these days . I think it is a good lesson to share with you. The STM32F207 MCU has three I2Cs. I2C1 is used to connect DSP/Codec/EEPROM in Pre-ES1 and it works well. The new hardware (ES1) moved the Codec/EEPROM to I2C3, but the DSP remained on I
[Microcontroller]
STM32 I2C Difficulties
I2C bus is widely used in all embedded systems and is an industrial-grade bus. However, since STM32 is a 32-bit MCU, its I2C hardware interface is destined to be powerful, but also difficult to control, unlike 8-bit machines, such as AVR8-bit machine's TWI (which actually fully complies with the I2C standard). The fol
[Microcontroller]
STM32 I2C Difficulties
Selection of stm32 chip startup file type
First, look at the startup file in the latest STM32 firmware library 3.5. As shown in the figure:  The corresponding relationship is as follows: 
[Microcontroller]
8 configuration modes of STM32 GPIO port
1. The input and output pins of STM32 have the following 8 possible configurations (4 inputs + 2 outputs + 2 multiplexed outputs):         ① Floating input_IN_FLOATING         ② With pull-up input_IPU          ③ With pull-down input_IPD                            ④ Analog input_AIN         ⑤ Open dra
[Microcontroller]
STM32-DMA study notes
       DMA, the full name is: Direct Memory Access, which means direct memory access. The DMA transmission method does not require the CPU to directly control the transmission, nor does it have the process of retaining and restoring the scene like the interrupt processing method. It opens a direct data transmission pat
[Microcontroller]
STM32 boot process complete solution
This article mainly explains the comprehensive analysis of the STM32 startup process, including the introduction of the startup process, the display of the startup code, and in-depth analysis.   Compared with the mainstream ARM7/ARM9 core architecture of the previous generation of ARM, the startup method of the new ge
[Analog Electronics]
STM32 boot process complete solution
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号