图文概述:
代码:
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
/*对应的引脚号*/
#define USART1_TX GPIO_Pin_9
#define USART1_RX GPIO_Pin_10
/*模块需要使用到的端口:GPIOA或GPIOB*/
#define BUS GPIOA
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
/**
* @brief Serial_Init---对串口通信的初始化配置(针对发送数据即USART1外设的TX引脚)
* @param 无
* @retval 无
*/
void Serial_Init(void)
{
//1.开启APB2外设的时钟---USART1是APB2的外设
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//2.初始化GPIO的引脚配置(即USART1外设的TX引脚)
GPIO_InitTypeDef GPIO_InitStructure;
/* TX引脚是USART1外设控制的输出脚,需要选复用推挽输出模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = USART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(BUS, &GPIO_InitStructure);
/* RX引脚是USART1外设控制的输出脚,需要选浮空输入或上拉输入 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入模式
GPIO_InitStructure.GPIO_Pin = USART1_RX;
GPIO_InitStructure.GPIO_Speed = GPIO_Sp