新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)的FIFO(先入先出)循環(huán)隊(duì)列實(shí)現(xiàn)

單片機(jī)的FIFO(先入先出)循環(huán)隊(duì)列實(shí)現(xiàn)

作者: 時(shí)間:2016-11-28 來(lái)源:網(wǎng)絡(luò) 收藏
//////////////////////////////////////////////////////////
// 文件:config.h
//////////////////////////////////////////////////////////
#ifndef __CONFIG_H
#define __CONFIG_H
//這一段無(wú)需改動(dòng)
//This segmentshould not be modified
#ifndef TRUE
#define TRUE1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef unsigned charuint8;
typedef signedcharint8;
typedef unsigned short uint16;
typedef signedshort int16;
typedef unsigned intuint32;
typedef signedintint32;
typedef floatfp32;

#i nclude "FIFOQUEUE.h"
#endif
//////////////////////////////////////////////////////////
// 文件:FIFOQUEUE.h
//////////////////////////////////////////////////////////
#ifndef _FIFOQUEUE_H
#define _FIFOQUEUE_H
#define ElemTypeuint8
#define QueueSize20 //fifo隊(duì)列的大小
#define QueueFull0 //fifo滿置0
#define QueueEmpty1 //FIFO空置1
#define QueueOperateOk 2 //隊(duì)列操作完成 賦值為2
struct FifoQueue
{
uint16 front; //隊(duì)列頭
uint16 rear; //隊(duì)列尾
uint16 count; //隊(duì)列計(jì)數(shù)
ElemType dat[QueueSize];
};
//Queue Initalize
extern void QueueInit(struct FifoQueue *Queue);
// Queue In
extern uint8 QueueIn(struct FifoQueue *Queue,ElemType sdat);
// Queue Out
extern uint8 QueueOut(struct FifoQueue *Queue,ElemType *sdat);
#endif
//////////////////////////////////////////////////////////
// 文件:FIFOQUEUE.C
//////////////////////////////////////////////////////////
#i nclude "config.h"
//Queue Init
void QueueInit(struct FifoQueue *Queue)
{
Queue->front = Queue->rear;//初始化時(shí)隊(duì)列頭隊(duì)列首相連
Queue->count = 0; //隊(duì)列計(jì)數(shù)為0
}
// Queue In
uint8 QueueIn(struct FifoQueue *Queue,ElemType sdat) //數(shù)據(jù)進(jìn)入隊(duì)列
{
if((Queue->front == Queue->rear) && (Queue->count == QueueSize))
{// full //判斷如果隊(duì)列滿了
return QueueFull; //返回隊(duì)列滿的標(biāo)志
}else
{// in
Queue->dat[Queue->rear] = sdat;
Queue->rear = (Queue->rear + 1) % QueueSize;
Queue->count = Queue->count + 1;
return QueueOperateOk;
}
}
// Queue Out
uint8 QueueOut(struct FifoQueue *Queue,ElemType *sdat)
{
if((Queue->front == Queue->rear) && (Queue->count == 0))
{// empty
return QueueEmpty;
}else
{// out
*sdat = Queue->dat[Queue->front];
Queue->front = (Queue->front + 1) % QueueSize;
Queue->count = Queue->count - 1;
return QueueOperateOk;
}
}
//////////////////////////////////////////////////////////
// 文件:Main.C
//////////////////////////////////////////////////////////
#i nclude
#i nclude "config.h"
void main(void)
{
struct FifoQueue MyQueue;
ElemType sh;
uint8 i;
QueueInit(&MyQueue);
while(1)
{
for(i = 0;i < 30;i++)
{
if(QueueIn(&MyQueue,i) == QueueFull) break;
}
for(i = 0;i < 30;i++)
{
if(QueueOut(&MyQueue,&sh) == QueueEmpty) break;
}
}
while(1);
}

隊(duì)列是一種先進(jìn)先出(first infirst out,縮寫(xiě)為FIFO)的線性表。它只允許在標(biāo)的一端進(jìn)行插入,而在另一端刪除元素。這和我們?nèi)粘I钪械呐抨?duì)是一致的,最早進(jìn)入隊(duì)列的元素最早離開(kāi)。在隊(duì)列中,允許插入的一端叫做隊(duì)尾(rear),允許刪除的一端則稱為對(duì)頭(front)(排隊(duì)買(mǎi)票,窗口一端叫對(duì)頭,末尾進(jìn)隊(duì)叫隊(duì)尾)。


上一頁(yè) 1 2 3 下一頁(yè)

評(píng)論


技術(shù)專區(qū)

關(guān)閉