1) serial clock enabling and gpio clock enabling
2) Serial Port Reset
3) gpio port Mode settings
4) serial port parameter initialization
5) Enable interrupt and initialize interrupt C (this step is required if interrupt is enabled)
6) Enable serial port
7) Compile the Interrupt Processing Function
Serial clock enabling. The serial port is a peripheral mounted under apb2, so the enable function is:
Rcc_apb2periphclockcmd (rcc_apb2periph_usart1 );
2. Serial Port reset. When a peripheral encounters an exception, You can reset the settings to reset the peripheral and reconfigure it.
This peripheral achieves the goal of re-working it. Generally, when the system begins to configure peripherals, it will first reset the external
Operation. Reset is completed in the usart_deinit () function:
Void usart_deinit (usart_typedef * usartx); // reset the serial port
For example, we want to reset Serial Port 1:
Usart_deinit (usart1); // reset Serial Port 1
3. initialize the serial port parameters. Serial Port Initialization is implemented through the usart_init () function,
Void usart_init (usart_typedef * usartx, usart_inittypedef * usart_initstruct );
The first entry parameter of this function is to specify the serial port number for initialization. usart1 is selected here.
The second entry parameter is a struct pointer of the usart_inittypedef type.
To set parameters for the serial port. The general implementation format is:
Usart_initstructure.usart_baudrate = bound; // this parameter is generally set to 9600;
Usart_initstructure.usart_wordlength = usart_wordlength_8b; // the data format is 8 characters long.
Usart_initstructure.usart_stopbits = usart_stopbits_1; // a 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;
// Sending and receiving modes
Usart_init (usart1, & usart_initstructure); // initialize the serial port
From the above initialization format, we can see that the parameters to be set for initialization are: baud rate, character length, stop bit, and parity bit,
Hardware data flow control, mode (receiving, sending ). You can set these parameters as needed.
4. send and receive data. The sending and receiving of stm32 is implemented through the data register usart_dr.
A dual register, including TdR and RDR. When writing data to this register, the serial port will automatically send the data.
This register also exists when data arrives.
The function used by the stm32 library to operate the usart_dr register to send data is:
Void usart_senddata (usart_typedef * usartx, uint16_t data );
This function is used to write data to the serial port register usart_dr.
The function for reading data received by the serial port through the usart_dr register of the stm32 library is:
Uint16_t usart_receivedata (usart_typedef * usartx );
This function can be used to read data received by the serial port.
5. serial port status. The serial port status can be read through the Status Register usart_sr. Descriptions of usart_sr are as follows:
Here, let's take a look at two digits: 5th, six digits rxne, and TC.
Rxne (the read data register is not empty). When this bit is set to 1, it prompts that data has been received and
And can be read. In this case, we need to read usart_dr as soon as possible. By reading usart_dr, we can
This bit is cleared. You can also write 0 to this bit to clear it directly.
TC (sent successfully). When this bit is set, it indicates that the data in usart_dr has been sent completely. For example
If this bit is set, an interruption occurs. This bit can also be cleared in two ways: 1) read usart_sr, write
Usart_dr. 2) Write 0 directly to this bit.
We will not explain the other bit of the Status Register too much here. You need to refer to the Chinese reference manual.
In our Firmware Library function, the function for reading the serial port status is:
Flagstatus usart_getflagstatus (usart_typedef * usartx, uint16_t usart_flag );
The second entry parameter of this function is critical. It indicates the status of the serial port to be checked, as described above.
Rxne (the read data register is not empty) and TC (sent completely ). For example, we need to determine whether the read register is not empty (rxne ).
The method for database functions is:
Usart_getflagstatus (usart1, usart_flag_rxne );
We need to determine whether the sending is complete (TC). The method of operating the library function is:
Usart_getflagstatus (usart1, usart_flag_tc );
These identification numbers are defined in the MDK using macros:
# Define usart_it_pe (uint16_t) 0x0028)
# Define usart_it_txe (uint16_t) 0x0727)
# Define usart_it_tc (uint16_t) 0x0626)
# Define usart_it_rxne (uint16_t) 0x0525)
# Define usart_it_idle (uint16_t) 0x0424)
# Define usart_it_lbd (uint16_t) 0x0846)
# Define usart_it_cts (uint16_t) 0x096a)
# Define usart_it_err (uint16_t) 0x0060)
# Define usart_it_ore (uint16_t) 0x0360)
# Define usart_it_ne (uint16_t) 0x0260)
# Define usart_it_fe (uint16_t) 0x0160)
6. Serial Port enabling. Serial Port enabling is implemented through the usart_cmd () function, which is easy to understand and can be used.
Yes:
Usart_cmd (usart1, enable); // enable serial port
7. Enable serial port response interruption. In some cases, when we need to enable the serial port interruption, we also need to enable
The function to enable serial port interruption is:
Void usart_itconfig (usart_typedef * usartx, uint16_t usart_it,
Functionalstate newstate)
The second entry parameter of this function indicates the type of the Enable serial port, that is, the type of the Enable interrupt, because of the interrupt class of the serial port.
There are many types. For example, when we receive the data (the rxne read data register is not empty), we need to interrupt it.
You can enable the interrupt:
Usart_itconfig (usart1, usart_it_rxne, enable); // enable interruption and receive data interruption
When sending data is completed (TC, sending is complete), we need to interrupt the data:
Usart_itconfig (usart1, usart_it_tc, enable );
8. Obtain the interrupt status. When we enable an interruption, when the interruption occurs, the status will be set to send
A flag in the memory. In the interrupt processing function, we often need to determine which interrupt the interrupt is. The function used is:
Itstatus usart_getitstatus (usart_typedef * usartx, uint16_t usart_it)
For example, if we enable the serial port sending to complete the interruption, when the interruption occurs, we can call
Function to determine whether the serial port sending is interrupted. The method is:
Usart_getitstatus (usart1, usart_it_tc)
The returned value is set, indicating that the serial sending is interrupted.