uart.h
#ifndef __UART_H
#define __UART_H
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
void uart_Init(uint32_t Baud);
void UartSendStr(USART_TypeDef* USARTx, char* str);
#endif
uart.c
#include "uart.h"
/*******************************************************************
* @brief 配置串口所需要的管脚
* @retval None
*/
void uart_IO_config(void)
{
//1.定义IO结构体
GPIO_InitTypeDef GPIO_InitStruct_uart; //配置串口所需要的管脚IO模式
//2.打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //打开A组管脚时钟
//3.设定GPIO TX、RX复用功能的配置
//TX配置PA9
GPIO_InitStruct_uart.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct_uart.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStruct_uart.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA ,&GPIO_InitStruct_uart); //管脚初始化
//RX配置PA10
GPIO_InitStruct_uart.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct_uart.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA ,&GPIO_InitStruct_uart); //管脚初始化
}
/*******************************************************************
* @brief 配置串口
* @param 波特率
* @retval None
*/
void uart_config(uint32_t Baud)
{
//1.定义串口结构体
USART_InitTypeDef USART_InitStruct;
//2.打开串口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //打开串口1
//3.配置串口结构体参数
//此成员配置USART通信波特率。
USART_InitStruct.USART_BaudRate = Baud; //此数值填常用数值即可
//指定是否启用硬件流控制模式 、或禁用。
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件控制流控制(一般填这个即可)
//指定启用还是禁用接收或传输模式。
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收发模式同时打开
//指定奇偶校验模式。
USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位
//指定传输的停止位数。
USART_InitStruct.USART_StopBits = USART_StopBits_1; //1个停止位(常用)
//指定帧中传输或接收的数据位数。
USART_InitStruct.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式(常用)
//4.串口1初始化
USART_Init(USART1, &USART_InitStruct);
//5.使能串口1
USART_Cmd(USART1, ENABLE);
}
/*******************************************************************
* @brief 结合以上函数,进行串口完整版初始化
* @param 波特率
* @retval None
*/
void uart_Init(uint32_t Baud)
{
uart_IO_config();
uart_config(Baud);
}
/*************************************************************************/
/**
*@brief 发送字符串
*@param 串口管脚号
*@param 字符串
*/
void UartSendStr(USART_TypeDef* USARTx, char* str)
{
while(*str != '\0')
{
USART_SendData(USARTx, *str);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
str++;
}
// while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
/*************下面这段代码直接用即可,串口号不同则需要更改串口号*************/
/****************************************************************************/
/**
*@brief 重定向printf函数
*@param 不知
*@param 不知
*/
/*重定向c库函数printf到串口RS232_USART,重定向后可使用printf函数*/
int fputc(int ch, FILE *f)
{
/* 发送一个字节数据到串口RS232_USART */
USART_SendData(USART1, (uint8_t) ch);
/* 等待发送完毕 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
return (ch);
}
/*重定向c库函数scanf到串口RS232_USART,重写向后可使用scanf、getchar等函数*/
int fgetc(FILE *f)
{
/* 等待串口输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
/*****************************END OF FILE************************************/
main.c
#include "stm32f10x.h"
#include "uart.h"
int main()
{
//调用uart初始化函数
uart_Init(115200);
while(1)
{
printf("%c",getchar());//接收字符,并发送出去
}
}