stm32f4--serial port (USART) Communication summary

Source: Internet
Author: User

I. Overview:

The Usart is a universal synchronous asynchronous transceiver for flexible full-duplex data exchange with external devices, which supports a variety of communication modes and provides multiple baud rates via the fractional baud rate generator.

Second, serial IO:

For stm32f407 there are 6 serial ports, for each serial port corresponding to the IO can be found from the chip schematic diagram, for its serial port 1, the corresponding IO for the PA9/PB6 port corresponding to the serial 1 transmitter, PA10/PB7 port corresponding to the serial 1 receiver.

Third, communication parameters:

For data transmission needs to set up the relevant data transmission protocol, so the necessary parameter setting is necessary, including: Start bit, data bit (8 or 9 bits), parity bit (open or not), stop bit (1, 1.5, 2 bit), baud rate setting.

Iv. Related registers:

The relevant registers are: status register, data register, baud rate register, control register 1, Control register 2, control register 3, protection time and prescaler register; Below is a simple analysis of the first three registers.

1. Status Register:

The current known bits are: TXE: send register is empty; TC: Send complete; RXNE: Read data register is not empty; ORE: overflow error; FE: Frame error; PE: Parity error.

2, Data register: The register is only low 8 bits valid, and has read and write operations, the data in the register to send the data to be sent or the data received depends on whether the operation performed is read or write.

3. Baud Rate Register:

The baud rate register is used to generate the corresponding baud rate through the clock by setting the relevant values. The relationship between the numerical setting of the serial port 1 and the baud rate is as follows:

where the corresponding value is set in the register, div_mantissa[11,0] is used to set the integer part of the crossover factor, div_fraction[3,0] is used to set the fractional part of the divider factor.

V. Related Code Analysis:

Follow the configuration process of the serial port to analyze the relevant code:

1. Related clock enable:

	Rcc_ahb1periphclockcmd (rcc_ahb1periph_gpioa,enable); Enable Gpioa clock
	rcc_apb2periphclockcmd (rcc_apb2periph_usart1,enable);//enable USART1 clock

2. Pin multiplexing Mapping:

	Serial 1 corresponds to pin multiplexing mapping
	gpio_pinafconfig (GPIOA,GPIO_PINSOURCE9,GPIO_AF_USART1);//gpioa9 multiplexing is USART1
	Gpio_ Pinafconfig (GPIOA,GPIO_PINSOURCE10,GPIO_AF_USART1); GPIOA10 Multiplexing for USART1

3. Gpio Port Mode setting:

	USART1 port configuration
        gpio_initstructure.gpio_pin = Gpio_pin_9 | gpio_pin_10; GPIOA9 and GPIOA10
	gpio_initstructure.gpio_mode = gpio_mode_af;//multiplexing function
	gpio_initstructure.gpio_speed = Gpio_ Speed_50mhz;	Speed 50MHz
	gpio_initstructure.gpio_otype = gpio_otype_pp;//push-pull multiplexing output
	gpio_initstructure.gpio_pupd = Gpio_pupd_ up; Pull
	-up gpio_init (gpioa,&gpio_initstructure);//Initialize PA9,PA10

4. Serial Port parameter setting:

        USART1 initialization setting
	usart_initstructure.usart_baudrate = bound;//baud rate setting
	Usart_initstructure.usart_wordlength = usart_wordlength_8b;//Word length is 8-bit data format
	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;	Transceiver Mode
    usart_init (USART1, &usart_initstructure);//Initialize serial port 1

5. Configuration related interrupts:

#if en_usart1_rx	
	usart_itconfig (USART1, Usart_it_rxne, enable);//Turn on related interrupts

	//usart1 NVIC configuration
        nvic_ Initstructure.nvic_irqchannel = usart1_irqn;//Serial 1 interrupt channel
	nvic_initstructure.nvic_irqchannelpreemptionpriority=3 ;//preemption Priority 3
	nvic_initstructure.nvic_irqchannelsubpriority =3;		Sub-priority 3
	nvic_initstructure.nvic_irqchannelcmd = ENABLE;			IRQ Channel enable
	Nvic_init (&nvic_initstructure);	Initializes the VIC register, #endif based on the specified parameters

6, enable the serial port (and pay attention to the relevant flag bit):

        Usart_cmd (USART1, ENABLE);  Enable serial port 1 
	usart_clearflag (USART1, USART_FLAG_TC);

7. Interrupt Service function (self-processing as required):

	if (Usart_getitstatus (USART1, usart_it_rxne)! = RESET)  //Receive interrupt (Received data must be 0x0d 0x0a end)
	{
		Res =usart_ Receivedata (USART1);//(USART1->DR);	Read the Received Data
		
		if ((usart_rx_sta&0x8000) ==0)//Receive incomplete
		{
			if (usart_rx_sta&0x4000)//received 0x0d
			{
				if (res!=0x0a) usart_rx_sta=0;//receive error, restart
				else usart_rx_sta|=0x8000;	The Receive 
			is complete}
			else//has not received 0x0d
			{	
				if (res==0x0d) usart_rx_sta|=0x4000;
				else
				{
					usart_rx_buf[usart_rx_sta&0x3fff]=res;
					usart_rx_sta++;
					if (usart_rx_sta> (usart_rec_len-1)) usart_rx_sta=0;//receive data error, start receiving again}}}}   		 
  
8, the rest is based on the relevant transmission status of the serial port to send and receive data, according to their own needs for design and analysis.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.