Stm32 Usart Serial Port

Source: Internet
Author: User

This paper takes USART1 as an example to describe the programming process of serial port interruption.

1, first to tell about the application of the string in the mouth of some of the library files involved.

First of all, for STM32 Peripheral library file application programming, MISC.C and STM32F10X_RCC.C are sure to be added to.

And then there's the peripherals we're going to use. There is no doubt that serial file stm32f10x_usart.c is necessary. Serial communication is the function of general-purpose gpio port pins, so it is necessary to stm32f10x_gpio.c files. In addition, because of the interruption of the production, so interrupt file stm32f10x_it.c is also necessary, of course this file and main.c placed in a folder (generally used as the user folder), because our interrupt response function is to be written in the inside.

Of course, there are other basic necessary files such as system configuration files in this place will not say, this is to create a project should know.

2. Initialization

For the initialization of serial communication, not just the initialization of the serial port (this place is more annoying, unlike other chips as concise and clear).

L first clocks are enabled to configure. STM32 internal clock has a lot of interest to look at the reference manual. Here, take USART1 as an example to illustrate. There are USART1 clocks, gpioa clocks, Gpio multiplexing (Afio) clocks. Since USART1 and Gpioa and Afio are all on APB2 here, you can configure it once. As follows:

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_afio| Rcc_apb2periph_usart1, ENABLE);

L Second Interrupt configuration. Mainly has the priority group setting, the USART1 interrupt enables, the interrupt priority, interrupts initializes. The procedure is as follows:

void Nvic_configuration (void)

{

Nvic_inittypedef nvic_initstructure;

Nvic_prioritygroupconfig (NVIC_PRIORITYGROUP_0)//select group mode 0

/* Enable USART1 interruption * *

Nvic_initstructure.nvic_irqchannel = USART1_IRQN;

nvic_initstructure.nvic_irqchannelsubpriority = 0;

Nvic_initstructure.nvic_irqchannelcmd = ENABLE;

Nvic_init (&nvic_initstructure);

}

L then Gpio multiplexing function configuration. In general, we use the original peripherals and the GPIO port pin mapping relationship, if you want to change its mapping, please look at the reference manual on the Gpio part of the remapping. For Gpio multiplexing, the input and output modes of the pins are required, which are described in detail in the reference manual.

void Gpio_configuration (void)

{

Gpio_inittypedef gpio_initstructure;

/* Configure USART1 Rx as float input/*

Gpio_initstructure.gpio_pin = gpio_pin_10;

Gpio_initstructure.gpio_mode = gpio_mode_in_floating;

Gpio_init (Usarty_gpio, &gpio_initstructure);

/* Configure USART1 Tx as push-pull output * *

Gpio_initstructure.gpio_pin = Gpio_pin_9;

Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;

Gpio_initstructure.gpio_mode = gpio_mode_af_pp;

Gpio_init (Usarty_gpio, &gpio_initstructure);

}

L serial Port initialization configuration. The main configuration of the basic parameters of the serial port (such as baud rate, data bits, working methods, etc.), the serial port can be interrupted, the serial port can be.

(1) Basic parameter configuration

Usart_inittypedef usart_initstructure;

Usart_initstructure.usart_baudrate = 9600;//baud rate

Usart_initstructure.usart_wordlength = usart_wordlength_8b;//Data length

Usart_initstructure.usart_stopbits = usart_stopbits_1;//Stop bit

usart_initstructure.usart_parity = usart_parity_no;//Checksum

Usart_initstructure.usart_hardwareflowcontrol

=usart_hardwareflowcontrol_none; Hardware flow control without

Usart_initstructure.usart_mode = Usart_mode_rx | Usart_mode_tx; Two ways of sending and receiving

Usart_init (USART1, &usart_initstructure);//surprise serial port initialization with configuration parameters

(2) Serial port interruption enables

Usart_itconfig (Usarty, Usart_it_rxne, enable);//enable acceptance of interrupts, data in the receiving shift register is generated

Usart_itconfig (Usarty, Usart_it_txe, enable);//enable the sending of interrupts, generated after the data is sent.

In general, if you are communicating with your PC, we can only accept interrupts.

(3) The serial port can

Usart_cmd (USART1, ENABLE); USART1 to enable

Well, after we don't go, we can send and receive data.

3, send data

Use function Usart_senddata (USART1, char data) to send only one character at a time. Of course we can send a string using the following function.

void Usart1_puts (char * str)

{

while (*STR)

{

Usart_senddata (USART1, *str++); Send a character

while (Usart_getflagstatus (USART1, usart_flag_txe) = = RESET); Wait for send complete

}

}

Of course we can also loop through the string array

for (i = 0; TxBuf1!= '; i++)//TXBUF1

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.