单片机实用库之使用宏定义来实现循环队列
单片机模块化编程之循环队列
队列是常用的数据结构之一,根据百度百科的说明,队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。在我们的实际生产应用中,串口解析数据是最常用这种数据结构的。
今天在面包板社区看到一篇文件,是使用宏定义来实现循环队列 ,觉得比较好用,特地实操分享下。
1:开发环境
- 电脑系统Windows7
- Keil编译器
- STM32F103开发板
2:模块下载地址
https://github.com/barraq/BRBrain/blob/master/firmware/CBUF.h
具体内容如下:
/****************************************************************************
*
* Since this code originated from code which is public domain, I
* hereby declare this code to be public domain as well.
*
****************************************************************************/
/**
*
* @file CBUF.h
*
* @brief This file contains global definitions for circular buffer
* manipulation.
*
* These macros implement a circular buffer which employs get and put
* pointers, in such a way that mutual exclusion is not required
* (assumes one reader & one writer).
*
* It requires that the circular buffer size be a power of two, and the
* size of the buffer needs to smaller than the index. So an 8 bit index
* supports a circular buffer upto ( 1 << 7 ) = 128 entries, and a 16 bit index
* supports a circular buffer upto ( 1 << 15 ) = 32768 entries.
*
* The basis for these routines came from an article in Jack Ganssle's
* Embedded Muse: http://www.ganssle.com/tem/tem110.pdf
*
* In order to offer the most amount of flexibility for embedded environments
* you need to define a macro for the size.
*
* First, you need to name your circular buffer. For this example, we'll
* call it myQ.
*
* The size macro that needs to be defined will be the name of the
* circular buffer followed by _SIZE. The size must be a power of two
* and it needs to fit in the get/put indicies. i.e. if you use an
* 8 bit index, then the maximum supported size would be 128.
*
* The structure which defines the circular buffer needs to have 3 members
* m_getIdx, m_putIdx, and m_entry.
*
* m_getIdx and m_putIdx need to be unsigned integers of the same size.
*
* m_entry needs to be an array of xxx_SIZE entries, or a pointer to an
* array of xxx_SIZE entries. The type of each entry is entirely up to the
* caller.
*
* #define myQ_SIZE 64
*
* volatile struct
* {
* uint8_t m_getIdx;
* uint8_t m_putIdx;
* uint8_t m_entry[ myQ_SIZE ];
*
* } myQ;
*
* You could then use
*
* CBUF_Push( myQ, 'x' );
*
* to add a character to the circular buffer, or
*
* ch = CBUF_Pop( myQ );
*
* to retrieve an element from the buffer.
*
* If you happen to prefer to use C++ instead, there is a templatized
* version which requires no macros. You just declare 3 template parameters:
*
* - The type that should be used for the index
* - The size of the circular buffer
* - The type that should be used for the entry
*
* For example:
*
* CBUF< uint8_t, 64, char > myQ;
*
****************************************************************************/
#if !defined( CBUF_H )
#define CBUF_H /**< Include Guard */
/* ---- Include Files ---------------------------------------------------- */
/* ---- Constants and Types ---------------------------------------------- */
/**
* Initializes the circular buffer for use.
*/
#define CBUF_Init( cbuf ) cbuf.m_getIdx = cbuf.m_putIdx = 0
/**
* Returns the number of elements which are currently contained in the
* circular buffer.
*/
#define CBUF_Len( cbuf ) ((typeof( cbuf.m_putIdx ))(( cbuf.m_putIdx ) - ( cbuf.m_getIdx )))
/**
* Appends an element to the end of the circular buffer
*/
#define CBUF_Push( cbuf, elem ) (cbuf.m_entry)[ cbuf.m_putIdx++ & (( cbuf##_SIZE ) - 1 )] = (elem)
/**
* Retrieves an element from the beginning of the circular buffer
*/
#define CBUF_Pop( cbuf ) (cbuf.m_entry)[ cbuf.m_getIdx++ & (( cbuf##_SIZE ) - 1 )]
/**
* Retrieves the i'th element from the beginning of the circular buffer
*/
#define CBUF_Get( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_getIdx + idx ) & (( cbuf##_SIZE ) - 1 )]
/**
* Retrieves the i'th element from the end of the circular buffer
*/
#define CBUF_GetEnd( cbuf, idx ) (cbuf.m_entry)[( cbuf.m_putIdx - idx - 1 ) & (( cbuf##_SIZE ) - 1 )]
/**
* Determines if the circular buffer is empty
*/
#define CBUF_IsEmpty( cbuf ) ( CBUF_Len( cbuf ) == 0 )
/**
* Determines if the circular buffer is full.
*/
#define CBUF_IsFull( cbuf ) ( CBUF_Len( cbuf ) == ( cbuf##_SIZE ))
/**
* Determines if the circular buffer is currenly overflowed or underflowed.
*/
#define CBUF_Error( cbuf ) ( CBUF_Len( cbuf ) > cbuf##_SIZE )
#if defined( __cplusplus )
template < class IndexType, unsigned Size, class EntryType >
class CBUF
{
public:
CBUF()
{
m_getIdx = m_putIdx = 0;
}
IndexType Len() const { return m_putIdx - m_getIdx; }
bool IsEmpty() const { return Len() == 0; }
bool IsFull() const { return Len() == Size; }
bool Error() const { return Len() > Size; }
void Push( EntryType val )
{
m_entry[ m_putIdx++ & ( Size - 1 )] = val;
}
EntryType Pop()
{
return m_entry[ m_getIdx++ & ( Size - 1 )];
}
private:
volatile IndexType m_getIdx;
volatile IndexType m_putIdx;
EntryType m_entry[ Size ];
};
#endif // __cplusplus
/* ---- Variable Externs ------------------------------------------------- */
/* ---- Function Prototypes ---------------------------------------------- */
/** @} */
#endif // CBUF_H
3:程序移植
3.1、新建STM32工程
首先,打开keil工程,新建工程,平时大家就用自己手上的工程来移植就好了。工程目录如下:
3.2、 添加文件,串口打印输出
新建串口驱动文件,这里使用STM32的串口1来实现串口通讯,串口打印输出的资料有很多,这里也不做多讲,如果能在串口打印输出结果,说明串口工作正常。下面是每隔10秒打印一次数据。
3.3、移植CBUF.h宏定义文件
直接在文件中引用头文件就好
3.4、实现与应用
配置好工作之后,按照cbuf.h文件里面的说明,需要定义一个结构体对象,进行初始化。
具体代码如下:
#define myQ_SIZE 64
typedef volatile struct
{
uint8_t m_getIdx;
uint8_t m_putIdx;
uint8_t m_entry[ myQ_SIZE ];
}myQ;
static myQ volatile RxBuffer;
static myQ volatile TxBuffer;
#define RxBuffer_SIZE myQ_SIZE
定义对象之后,调用初始化函数 ,主要是定义初始化缓冲区,下面定义一个接收和发送的缓冲区。
void UART1_Buffer_Init(void)
{
CBUF_Init(RxBuffer);
CBUF_Init(TxBuffer);
}
在实际应用中,当串口接收数据之后,有很多种方式处理数据,包括DMA接收数据,串口中断接收等等,本来就直接串口中断处理数据,串口中断函数如下
void USART1_IRQHandler(void)
{
unsigned char dat;
if(USART_GetITStatus(USART1,USART_IT_RXNE) != RESET)
{
dat = USART_ReceiveData(USART1);
/*直接调用宏定义,将接收到的数据插入队列中*/
CBUF_Push(RxBuffer,dat);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
到这里,就已经完成数据接收过程。接收数据完成之后,我每一秒读出一个数据并通过串口发送出来,如果没有数据,就不发送。编写一个串口处理数据的任务。
void UART1_Recv_Task(void)
{
unsigned char TxChar=0;
if(CBUF_Len(RxBuffer)!=0)
{
TxChar=CBUF_Pop(RxBuffer);
printf("%c\r\n",TxChar);
}
}
代码的意思主要是每次读取一个数据并打印,我把这个任务每秒执行一次,
最后,通过串口调试工具,发送12345678这8个数据,串口每一秒读取一个,8秒后读完。
在实际应用中,我们基本上一次读取全部数据,然后去解析,一般都是自定义协议的时候用,来读取有效数据。
- 点赞
- 收藏
- 关注作者
评论(0)