cpp
//Serial.c
#include "stm32f10x.h" // Device header
#include "stdio.h"
#include "stdarg.h"
uint8_t Serial_RxPacket[4];
uint8_t Serial_TxPacket[4];
uint8_t Serial_Rxflag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_PriorityGroupConfig(NVIC_PriorityGroup_2);
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);
USART_Cmd(USART1,ENABLE);
}
void Serial_SendByte(uint8_t Byte)
{
USART_SendData(USART1,Byte);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//Transmit Data Register Empty",即发送数据寄存器空标志
}
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=1;
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(void)
{
Serial_SendByte(0XFF);
Serial_SendArray(Serial_TxPacket,4);
Serial_SendByte(0XFE);
}
uint8_t Serial_GetFlag(void)
{
if(Serial_Rxflag==1)
{
Serial_Rxflag=0;
return 1;
}
return 0;
}
void USART1_IRQHandler(void)
{
static uint8_t RxState =0;
static uint8_t pRxPacket=0;
if(USART_GetFlagStatus(USART1,USART_FLAG_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);
}
}
cpp
//main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "key.h"
uint8_t KeyNum;
int main(void)
{
Key_Init();
OLED_Init();
Serial_Init();
OLED_ShowString(1,1,"TxPacket");
OLED_ShowString(3,1,"RxPacket");
Serial_TxPacket[0]=0X01;
Serial_TxPacket[1]=0X02;
Serial_TxPacket[2]=0X03;
Serial_TxPacket[3]=0X04;
while(1)
{
KeyNum=Key_GetNum();
if(KeyNum==1)
{
Serial_TxPacket[0]++;
Serial_TxPacket[1]++;
Serial_TxPacket[2]++;
Serial_TxPacket[3]++;
Serial_SendPacket();
OLED_ShowHexNum(2,1,Serial_TxPacket[0],2);
OLED_ShowHexNum(2,4,Serial_TxPacket[1],2);
OLED_ShowHexNum(2,7,Serial_TxPacket[2],2);
OLED_ShowHexNum(2,10,Serial_TxPacket[3],2);
}
if(Serial_GetFlag()==1)
{
OLED_ShowHexNum(4,1,Serial_RxPacket[0],2);
OLED_ShowHexNum(4,4,Serial_RxPacket[1],2);
OLED_ShowHexNum(4,7,Serial_RxPacket[2],2);
OLED_ShowHexNum(4,10,Serial_RxPacket[3],2);
}
}
}

1.本程序实现了多字节数据发送和接收,在接收时采用三状态的状态机转换实现连贯的多字节接收
2.void Serial_SendPacket(void)
{
Serial_SendByte(0XFF);
Serial_SendArray(Serial_TxPacket,4);
Serial_SendByte(0XFE);
}
在发送时通过帧头0XFF和帧尾0XFE把数据包裹起来发送。
void USART1_IRQHandler(void)
{
static uint8_t RxState =0;
static uint8_t pRxPacket=0;
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
{
uint8_t RxData=USART_ReceiveData(USART1);
if (RxState==0)
{
if(RxData==0xFF)
{
RxState=1;
pRxPacket=0;
}
}
else if (RxState==1)
{
Serial_RxPacketpRxPacket=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);
}
}
接收时通过中断响应接收,一个字节触发一次中断
RxState:状态机的状态,有0,1,2
0状态:等待帧头,
1状态:接收数据
2状态:等待帧尾
pRxPacket:计数器,状态1时计数,到4之后转到状态2.
RXNE:(R eceive X Data Register N ot E mpty),接收数据寄存器非空
整个中断程序我完整的讲一遍:
第一步,从电脑端的串口发送数据,发送到USART串口,进入STM32
第二步,当发现STM32的USART-DR数据寄存器有数之后,就进入状态机,把接收到的数据给RxData,
注意 :一次中断只能传一个字节的数,比如你发送FF 01 02 03 04 FE,那么一次中断就只能处理一个FF,下一次处理01,以此类推。
第三步:由于初始设置 static uint8_t RxState =0;,所以会进入状态0,开始等待FF,如果FF一直不来,程序就会卡住在这,比如你发送01 02 07 FF,FF之前的数都不会被发送。
第四步:遇到FF之后进入状态1,开始把后面发送的数存到 Serial_RxPacket数组,注意一次中断只能处理一个字节的数,每来一个新的字节又会触发新中断,而不是一次中断处理了4个字节。
第五步:4个字节的数装完之后进入状态2,等待FE,如果FE没来,后面发送的数据也不会被接收,FE来了之后 ,回到状态0,并且Serial_Rxflag置1,意思是已经收到完整数据包,快来处理我吧。
- USART_ClearITPendingBit(USART1,USART_IT_RXNE);
手动清除中断挂起标志位,这里不写这行也行,因为USART_ReceiveData(USART1)会自动清除中断挂起标志位,这里写是给提个醒。