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.
Previous article:STM32f10x series project creation demonstration
Next article:STM32 USART 232 serial communication <1>
Recommended ReadingLatest update time:2024-11-15 15:10
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Looking for a board-to-board connector model
- Can you guys tell me how to do warm tonic?
- 【GD32L233C-START Review】VII. TIMER
- TCP/IP Detailed Volume 1: Protocols (2nd Edition)
- Charging box modified LED light
- Write a Unico tool for STEVAL-MKI109V3
- Human body blowing sensor module
- Parking Robot Embedded Engineer Recruitment
- C++ Computer Vision OpenCV Official Introduction (2017 Edition)
- I built a chassis over the weekend.