Serial 串口读写

Serial.H

#ifndef __SERIAL_H
#define __SERIAL_H

void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array, uint16_t Length);
void Serial_SendString(char *String);

uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);

#endif

Serial.C

#include "stm32f10x.h" // Device header

uint8_t Serial_RxData; // 串口接收的数据
uint8_t Serial_RxFlag; // 串口接收数据的标志位

/**
 * 初始化串口
 */
void Serial_Init(void)
{
	// 开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	// 初始化 GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // PA9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;// PA10
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	// 初始化 USART
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 9600; // 波特率
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;	//发送 | 接收
	USART_InitStructure.USART_Parity = USART_Parity_No; // 奇偶校验
	USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位
	USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 字长
	USART_Init(USART1, &USART_InitStructure);
	
	// 开启串口接收数据的中断
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	
	// 配置 NVIC 中断分组
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	// 初始化 NVIC
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 响应优先级
	NVIC_Init(&NVIC_InitStructure);
	
	// 使能 USART1
	USART_Cmd(USART1, ENABLE);
}

/**
 * 发送一个字节
 */
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

/**
 * 发送一个数组
 */
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	for (uint16_t i = 0; i < Length; i++)
	{
		Serial_SendByte(Array[i]);
	}
}

/**
 * 发送字符串
 */
void Serial_SendString(char *String)
{
	for (uint16_t i = 0; String[i] != '\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

/**
 * 获取串口接收数据的标志位(0/1)
 * 接收到数据后,标志位置1,读取后标志位自动清零
 */
uint8_t Serial_GetRxFlag(void)
{
	if (Serial_RxFlag == 1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}

/**
 * 获取串口接收的数据
 */
uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;
}

/**
 * USART1 中断服务函数
 */
void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		Serial_RxData = USART_ReceiveData(USART1);
		Serial_RxFlag = 1; // 标志位置1
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);
	}
}

posted on 2025-10-26 22:10  星辰河岳  阅读(6)  评论(0)    收藏  举报