前言
在单片机开发过程中,如串口通讯中,大家往往会遇到数据传输有时候并发量很大,处理不过来导致数据丢失。实际如何确保数据不丢失?估计有点经验的朋友都会想到缓冲存储。对的,先将数据存储起来,再慢慢对数据做处理。
1 环形缓冲队列
环形缓冲队列,如下图,再初始化时队列头尾相等,表示没有数据缓冲,当接收到一个数据时,队列头加1,并把这个数据存在移动到的位置下,实现数据保存。当头尾队列不相等时,尾队列指针追头队列移动,这时就可以将数据提取出来。
1.1程序
1.11队列实现
#include "sy_fifo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//创建队列初始化
void sy_obj_fifo_create(obj_fifo_typ_t *pfifo)
{
pfifo->_head = 0;
pfifo->_tail = 0;
pfifo->cmd_pos = 0;
pfifo->cmd_state = 0;
}
//队列数据压栈
unsigned int sy_obj_fifo_push(obj_fifo_typ_t *pfifo, const unsigned char data)
{
unsigned int pos = 0;
pos = (pfifo->_head+1)%QUEUE_MAX_SIZE;
if (pfifo==NULL)
{
return 0;
}
if(pos!=pfifo->_tail) //非满状态
{
pfifo->_data[pfifo->_head] = data;
pfifo->_head = pos;
return 1;
}
return 0;
}
//数据占用大小
unsigned int sy_obj_fifo_get_occupy_size(obj_fifo_typ_t *pfifo)
{
if (pfifo==NULL) return 0;
return (((pfifo->_head+QUEUE_MAX_SIZE-pfifo->_tail)%QUEUE_MAX_SIZE)*CMD_MAX_SIZE);
}
//从队列中提取指定长度数据 ,这里只是提示一个例子,具体根据自己实际项目实现
unsigned int sy_obj_fifo_read(obj_fifo_typ_t *pfifo, unsigned char *buf,const unsigned int size)
{
unsigned int i= 0;
unsigned int data_len = 0;
if ((size==0) || (pfifo==NULL) || (buf==NULL))
{
return 0;
}
if(sy_obj_fifo_get_occupy_size(pfifo)<size) return 0; //数据长度不足,退出
for(i= 0;i<size;i++){
if( pfifo->_tail!= pfifo->_head) //非空状态
{
*buf = pfifo->_data[ pfifo->_tail];
buf++;
pfifo->_tail = (pfifo->_tail+1)%QUEUE_MAX_SIZE;
data_len++;
}
}
return data_len;
}
1.12 头文件实现
#ifndef __SY_FIFO_H
#define __SY_FIFO_H
#define QUEUE_MAX_SIZE 1000
typedef struct
{
unsigned int cmd_state;
unsigned int cmd_pos;
unsigned int _head; //队列头
unsigned int _tail; //队列尾
unsigned char _data[QUEUE_MAX_SIZE];
}obj_fifo_typ_t;
void sy_obj_fifo_create(obj_fifo_typ_t *pfifo);
unsigned int sy_obj_fifo_push(obj_fifo_typ_t *pfifo, const unsigned char data);
unsigned int sy_obj_fifo_read(obj_fifo_typ_t *pfifo, unsigned char *buf,const unsigned int size);
unsigned int sy_obj_fifo_get_occupy_size(obj_fifo_typ_t *pfifo);
#endif
1.13 demo
.......................................
obj_fifo_typ_t obj_usart_buff; //定义一个实例
void main(void){
unsigned int data_size = 0;
unsigned char buff[20] = {0};
..............//user handle
sy_obj_fifo_create(&obj_usart_buff); //创建一个环形队列实例
while(1){
data_size = sy_obj_fifo_read(&obj_usart_buff,buff,10);//取10个数据
if(data_size == 10)
// user handle
}
}
//串口为例将串口接收到的数据压入队列中
void UART0_IRQHandler(void)
{
unsigned char data = 0;
if (UART_GetITIdentity(UART0) == UART_IT_RX)
{
data = UART_ReceiveData(UART0);
sy_obj_fifo_push(&obj_usart_buff, data); //将数据存到队列中
}
else if (UART_GetITIdentity(UART0) == UART_IT_TX)
{
UART_ClearTxITPendingBit(UART0);
}
|