1. Purpose of the experiment
- Master the principle of STM32 serial port communication.
- Learn programming to realize UART communication of STM32.
2. Experimental equipment
Hardware: one PC
STM32 development board set
Software: MDK V4.0 set
Windows 7 set
A set of debugging assistants
- Experimental principle
1. Serial port circuit diagram of the development board:
2. Status register USART_SR and functions
5th , 6th RXNE and TC _ _
RXNE (read data register is not empty), when this bit is set to 1 , it means that data has been received and can be read out.
TC (transmission complete), when this bit is set, it means that the data in USART_DR has been sent.
In the firmware library function, the function to read the status of the serial port is:
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG );
The second entry parameter of this function is very critical, it is to indicate which state of the serial port we want to check, such as RXNE ( read data register is not empty ) and TC ( transmission complete ) explained above .
For example : to determine whether the read register is not empty (RXNE) , the method of operating the library function is:
USART_GetFlagStatus(USART1,USART_FLAG_RXNE);
To determine whether the transmission is complete (TC) , the method of operating the library function is:
USART_GetFlagStatus (USART1,USART_FLAG_TC);
/****************************************************************/
3. Serial port programming is as follows:
1) Serial port clock enable, GPIO clock enable
Configure the clock. Since the serial port is multiplexing the IO port, the serial port clock and the corresponding IO port clock need to be turned on.
2) GPIO port mode setting
The corresponding IO port of the serial port needs to be configured, the output port is configured as a multiplexed push-pull output , the output speed is configured as required, and the input port is configured as a floating input .
3) Initialize the serial port parameters and enable the serial port. ( The serial port debugging assistant is consistent with it )
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200 ; //baud rate
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1 stop bit
USART_InitStructure.USART_Parity = USART_Parity_No; // no parity bit
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// send and receive mode
USART_Init(USART1, &USART_InitStructure ); //Initialize serial port 1
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//Enable receive interrupt. If there is no interruption, this item does not need
USART_Cmd(USART1, ENABLE); //Enable serial port 1
4) Initialize NVIC and enable interrupts (this step is only required if interrupts are enabled)
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
//Preemption priority 0, the level can be set by yourself, and the following sub-priorities can be customized
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //sub-priority 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//IRQ channel enable
NVIC_Init(&NVIC_InitStructure); //Initialize the VIC register according to the specified parameters
5) Write an interrupt handler
4. How to use the debugging assistant
1) Download the compiled program to the development board. (At this time, the serial port debugging assistant needs to be closed ).
2) Call the debugging assistant software, set the COM port, configure the parameter as 115200 8-N-1, and open the serial port . See below
3) Press the reset button of the development board to display the information to be printed in our program on the serial port debugging assistant.
4. Experimental content
1. Use STM32 to design an experiment for serial communication with a computer. STM32 sends 123ABC to the PC , which is displayed by the serial port debugging assistant , and completed by query .
2. Use STM32 to design an experiment for serial communication with a computer. The PC sends 456789 to the STM32 , and after the STM32 receives it, it sends the value of each bit + 3 to the PC, and uses the serial port debugging assistant to display "789ABC" , and completes it with an interrupt .
3. Use STM32 to design an experiment of serial communication with the computer, and use the PC to control the LED on and off. Timing is accomplished with a timer.
(1) The PC sends A to the STM32 to control LED1, LED2, and LED3 to turn on and off at the same time, with an interval of 0.5s. At the same time, the serial port debugging assistant displays " LED1, LED2, and LED3 turn on and off at the same time".
(2) The PC sends B to the STM32 to control LED1, LED2, LED3 to turn on and off the forward flow of water, with an interval of 1 second. At the same time, the serial port debugging assistant displays " LED1, LED2, LED3 forward flow on and off".
(3) The PC sends C to the STM32 to control LED1, LED2, LED3 to turn on and off the reverse flow, with an interval of 2 seconds. At the same time, the serial port debugging assistant displays " LED1, LED2, LED3 reverse flow on and off".
Use STM32 to design an experiment for serial communication with a computer. STM32 sends 123ABC to PC, which is displayed by the serial port debugging assistant, and the query method is completed. (Code: Experiment 4-1)
(1) main main function program
( 2 ) Usart.c function , serial port 1 interrupt service routine
( 3 ) usart.h function
Use STM32 to design an experiment for serial communication with a computer. The PC sends 456789 to the STM32, and after the STM32 receives it, it sends the value of each bit + 3 to the PC, displays it with the serial port debugging assistant, and completes it with an interrupt. (Code: Experiment 4-2)
(1) main main function program
(2) usart serial port initialization program
(3) Serial port 1 interrupt service program in usart
(4) The usart.h program is as follows
Use STM32 to design an experiment for serial communication with the computer, and use the PC to control the LED on and off. (Code: Experiment 4-3)
(1) Led initialization program
(2) The initialization program of usart
(3) Writing the delay function program
(4) Writing the main function program
4. Experiment summary.
In this experiment——serial port communication experiment, I learned how to use the serial port to communicate between the MCU and the computer, as well as the way of burning. Have a better understanding of the principle of STM32 serial port communication. Learned how to use programming to realize UART communication of STM32. In the future, in the study, I will still guard against arrogance and impetuosity, and pay more attention to hands-on ability, so that what I have learned in class can be used in projects, so that practice makes perfect.
search
copy