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