所长的分配内存就不好搞,我就分配一个数组,然后让一个指针指向队列,看这样好不?//文件.h
#ifdef FIFO_GLOBALS
#define FIFO_EXT
#else
#define FIFO_EXT extern
#endif
typedef struct QueueML{
unsigned char *pFirst;//数组的首地址
unsigned char PutFifoPt;//放数据的索引
unsigned char GetFifoPt;
unsigned char Size;//当前FIFO放了几个数进去
unsigned char MaxCount;//FIFO最大长度
unsigned char Wrap;
}QUEUE;
FIFO_EXT QUEUE TestQueue;
#ifndef __FIFO_H__
#define __FIFO_H__ 1 //有的编译器不接受#define __FIFO_H__????
#define FIFO_FULL 0
#define FIFO_EMPTY 0xFF
extern void FifoInit( QUEUE *pQueue, unsigned char *pArray, unsigned char MaxCount);
extern unsigned char PutFifo( QUEUE *pQueue, unsigned char Data);
extern unsigned char GetFifo( QUEUE *pQueue );
extern unsigned char GetFifoSize( QUEUE *pQueue );
extern void FifoClear( QUEUE *pQueue );
#endif
//.c文件
#include "Config.h"
/*
typedef struct QueueML{
unsigned char *pFirst;//
unsigned char PutFifoPt;//放数据的索引
unsigned char Size;//当前FIFO放了几个数进去
unsigned char MaxCount;//FIFO最大长度
unsigned char Wrap;
}QUEUE;
*/
/*
函数名:void FifoInit(struct Queue *pQueue, unsigned char *pArray)
入口参数:struct Queue *pQueue:
*/
void FifoInit( QUEUE *pQueue, unsigned char *pArray, unsigned char MaxCount)
{
pQueue->pFirst = pArray;//分配给队列数组的指针
pQueue->MaxCount = MaxCount;
pQueue->Wrap = MaxCount - 1;
pQueue->PutFifoPt = 0;
pQueue->GetFifoPt = 0;
pQueue->Size = 0;
}
void FifoClear( QUEUE *pQueue )
{
pQueue->PutFifoPt = 0;
pQueue->GetFifoPt = 0;
pQueue->Size = 0;
}
unsigned char PutFifo(QUEUE *pQueue, unsigned char Data)
{
if(pQueue->Size == pQueue->MaxCount) //FIFO FULL
return ~0;
else
{
*(pQueue->pFirst + pQueue->PutFifoPt) = Data;// (FIFO首地址 + 索引 ) = 数据
(pQueue->PutFifoPt)++;
pQueue->PutFifoPt &= pQueue->Wrap;
(pQueue->Size)++;
return 0;
/* *(RecBuf + PutFifoPt) = data; //put data into fifo
PutFifoPt++;
PutFifoPt = PutFifoPt & WRAP;// WRAP Point
RecCount++;*/
}
}
/*
功能:返回Fifo当前有多少空间被使用
*/
unsigned char GetFifoSize( QUEUE *pQueue )
{
return pQueue->Size;
}
/*
函数名:unsigned char GetFifo( QUEUE *pQueue )
说明:取FIFO要判断Fifo中有没有数据
*/
unsigned char GetFifo( QUEUE *pQueue )
{
unsigned char TempFifo;
TempFifo = *(pQueue->pFirst + pQueue->GetFifoPt);
(pQueue->GetFifoPt)++;
(pQueue->GetFifoPt) &= pQueue->Wrap;
(pQueue->Size)--;
return TempFifo;
}
|