FIFO为空时,不能执行读操作
FIFO为满时,不能执行写操作
rd < wr
【资料图】
rd > wr
/**Copyright(C),2010-2023,CSDN@whik1194*Time:2023年4月9日*Author:https://blog.csdn.net/whik1194*GitHub:https://gitee.com/whik/xqueue*/#ifndef__XQUEUE_H__#define__XQUEUE_H__#include"stdint.h"/*FIFO数据的类型,可以是结构体类型*/#defineqdata_tuint8_t/*FIFO长度,实际存放的数据=FIFO_SIZE-1*/#defineFIFO_SIZE6typedefenum{QUEUE_OK,QUEUE_FULL,QUEUE_EMPTY}qstatus_t;typedefstruct{uint16_taddr_wr;/*写地址*/uint16_taddr_rd;/*读地址*/uint16_tlength;/*FIFO长度,实际存放的数据=length-1*/qdata_tfifo[FIFO_SIZE];}queue_t;qstatus_tqueue_reset(queue_t*q);qstatus_tqueue_read(queue_t*q,qdata_t*pdata);qstatus_tqueue_write(queue_t*q,qdata_tdata);intqueue_isFull(queue_t*q);intqueue_isEmpty(queue_t*q);intqueue_print(queue_t*q);#endif
xqueue.c文件
/**Copyright(C),2010-2023,CSDN@whik1194*Time:2023年4月9日*Author:https://blog.csdn.net/whik1194*GitHub:https://gitee.com/whik/xqueue*/#include"xqueue.h"#include"stdio.h"/*FIFO复位*/qstatus_tqueue_reset(queue_t*q){inti=0;q->addr_wr=0;q->addr_rd=0;q->length=FIFO_SIZE;for(i=0;i
length;i++)q->fifo[i]=0;returnQUEUE_OK;}/*FIFO写入数据*/qstatus_tqueue_write(queue_t*q,qdata_tdata){if(queue_isFull(q)){printf("Writefailed(%d),queueisfull\n",data);returnQUEUE_FULL;}q->fifo[q->addr_wr]=data;q->addr_wr=(q->addr_wr+1)%q->length;printf("writesuccess:%02d\n",data);queue_print(q);returnQUEUE_OK;}/*FIFO读出数据*/qstatus_tqueue_read(queue_t*q,qdata_t*pdata){if(queue_isEmpty(q)){printf("Readfailed,queueisempty\n");returnQUEUE_EMPTY;}*pdata=q->fifo[q->addr_rd];q->addr_rd=(q->addr_rd+1)%q->length;printf("readsuccess:%02d\n",*pdata);queue_print(q);returnQUEUE_OK;}/*FIFO是否为空*/intqueue_isEmpty(queue_t*q){return(q->addr_wr==q->addr_rd);}/*FIFO是否为满*/intqueue_isFull(queue_t*q){return((q->addr_wr+1)%q->length==q->addr_rd);}/*FIFO内数据的个数*/intqueue_count(queue_t*q){if(q->addr_rd<=q->addr_wr)return(q->addr_wr-q->addr_rd);//addr_rd>addr_wr;return(q->length+q->addr_wr-q->addr_rd);}/*打印当前FIFO内的数据和读写指针的位置*/intqueue_print(queue_t*q){inti=0;intj=0;for(i=0;i addr_rd;i++)printf("");printf("rd=%d",q->addr_rd);printf("\n");for(i=0;i length;i++){if(q->addr_wr>q->addr_rd){if(i>=q->addr_rd&&i addr_wr)printf("[%02d]",q->fifo[i]);elseprintf("[]");}else//addr_rd>addr_wr{if(i addr_wr||i>=q->addr_rd)printf("[%02d]",q->fifo[i]);elseprintf("[]");}}printf("------count=%d\n",queue_count(q));for(i=0;i addr_wr;i++)printf("");printf("wr=%d",q->addr_wr);printf("\n");returnQUEUE_OK;}
实际应用:
/**Copyright(C),2010-2023,CSDN@whik1194*Time:2023年4月9日*Author:https://blog.csdn.net/whik1194*GitHub:https://github.com/whik/xqueue*/#include
#include #include"xqueue.h"intmain(intargc,char*argv[]){queue_tqueue;qdata_tdata;queue_reset(&queue);queue_write(&queue,1);queue_write(&queue,2);queue_write(&queue,3);queue_read(&queue,&data);queue_read(&queue,&data);queue_write(&queue,4);queue_write(&queue,5);queue_write(&queue,6);queue_write(&queue,7);queue_read(&queue,&data);queue_read(&queue,&data);queue_read(&queue,&data);queue_write(&queue,8);queue_write(&queue,9);queue_write(&queue,10);queue_read(&queue,&data);system("pause");return0;}
运行结果:
https://gitee.com/whik/xqueue