Usart.h
#ifndef __usart_h__#define __usart_h__#include "stm32f10x.h" #include <stdio.h>void gpio_configuration (void); void nvic_configuration (void); void usart_configuration (void); #endif
Usart.c
#include "usart.h"//Add the following code to support the printf () function without having to check use microlib #if 1#pragma import (__use _no_semihosting) //The standard library requires supported functions struct __file { int handle; }; file __stdout; //definition _ Sys_exit () function to avoid using semi-master mode _sys_exit (int x) { x = x; } //redefine fputs () function int FPUTC (int ch, file *f) { while (USART_GetFlagStatus (USART1, USART_FLAG_TC) ==reset) usart_senddata (USART1, (uint8_t) ch); Return ch;} #endif void gpio_configuration (void) {gpio_inittypedef gpio_initstructure; //Enable Gpio multiplexing function Clock rcc_apb2periphclockcmd (rcc_apb2periph_gpioa | rcc_apb2periph_afio,enable);// TX multiplexed Push-Pull output gpio_initstructure.gpio_speed = gpio_speed_50mhz; gpio_initstructure.gpio_mode = gpio_mode_af_pp; gpio_initstructure.gpio_pin = gpio_pin_9; Gpio_init (gpioa,&gpio_initstructure);//rx float input Gpio_initstructure.gpio_mode = gpio_mode_in_ floating ; gpio_initstructure.gpio_pin = gpio_pin_10; Gpio_init (gpioa,&gpio_initstructure);} Void nvic_configuration (void) {nvic_inittypedef nvic_initstructure; Nvic_prioritygroupconfig (NVIC_PRIORITYGROUP_0); nvic_initstructure.nvic_irqchannel = usart1_irqn ; nvic_initstructure.nvic_irqchannelpreemptionpriority = 0; nvic_initstructure.nvic_irqchannelsubpriority= 0; nvic_initstructure.nvic_irqchannelcmd = enable; Nvic_init (&nvic_initstructure);} Void usart_configuration (void) {usart_inittypedef usart_initstructure; Rcc_apb2periphclockcmd (rcc_apb2periph_usart1,enable); Usart_deinit (USART1); Usart_initstructure.usart_baudrate = 9600; 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_tx | usart_mode_rx; usart_init (usart1,&usart_initstructure);//must first enable USART1, Again to enable the interruption. The order is not interchangeable. Because the program jumps directly into the interrupt function and executes the sending command after the initial interrupt, the Usart must be enabled before it can be sent properly. Usart_cmd (usart1,enable); Usart_itconfig (usart1,usart_it_tc,enable);}
Main.c
#include "MyTime.h" #include "usart.h" #include <stdbool.h> const char arr[] = "Hello Stm32,hello usart!!!"; int main (void) {mysystick_init (); Gpio_configuration (); Nvic_configuration (); Usart_configuration (); while (1);} void Usart1_irqhandler (void) {static S16 i = 0;static s16 ncount = sizeof (arr)/sizeof (arr[0]); static bool Bfirstin = True ;//At the very beginning of sending data, you need to clear the USART flag bit if (bfirstin) {usart_clearflag (USART1,USART_IT_TC); bfirstin = false;} if (Usart_getitstatus (USART1,USART_IT_TC) = = SET) {printf ("%c", arr[i++]); Mydelay_ms (+), if (I>=ncount) {printf ("\ r \ n"); i=0;}}}
Note the data is sent and When receiving, check the usart usart_getflagstatus () function.
also note that at the very beginning of sending data, you need to clear theUSARTthe flag bit, otherwise, the first1The bit data is lost. Because after a hardware reset,USARTthe status bitTCis a set position. When a frame containing the data has been sent, the location is bit by the hardware. Just whenUSARTthe status bitTCis set, the data can be sent. ThenTCbits are zeroed out by software sequences, and the steps are "read FirstUsart_sr, and then writeUSART_DR", the only way to clear the flag bitTC, but when the first frame of data is sent, it is not readUsart_sroperations, but writes directly, soTCThe flag bit is not emptied, then, when the first frame of data is sent, then theusart_getflagstatus ()The detection status is returned when it has been sent (becauseTCbit is set1), so the program will immediately send the next frame of data, so that the first frame of data is overwritten by the second frame of data, so the first frame of data is not visible to send.
This article is from the "whatever957" blog, make sure to keep this source http://whatever957.blog.51cto.com/6835003/1631983
Usart Interrupt Receiver Program