正点原子的串口中断函数如下:
void USART1_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
#if SYSTEM_SUPPORT_OS //如果SYSTEM_SUPPORT_OS为真,则需要支持OS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
else USART_RX_STA|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
}
此函数只是单纯的接收函数 ,没有对数据进行处理 ,由于本人做了一个项目需要对数据进行大量的处理
以前都是串口接收函数存取,然后对接收函数进行判断 处理
下面的方法是在中断处理函数中对数据进行处理。
/*使用microLib的方法*/
/*
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART1->SR & USART_FLAG_RXNE));
return ((int)(USART1->DR & 0x1FF));
}
*/
//#if EN_USART1_RX //如果使能了接收
//#if EN_USART1_RX //如果使能了接收
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误
//u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART_RX_STA=0; //接收状态标记
//串口1队列定义
u8 UART1SendBuff[UART1BuffSize]; //发送数据
u8 UART1ReceBuff[UART1BuffSize]; //接收数据?
u16 UART1ReceIn = 0;//接收状态标记数据位
u8 UART1ReceFullFlag = 0;
//串口2队列定义
u8 UART2SendBuff[UART2BuffSize];
u8 UART2ReceBuff[UART2BuffSize];
u16 UART2ReceIn = 0;
u8 UART2ReceFullFlag = 0;
//串口3队列定义
u8 UART3SendBuff[UART3BuffSize];
u8 UART3ReceBuff[UART3BuffSize];
u16 UART3ReceIn = 0;
u8 UART3ReceFullFlag = 0;
//串口1初始化
void USART1_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//开启GPIOA和USART1时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
//串口2初始化
void USART2_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA和USART1时钟
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.2
//USART2_RX GPIOA.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.3
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口2
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART2, ENABLE); //使能串口2
}
//串口3初始化
void USART3_Configuration(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOA和USART1时钟
//USART3_TX GPIOB.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB.10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOA.10
//USART3_RX GPIOB.3初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.11
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART3, &USART_InitStructure); //初始化串口3
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE); //开启串口接收中断
USART_Cmd(USART3, ENABLE); //使能串口3
}
//串口1发送一帧数据
void USART1_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART1, SendOneData);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
}
//串口2发送一帧数据
void USART2_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART2, SendOneData);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{}
}
//串口3发送一帧数据
void USART3_SendOneData(uint8_t SendOneData)
{
USART_SendData(USART3, SendOneData);
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
{}
}
//串口1发送一列数据
void USART1_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
for(i=0;i
USART1_SendOneData(*Buffer++);
}
}
//串口2发送一列数据
void USART2_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
for(i=0;i
USART2_SendOneData(*Buffer++);
}
}
//串口3发送一列数据
void USART3_SendUnfixedData(uint8_t *Buffer, uint8_t Length)
{
uint8_t i;
LED_ON;
for(i=0;i
重点讲解串口中断的服务函数
//串口1中断服务函数
void USART1_IRQHandler(void)
{
u8 Res;//数据暂存
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
Res =USART_ReceiveData(USART1); //读取接收到的数据
switch(UART1ReceIn)//读取接收到的数据有几位 每一位对应的数据协议校验
{
case 0:
if(Res=='T')
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 1:
if(Res=='M')
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 2:
if(Res==0x0f)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 3:
if(Res==0x01)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
case 4:
if(Res==0x31)
UART1ReceBuff[UART1ReceIn++] = Res;
else
UART1ReceIn = 0;
break;
default:
UART1ReceBuff[UART1ReceIn++] = Res;
break;
}
if(UART1ReceIn >= 57)
{
UART1ReceFullFlag = 1;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除相对应的中断位
}
else if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // 发送中断
{
USART_ClearITPendingBit(USART1, USART_IT_TXE); // clear interrupt
}
}