stm32f103 serial port Implementation mapping function

Source: Internet
Author: User

In the actual development, often encountered the serial port of the default output IO port is occupied by other modules, so we have to use the serial port IO port mapping function, refers to the original implementation of the function of the IO port mapping to other specified IO port, the other unchanged. Here's how:

First post the default serial port initialization settings:

void usart1conf (U32 baudrate) {Usart_inittypedef usart_initsturct;//defines the initialization structure for serial port 1 gpio_inittypedef gpio_initstruct;// Defines the structure of the serial port corresponding pin Rcc_apb2periphclockcmd (Rcc_apb2periph_usart1 | Rcc_apb2periph_gpioa, ENABLE);//open serial pin clock//usart1_tx_pin Configure gpio_initstruct.gpio_pin = gpio_pin_9;//output pin Gpio_ Initstruct.gpio_speed = gpio_speed_2mhz;//Set maximum speed 50mhzgpio_initstruct.gpio_mode = gpio_mode_af_pp;//push-pull multiplexing output gpio_init (Gpioa, &gpio_initstruct);//load the initialized structure into the register//usart1_rx_pin Configure gpio_initstruct.gpio_mode = Gpio_mode_in_ Floating;//gpio mode suspension Input gpio_initstruct.gpio_pin = gpio_pin_10;//input pin gpio_init (GPIOA, &gpio_initstruct);// Load the initialized structure into the register//usart1 Configure rcc_apb2periphclockcmd (Rcc_apb2periph_usart1, enable);//Clock Enable Usart_ Initsturct.usart_baudrate = baudrate;//Baud rate 19200usart_initsturct.usart_wordlength = usart_wordlength_8b;// Data width 8 bit usart_initsturct.usart_stopbits = usart_stopbits_1;//a stop bit usart_initsturct.usart_parity = USART_Parity_No;// No parity Usart_initsturct.usart_hardwareflowcontrol = USart_hardwareflowcontrol_none; Usart_initsturct.usart_mode = Usart_mode_rx | usart_mode_tx;//enable send and receive Usart_init (USART1, &USART_INITSTURCT);//load the initialized structure into the register//usart1_int Configureusart_ Itconfig (USART1, Usart_it_rxne, enable);//enable to receive interrupts//usart_itconfig (USART1, Usart_it_txe, enable); Usart_cmd (USART1, ENABLE);//Open the serial port Usart_clearflag (USART1, USART_FLAG_TC);//solve the problem of the first data sending failure}

The following is the mapped serial port settings, according to datasheet, remap settings io port: RX:PA9----PB6 tx:pa10------PB7

void usart1conf (U32 baudrate) {Usart_inittypedef usart_initsturct;//defines the initialization structure for serial port 1 gpio_inittypedef gpio_initstruct;// Define serial port corresponding PIN structure//Use serial port IO port mapping function add by LC 2015.07.01 18:03//remap settings PA9----PB6 PA10------pb7rcc_apb2periphclockcmd (Rcc_ap b2periph_afio,enable);//enable to map IO module rcc_apb2periphclockcmd (rcc_apb2periph_usart1 | RCC_APB2PERIPH_GPIOB, ENABLE);//open serial pin clock//usart1_tx_pin Configure gpio_initstruct.gpio_pin = gpio_pin_6;//output pin Gpio_ Initstruct.gpio_speed = gpio_speed_2mhz;//Set maximum speed 50mhzgpio_initstruct.gpio_mode = gpio_mode_af_pp;//push-pull multiplexing output gpio_init (Gpiob, &gpio_initstruct);//load the initialized structure into the register//usart1_rx_pin Configure gpio_initstruct.gpio_mode = Gpio_mode_in_ Floating;//gpio mode suspension Input gpio_initstruct.gpio_pin = gpio_pin_7;//input pin gpio_init (GPIOB, &gpio_initstruct);// Load the initialized structure into register gpio_pinremapconfig (gpio_remap_usart1,enable);//end by LC 2015.07.01 18:03//usart1 Configure rcc_ Apb2periphclockcmd (Rcc_apb2periph_usart1, enable);//clock Enable usart_initsturct.usart_baudrate = baudrate;// Baud Rate 19200usart_iNitsturct.usart_wordlength = usart_wordlength_8b;//data width 8 bits usart_initsturct.usart_stopbits = USART_StopBits_1;// One stop bit usart_initsturct.usart_parity = usart_parity_no;//no parity Usart_initsturct.usart_hardwareflowcontrol = USART_ Hardwareflowcontrol_none; Usart_initsturct.usart_mode = Usart_mode_rx | usart_mode_tx;//enable send and receive Usart_init (USART1, &USART_INITSTURCT);//load the initialized structure into the register//usart1_int Configureusart_ Itconfig (USART1, Usart_it_rxne, enable);//enable to receive interrupts//usart_itconfig (USART1, Usart_it_txe, enable); Usart_cmd (USART1, ENABLE);//Open the serial port Usart_clearflag (USART1, USART_FLAG_TC);//solve the problem of the first data sending failure}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

stm32f103 serial port Implementation mapping function

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.