Cortex-M3 learning and debugging of stm32f10x UART

Source: Internet
Author: User
Cortex-M3 learning and debugging of stm32f10x UART

In the process of learning the stm32f10x Development Board, the first learning routine is the UART Printing program. Due to the previous experience of single-chip microcomputer, UART programming is not unfamiliar, however, in order to lay a solid foundation for programming, the routine should be analyzed and introduced in detail, hoping to discuss with those who share common interests.

1. stm32f10x UART Configuration

Complete the following settings to initialize the UART configuration of stm32f10x:

(1) Enable the clock of gpio and usart;
(2) set the gpio mode of two usart pins (the hardware traffic control function is not set );
(3) configure the usart data format, baud rate, and other parameters;
(4) Enable the usart function;

Sample Code:

Void usart_configuration (void)
{
Gpio_inittypedef gpio_initstructure;
Uart_inittypedef usart_initstructure;

Rcc_apb2periphclockcmd (rcc_apb2periph_gpioa | rcc_apb2periph_afio, enable );
Rcc_apb2periphclockcmd (rcc_apb2periph_usart1, enable );

Gpio_initstructure.gpio_pin = gpio_pin_9;
Gpio_initstructure.gpio_mode = gpio_mode_af_pp;
Gpio_initstructure.gpio_speed = gpio_speed_50mhz;
Gpio_init (gpioa, & gpio_initstructure );

Gpio_initstructure.gpio_pin = gpio_pin_10;
Gpio_initstructure.gpio_mode = gpio_mode_in_floating;
Gpio_init (gpioa, & gpio_initstructure );

Usart_initstructure.usart_baudrate = 115200;
Usart_initstructure.usart_wordlength = usart_wordlength_8b;
Usart_initstructure.usart_stopbits = usart_stopbits_1;
Usart_initstructure.usart_parity = usart_parity_no;
Usart_initstructure.usart_hardwareflowcontrol = usart_hardwareflowcontrol_none;
Usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx;
Usart_init (usart1, & usart_initstructure );

Usart_cmd (usart1, enable );

Usart_clearflag (usart1, uart_flag_tc );
}

Note the clever design of the two data results in this function, so that the UART initialization function is concise and clear.

Then the main character of this evening will appear ..........

1.1 gpio_inittypedef struct typedef struct
{
Uint16_t gpio_pin;
Gpiospeed_typedef gpio_speed;
Gpiomode_typedef gpio_mode;
} Gpio_inittypedef;

The gpiospeed_typedef and gpiomode_typedef data types are defined in this struct. We continue to track the data. The following definition is found: typedef Enum
{
Gpio_speed_10mhz = 1,
Gpio_speed_2mhz,
Gpio_speed_50mhz
} Gpiospeed_typedef;

Typedef Enum
{Gpio_mode_ain = 0x0,
Gpio_mode_in_floating = 0x04,
Gpio_mode_ipd = 0x28,
Gpio_mode_ipu = 0x48,
Gpio_mode_out_od = 0x14,
Gpio_mode_out_pp = 0x10,
Gpio_mode_af_od = 0x1c,
Gpio_mode_af_pp = 0x18
} Gpiomode_typedef; 1.2 uart_inittypedef struct typedef struct
{
Uint32_t usart_baudrate;
Uint16_t usart_wordlength;
Uint16_t usart_stopbits;
Uint16_t usart_parity;
Uint16_t usart_mode;
Uint16_t usart_hardwareflowcontrol;
} Usart_inittypedef;
The 1.3 program reads that the data structure is well designed, but how is it implemented? After all, these configurations must be written into specific CPU registers, it's not necessary to make the program cheaper than reading the basic functions ...... this should not be the case, so the assignment part continues to be tracked ..... (1) gpio_initstructure.gpio_pin = gpio_pin_9; # define gpio_pin_9 (uint16_t) 0x0200) // select the 9th pin
(2) gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_mode_af_pp = 0x18 //




As you can see, we have explained the use of typedef user-defined data types in the previous article, which is not difficult to understand. We are still unfamiliar with the use of enumeration data here, here I will review it.
2 enumeration C language provides a type called "enumeration. All possible values are listed in the definition of the "enumeration" type. The value of the variable described as this "enumeration" type cannot exceed the defined range. It should be noted that the enumeration type is a basic data type, rather than a construction type, because it cannot be broken down into any basic type.


2.1. Enumeration definition the general form of Enumeration type definition is:

Enum enumeration name {enumeration value table };

Enum enumeration name {enumeration value 1, enumeration value 2, enumeration value 3 ,.....}

All available values should be listed in the enumerated values table. These values are also called enumeration elements.

The following rules apply to enumeration types:

(1) The enumerated value is a constant, not a variable. You cannot assign values to a value in a program.

For example, if you assign the following values to the elements that enumerate weekday: Sun = 5; MON = 2; Sun = mon; all of them are incorrect.

(2) The enumeration element itself is defined by the system as a numerical value indicating the serial number. The order starting from 0 is defined as 0, 1, 2 ....

For example, in weekday, Sun is 0, mon is 1 ,..., The SAT value is 6.

(3) Enumeration elements are neither character constants nor string constants. Do not add single or double quotation marks when using them.


3. UART receiving and sending function 3.1 send data usart_senddata (usart1, (uint8_t)
Ch) 3.2 receive data usart_receivedata (usart1 );


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.