嵌入式学习--江协stm32day5

发布于:2025-06-09 ⋅ 阅读:(12) ⋅ 点赞:(0)

USART

1. 引脚与接口层
  • 异步引脚
    • TX:发送数据输出;RX:接收数据输入;SW_RX:单线半双工模式的接收引脚(替代 RX)。
  • 同步引脚SCLK:同步模式下的时钟输出(主模式)或输入(从模式)。
  • IrDA 接口IrDA_OUT/IrDA_IN,配合编解码模块实现红外通信(需外接 IrDA 收发器)。
  • 硬件流控引脚nRTS(请求发送,输出)、nCTS(清除发送,输入),协调收发双方速率。
2. 数据收发核心:寄存器 + 移位寄存器
  • 发送路径
    CPU/DMA → 写入 发送数据寄存器(TDR) → 送入 发送移位寄存器(并行→串行,逐位输出到 TX/IrDA/SW_RX)。
  • 接收路径
    RX/IrDA/SW_RX 接收串行数据 → 送入 接收移位寄存器(串行→并行)→ 存入 接收数据寄存器(RDR) → 供 CPU/DMA 读取。
  • 关键逻辑:移位寄存器负责 “串并转换”,TDR/RDR 作为数据缓冲(避免 CPU 频繁干预)。
3. 控制与配置:CR1/CR2/CR3 寄存器
  • CR1:基础配置,如:
    • UE:USART 使能;M:字长(8/9 位);WAKE:唤醒模式(空闲线 / 地址标记);
    • 中断使能(TXEIE 发送空、RXNEIE 接收非空、IDLEIE 总线空闲等)。
  • CR2:帧格式与同步控制,如:
    • STOP[1:0]:停止位数量(0.5/1/1.5/2 位);LINEN:LIN 总线模式;
    • 同步模式时钟配置(CPOL 极性、CPHA 相位、CLKEN 时钟使能)。
  • CR3:高级功能,如:
    • DMA 使能(DMAT 发送 DMA、DMA 接收 DMA);
    • 硬件流控(RTSE 使能 nRTS、CTSE 使能 nCTS);
    • 红外模式(IREN)、半双工(HDSEL)等。
4. 波特率生成:USART_BRR 寄存器
  • 核心公式
    USARTDIV=16×波特率fPCLK​​=DIV_Mantissa+16DIV_Fraction​
    • fPCLK:USART 外设时钟(来自 RCC 配置);
    • DIV_Mantissa:整数部分(15 位),DIV_Fraction:小数部分(4 位),共同决定波特率精度(如 9600、115200 等)。
  • 时钟分配:生成的 USARTDIV 同时供给 发送器时钟 和 接收器时钟,保证收发同步。
5. 状态与中断:SR 寄存器 + 中断控制
  • 状态标志(SR
    • TXE:发送数据寄存器空(可写入新数据);
    • RXNE:接收数据寄存器非空(可读取数据);
    • IDLE:总线空闲(异步模式下,用于检测一帧结束);
    • 错误标志:OE(溢出)、PE(奇偶校验错)、FE(帧错)、NE(噪声错)。
  • 中断控制:通过 CR1 使能对应中断(如 TXEIE 使能 TXE 中断),触发后进入 USART 中断控制模块,通知 CPU 处理。

串口发送

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include "stdarg.h"
void Serial_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructrue;
	USART_InitStructrue.USART_BaudRate=9600;
	USART_InitStructrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  USART_InitStructrue.USART_Mode=USART_Mode_Tx;
	USART_InitStructrue.USART_Parity=USART_Parity_No;
	USART_InitStructrue.USART_StopBits=USART_StopBits_1;
	USART_InitStructrue.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructrue);
	
	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)
{
	uint16_t i;
	for(i=0;i<Length;i++)
	{
		Serial_SendByte(Array[i]);
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i=0;String[i]!='\0';i++)
	{
		Serial_SendByte(String[i]);
	
	}
	
}
uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
	uint32_t Result;
	while(Y--)
	{
		Result*=X;
	}
	return Result;

}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
	uint8_t i;
	for(i=0;i<Length;i++)
	{
		
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
	
	}
}
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;

}

void Serial_printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);

}

串口发送+接收

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include "stdarg.h"

uint8_t Serial_RxFlag;
uint8_t Serial_RxData;
void Serial_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	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;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructrue;
	USART_InitStructrue.USART_BaudRate=9600;
	USART_InitStructrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  USART_InitStructrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStructrue.USART_Parity=USART_Parity_No;
	USART_InitStructrue.USART_StopBits=USART_StopBits_1;
	USART_InitStructrue.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructrue);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructrue;
	NVIC_InitStructrue.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructrue.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructrue.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructrue.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_InitStructrue);
	
	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)
{
	uint16_t i;
	for(i=0;i<Length;i++)
	{
		Serial_SendByte(Array[i]);
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i=0;String[i]!='\0';i++)
	{
		Serial_SendByte(String[i]);
	
	}
	
}
uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
	uint32_t Result;
	while(Y--)
	{
		Result*=X;
	}
	return Result;

}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
	uint8_t i;
	for(i=0;i<Length;i++)
	{
		
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
	
	}
}
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;

}

void Serial_printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);

}
uint8_t Serial_GetRxFlag()
{
	if(Serial_RxFlag==1)
	{
		Serial_RxFlag=0;
		return 1;
	}
		return 0;
}
uint8_t Serial_GetRxData()
{
	return Serial_RxData;
}
void USART1_IRQHandler()
{
	if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
	{
		
		Serial_RxData=USART_ReceiveData(USART1);
		Serial_RxFlag=1;
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	
	}

}

串口发送HEX数据包

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include "stdarg.h"

uint8_t Serial_RxFlag;
uint8_t Serial_RxData;
uint8_t Serial_TxPacket[4];
uint8_t Serial_RxPacket[4];

void Serial_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	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;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructrue;
	USART_InitStructrue.USART_BaudRate=9600;
	USART_InitStructrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  USART_InitStructrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStructrue.USART_Parity=USART_Parity_No;
	USART_InitStructrue.USART_StopBits=USART_StopBits_1;
	USART_InitStructrue.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructrue);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructrue;
	NVIC_InitStructrue.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructrue.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructrue.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructrue.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_InitStructrue);
	
	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)
{
	uint16_t i;
	for(i=0;i<Length;i++)
	{
		Serial_SendByte(Array[i]);
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i=0;String[i]!='\0';i++)
	{
		Serial_SendByte(String[i]);
	
	}
	
}
uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
	uint32_t Result;
	while(Y--)
	{
		Result*=X;
	}
	return Result;

}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
	uint8_t i;
	for(i=0;i<Length;i++)
	{
		
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
	
	}
}
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;

}

void Serial_printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);

}
void Serial_SendPacket()
{
	Serial_SendByte(0xFF);
	Serial_SendArray(Serial_TxPacket,4);
	Serial_SendByte(0xFE);

}
uint8_t Serial_GetRxFlag()
{
	if(Serial_RxFlag==1)
	{
		Serial_RxFlag=0;
		return 1;
	}
		return 0;
}

void USART1_IRQHandler()
{
		static uint8_t RxState=0;
		static uint8_t pRxPacket=0;

	if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
	{
		uint8_t RxData=USART_ReceiveData(USART1);
		if(RxState==0)
		{
			if(RxData==0xFF)
			{
				RxState=1;
				pRxPacket=0;
			}
		}
		else if(RxState==1)
		{
			Serial_RxPacket[pRxPacket]=RxData;
			pRxPacket++;
			if(pRxPacket>=4)
			{
				RxState=2;
			}
		}
		else if(RxState==2)
		{
			if(RxData==0xFE)
			{
				RxState=0;
				Serial_RxFlag=1;
			
			}
		
		}
		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	
	}

}

串口发送文本数据包

#include "stm32f10x.h"                  // Device header
#include "stdio.h"
#include "stdarg.h"

uint8_t Serial_RxFlag;
uint8_t Serial_RxData;
char Serial_RxPacket[100];

void Serial_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	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;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitTypeDef USART_InitStructrue;
	USART_InitStructrue.USART_BaudRate=9600;
	USART_InitStructrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
  USART_InitStructrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStructrue.USART_Parity=USART_Parity_No;
	USART_InitStructrue.USART_StopBits=USART_StopBits_1;
	USART_InitStructrue.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructrue);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructrue;
	NVIC_InitStructrue.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructrue.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructrue.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructrue.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_InitStructrue);
	
	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)
{
	uint16_t i;
	for(i=0;i<Length;i++)
	{
		Serial_SendByte(Array[i]);
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for(i=0;String[i]!='\0';i++)
	{
		Serial_SendByte(String[i]);
	
	}
	
}
uint32_t Serial_Pow(uint32_t X,uint32_t Y)
{
	uint32_t Result;
	while(Y--)
	{
		Result*=X;
	}
	return Result;

}
void Serial_SendNumber(uint32_t Number,uint8_t Length)
{
	uint8_t i;
	for(i=0;i<Length;i++)
	{
		
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+'0');
	
	}
}
int fputc(int ch,FILE *f)
{
	Serial_SendByte(ch);
	return ch;

}

void Serial_printf(char *format,...)
{
	char String[100];
	va_list arg;
	va_start(arg,format);
	vsprintf(String,format,arg);
	va_end(arg);
	Serial_SendString(String);

}


void USART1_IRQHandler()
{
	static uint8_t RxState=0;
	static uint8_t pRxPacket=0;
	if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)
	{
		uint8_t RxData=USART_ReceiveData(USART1);
		if(RxState==0)
		{
			if(RxData=='@'&&Serial_RxFlag==0)
			{
				RxState=1;
				pRxPacket=0;
			}
		}
		else if(RxState==1)
		{
			if(RxData=='\r')
			{
				RxState=2;
			}
			else
			{
			Serial_RxPacket[pRxPacket]=RxData;
			pRxPacket++;
			}
			
		}
		else if(RxState==2)
		{
			if(RxData=='\n')
			{
				RxState=0;
				Serial_RxFlag=1;
				Serial_RxPacket[pRxPacket]='\0';

			}
		
		}
		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	
	}

}