The code for the main function is as follows:
/*----------------------------------------------------------------------------------function Description: Through the serial port 1, using printf function to print information Hardware platform: STM32F103 Development Board Author: Qiu Shi 07 firmware library: V3.5------------------------------------------------------------------------------ -----* * * * Includes------------------------------------------------------------------/* #include <stdio.h> Include "Stm32f10x.h" #include "led.h" #include "delay.h" #include "key.h" #include "timer.h" #include "beep.h" #ifdef __ gnuc__/* with gcc/raisonance, small printf (option LD linker->libraries->small printf set to ' Yes ') calls __ Io_putchar () */#define PUTCHAR_PROTOTYPE int __io_putchar (int ch) #else #define PUTCHAR_PROTOTYPE int FPUTC (int ch, F ILE *f) #endif/* __gnuc__/int main (void) {<span style= "white-space:pre" > </span>gpio_inittypedef gpio_in
Itstructure;
Usart_inittypedef usart_initstructure; Rcc_apb2periphclockcmd (rcc_apb2periph_usart1| Rcc_apb2periph_gpioa, ENABLE); Enable to Usart1,gpioa clock Usart_deinit (USART1); Reset serial port 1//usart1_tx pa.9 gpio_initstructure.gpio_pin = gpio_pin_9;
pa.9 gpio_initstructure.gpio_speed = Gpio_speed_50mhz; Gpio_initstructure.gpio_mode = gpio_mode_af_pp; Multiplexing push-Pull output gpio_init (Gpioa, &gpio_initstructure);
Initialization of PA9//usart1_rx pa.10 gpio_initstructure.gpio_pin = gpio_pin_10; Gpio_initstructure.gpio_mode = gpio_mode_in_floating;//Floating Air input gpio_init (GPIOA, &gpio_initstructure);
Initializing PA10/* Usartx configured as follow:-baudrate = 9600 baud baud rate-Word length = 8 Bits data Lengths -One stop bit stop bit-No parity check mode-Hardware flow control disabled (RTS and CTS signals) hardware control flow-receive and transmit enabled to send and receive * * Usart_initstructure.usar
T_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); Enable serial port Led_init (),//led initialization key_init (),//keystroke initialization systick_init (),//Delay initialization beep_init ();
Buzzer initialization while (1) {//using printf function to cycle send fixed information printf ("USART printf example:stm32 serial test program \ n");
Delay_ms (500);
Led2_rev; } putchar_prototype {/* Place your implementation's FPUTC here/* e.g. write a character to the USART * * US
Art_senddata (USART1, (uint8_t) ch);
/* loop wait until send end/while (Usart_getflagstatus (USART1, USART_FLAG_TC) = RESET) {} return ch;
}
Send data via serial 1
The schematic diagram is as follows:
Leveraging Library functions
Using the Serial debugging assistant you can see the following information:
Some of the configuration flow of the serial port can actually be seen from the serial debugging assistant.
There is a detail to be aware of when compiling the following: