目录
1.介绍
本文主要使用串口接收中断加空闲中断的方式接收不定长数据,主要思路是在中断服务函数里面判断是发生了接收中断还是空闲中断,如果是接收中断就接收一个字节数据到接收数组中,如果是空闲中断就将接收到的所有数据发送出去,并清空接收数组。
源码地址在文章最后。
2.代码
代码主要都在usart.c、stm32f10x_it.c和main.c这三个文件中,具体内容如下所示:
2.1 usart.c
#include "usart.h"
#include "string.h"
#include "stdarg.h"
#define SIZE 512
uint8_t SendBuffer[SIZE];//定义发送数组;
void NVIC_Configuration(void)//配置中断函数;
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void)//串口初始化函数;
{
USART_DeInit(USART1);
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
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_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//波特率115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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);
NVIC_Configuration();
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//设置RXNE中断;
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//设置IDLE中断;
USART_ClearFlag(USART1, USART_FLAG_IDLE | USART_FLAG_RXNE);//清除状态标志位;
USART_Cmd(USART1, ENABLE);
}
void Usart_SendBuffer(uint8_t * sendbuffer)//发送数组函数;
{
uint16_t len = strlen((char *)sendbuffer);
uint16_t i = 0;
while(i < len)
{
USART_SendData(USART1, (uint8_t) sendbuffer[i]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
i++;
}
}
void Usart_Printf(const char * format, ...)//发送数据函数;
{
va_list args;
va_start(args, format);
vsnprintf((char *)SendBuffer,SIZE,format,args);
va_end(args);
Usart_SendBuffer(SendBuffer);
memset(SendBuffer, 0, SIZE);
}
2.2 stm32f10x_it.c
#include "stm32f10x_it.h"
#include "usart.h"
#include "string.h"
#define BUFFER_SIZE 256
uint8_t ReceiveBuffer[BUFFER_SIZE];//定义接收数组;
void USART1_IRQHandler(void)//串口中断服务函数;
{
static uint8_t buffercnt = 0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//判断是否发生了接收中断;
{
ReceiveBuffer[buffercnt] = USART_ReceiveData(USART1);//将接收到一个字节数据写入ReceiveBuffer数组中;
buffercnt ++;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清除接收中断;
}
else if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//判断是否发生了空闲中断;
{
Usart_Printf("%s\n", ReceiveBuffer);//将接收到的数据发送出去;
memset(ReceiveBuffer, 0, BUFFER_SIZE);//将接收数组ReceiveBuffer清除;
buffercnt = 0;
USART_ReceiveData(USART1);//清除空闲中断标志位;
USART_ClearITPendingBit(USART1, USART_IT_IDLE);
}
}
2.3 main.c
#include "stm32f10x.h"
#include "usart.h"
int main(void)
{
USART_Config();//串口初始化;
while(1)
{
}
}
3.效果演示
将程序下载到stm32单片机中,开发板使用的正点原子的stm32mini板,其他开发板需要适当修改代码,效果如下:
4.源码地址
百度云盘地址:https://pan.baidu.com/s/1DXu2rSQEjLUdPJkJrwEY1Q?pwd=aclk 提取码: aclk