Create new bsp_usart.c and bsp_usart.h, add them to the project, and add the folder where the header files are located by the magic wand.
#include "bsp_usart.h"
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Nested vectored interrupt controller group selection*/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Configure USART as interrupt source*/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
/* Steal priority*/
NVIC_InitStructure .NVIC_IRQChannelPreemptionPriority = 1;
/* Subpriority*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* Enable interrupts*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* Initialize configuration NVIC */
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// Turn on the serial port GPIO clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Turn on the serial port peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// Configure USART Tx's GPIO as push-pull multiplexing mode
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);
// Configure the GPIO of USART Rx as floating input mode
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure the working parameters of the serial port
// Configure the baud rate
USART_InitStructure.USART_BaudRate = 115200 ;
// Configure pin data word length
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// Configure stop bit
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// Configure parity bit
USART_InitStructure.USART_Parity = USART_Parity_No ;
// Configure hardware flow control
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl / Configure the working mode, send and receive together
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// Complete the serial port initialization configuration
USART_Init(USART1, &USART_InitStructure);
// Serial port interrupt priority configuration
NVIC_Configuration();
// Enable serial port receive interrupt
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/ / Enable serial port
USART_Cmd(USART1, ENABLE);
}
/* Send a byte 8 bits*/
/*STM32 USART can only send 8 bits at a time*/
void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data)
{
USART_SendData(pUSARTx, data);
while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
}
/* Send two bytes of data* 16 bits*/
/*Send twice*/
void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data)
{
uint8_t temp_h,temp_l;/*High and low 8 bits*/
temp_h = (data&0xff00) >> 8 ;
temp_l = data&0xff;
/*Send the upper 8 bits first*/
USART_SendData(pUSARTx, temp_h);
while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
/*Send the lower 8 bits*/
USART_SendData(pUSARTx, temp_l);
while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET );
}
/* Send an array of 8-bit data*/
void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array, uint8_t num)
{
uint8_t i;
for( i=0; i<num; i++ )
{
Usart_SendByte(pUSARTx, array[i]) ;
}
//Sending 8 bits is for reading, TXE, greater than 8 bits to judge TC
while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
}
/* 发送字符串 */
void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str)
{
uint8_t i=0;
do
{
Usart_SendByte(pUSARTx, *(str+i));
i++;
}while(*(str+i) != '\0');
while( USART_GetFlagStatus(pUSARTx, USART_FLAG_TC) == RESET );
}
///Redirect the c library function printf to the serial port. After redirection, you can use the printf function
int fputc(int ch, FILE *f)
{
/* Send a byte of data to the serial port */
USART_SendData(USART1, (uint8_t) ch) ;
/* wait for sending to complete*/
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
///Redirect the c library function scanf to the serial port, rewrite and use scanf, getchar and other functions
int fgetc(FILE *f)
{
/* Wait for the serial port input data */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET );
return (int)USART_ReceiveData(USART1);
}
#ifndef __BSP_USART_H
#define __BSP_USART_H
#include "stm32f10x.h"
#include <stdio.h>
void USART_Config(void);
void Usart_SendByte(USART_TypeDef* pUSARTx, uint8_t data);
void Usart_SendHalfWord(USART_TypeDef* pUSARTx, uint16_t data);
void Usart_SendArray(USART_TypeDef* pUSARTx, uint8_t *array,uint8_t num);
void Usart_SendStr(USART_TypeDef* pUSARTx, uint8_t *str);
#endif