前言
在單片機開發(fā)過程中,如串口通訊中,大家往往會遇到數(shù)據(jù)傳輸有時候并發(fā)量很大,處理不過來導(dǎo)致數(shù)據(jù)丟失。實際如何確保數(shù)據(jù)不丟失?估計有點經(jīng)驗的朋友都會想到緩沖存儲。對的,先將數(shù)據(jù)存儲起來,再慢慢對數(shù)據(jù)做處理。
1 環(huán)形緩沖隊列
環(huán)形緩沖隊列,如下圖,再初始化時隊列頭尾相等,表示沒有數(shù)據(jù)緩沖,當接收到一個數(shù)據(jù)時,隊列頭加1,并把這個數(shù)據(jù)存在移動到的位置下,實現(xiàn)數(shù)據(jù)保存。當頭尾隊列不相等時,尾隊列指針追頭隊列移動,這時就可以將數(shù)據(jù)提取出來。
1.1程序
1.11隊列實現(xiàn)
#include "sy_fifo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//創(chuàng)建隊列初始化
void sy_obj_fifo_create(obj_fifo_typ_t *pfifo)
{
pfifo->_head = 0;
pfifo->_tail = 0;
pfifo->cmd_pos = 0;
pfifo->cmd_state = 0;
}
//隊列數(shù)據(jù)壓棧
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) //非滿狀態(tài)
{
pfifo->_data[pfifo->_head] = data;
pfifo->_head = pos;
return 1;
}
return 0;
}
//數(shù)據(jù)占用大小
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);
}
//從隊列中提取指定長度數(shù)據(jù) ,這里只是提示一個例子,具體根據(jù)自己實際項目實現(xiàn)
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; //數(shù)據(jù)長度不足,退出
for(i= 0;i<size;i++){
if( pfifo->_tail!= pfifo->_head) //非空狀態(tài)
{
*buf = pfifo->_data[ pfifo->_tail];
buf++;
pfifo->_tail = (pfifo->_tail+1)%QUEUE_MAX_SIZE;
data_len++;
}
}
return data_len;
}
1.12 頭文件實現(xiàn)
#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); //創(chuàng)建一個環(huán)形隊列實例
while(1){
data_size = sy_obj_fifo_read(&obj_usart_buff,buff,10);//取10個數(shù)據(jù)
if(data_size == 10)
// user handle
}
}
//串口為例將串口接收到的數(shù)據(jù)壓入隊列中
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); //將數(shù)據(jù)存到隊列中
}
else if (UART_GetITIdentity(UART0) == UART_IT_TX)
{
UART_ClearTxITPendingBit(UART0);
}
|