跳转到主要内容

单片机裸机实现队列功能的方案

cathy 提交于

<strong><font color="#004a85">QueueForMcu</font> </strong>

基于单片机实现的队列功能模块,主要用于8位、16位、32位非运行RTOS的单片机应用,兼容大多数单片机平台。

开源代码:https://github.com/xiaoxinpro/QueueForMcu

<strong><font color="#004a85">一、特性</font> </strong>

<ul data-tool="mdnice编辑器">
<li>动态创建队列对象</li>
<li>动态设置队列数据缓冲区</li>
<li>静态指定队列元素数据长度</li>
<li>采用值传递的方式保存队列数据</li>
</ul>

<strong><font color="#004a85">二、快速使用</font> </strong>

<pre style="overflow-x:auto; background-color:#e9e9e9;">#include "queue.h"

#define Q_UART_BUFFER_SIZE 1024

QUEUE_HandleTypeDef qUartTx;
QUEUE_DATA_T BufferUartTx[Q_UART_BUFFER_SIZE];

int main(void)
{
QUEUE_DATA_T temp;

//初始化队列
Queue_Init(&qUartTx, BufferUartTx, Q_UART_BUFFER_SIZE);

while(1)
{
//入队
Queue_Push(&qUartTx, 'Q');
Queue_Push(&qUartTx, 'u');
Queue_Push(&qUartTx, 'e');
Queue_Push(&qUartTx, 'u');
Queue_Push(&qUartTx, 'e');

//出队
Queue_Pop(&qUartTx, &temp);
Queue_Pop(&qUartTx, &temp);
Queue_Pop(&qUartTx, &temp);
Queue_Pop(&qUartTx, &temp);
Queue_Pop(&qUartTx, &temp);
}
}</pre>

<strong><font color="#004a85">三、配置说明</font> </strong>

目前QueueForMcu只有一个静态配置项,具体如下:

在文件 queue.h 中有一个宏定义 QUEUE_DATA_T 用于指定队列元素的数据长度,默认是 unsigned char ,可以根据需要更改为其他数据类型。

<strong><font color="#004a85">四、数据结构</font> </strong>

队列的数据结构为 QUEUE_HandleTypeDef 用于保存队列的状态,源码如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">typedef struct QUEUE_HandleTypeDef{
unsigned int head; //队列头指针
unsigned int tail; //队列尾指针
unsigned int buffer_length; //队列缓存长度(初始化时赋值)
QUEUE_DATA_T * buffer; //队列缓存数组(初始化时赋值)
}QUEUE_HandleTypeDef;</pre>

其中 QUEUE_DATA_T 为配置项中自定义的数据类型。

<strong><font color="#004a85">五、创建队列</font> </strong>

<strong>1、创建队列缓存</strong>

由于我们采用值传递的方式保存队列数据,因此我们在创建队列前要手动创建一个队列缓存区,用于存放队列数据。

<pre style="overflow-x:auto; background-color:#e9e9e9;">QUEUE_DATA_T BufferUartTx[1024];</pre>

以上代码即创建一个大小为 1024 的队列缓存区。

<strong>2、创建队列结构</strong>

接下来使用 QUEUE_HandleTypeDef 创建队列结构,用于保存队列的状态:

<pre style="overflow-x:auto; background-color:#e9e9e9;">QUEUE_HandleTypeDef qUartTx;</pre>

<strong>3、初始化队列</strong>

准备好队列缓存和队列结构后调用 Queue_Init 函数来创建队列,该函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">void Queue_Init(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * buffer, unsigned int len)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要初始化的队列结构,如果二次初始化将清空原队列的内容。</td>
</tr>
<tr>
<td>buffer</td>
<td>队列缓存的首地址指针</td>
</tr>
<tr>
<td>len</td>
<td>队列长度,不能比队列缓存长度还要大。</td>
</tr>
</tbody>
</table>

参考代码:

<pre style="overflow-x:auto; background-color:#e9e9e9;">Queue_Init(&qUartTx, BufferUartTx, Q_UART_BUFFER_SIZE);</pre>

<strong><font color="#004a85">六、压入队列</font> </strong>

<strong>1、单数据压入</strong>

将数据压入队列尾部使用 Queue_Push 函数,该函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">QUEUE_StatusTypeDef Queue_Push(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T data)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要压入数据的队列结构。</td>
</tr>
<tr>
<td>data</td>
<td>待压入队列的数据。</td>
</tr>
</tbody>
</table>

返回值说明:

该函数会返回一个 QUEUE_StatusTypeDef 枚举数据类型,返回值会根据队列状态返回以下几个值:

<table border="1" align="center">
<thead>
<tr>
<th>返回值</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>QUEUE_OK</td>
<td>数据压入队列成功。</td>
</tr>
<tr>
<td>QUEUE_OVERLOAD</td>
<td>未压入数据到队列中,原因队列已满。</td>
</tr>
</tbody>
</table>

参考代码:

<pre style="overflow-x:auto; background-color:#e9e9e9;">Queue_Push(&qUartTx, 'Q');
Queue_Push(&qUartTx, 0x51);
Queue_Push(&qUartTx, 81);</pre>

<strong>2、多数据压入</strong>

若需要将多个数据(数组)压入队列可以使用 Queue_Push_Array 函数,原理上循环调用 Queue_Push 函数来实现的,函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">unsigned int Queue_Push_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要压入数据的队列结构。</td>
</tr>
<tr>
<td>pdatas</td>
<td>待压入队列的数组首地址。</td>
</tr>
<tr>
<td>len</td>
<td>待压入队列的数组长度。</td>
</tr>
</tbody>
</table>

当数组长度大于队列剩余长度时,数组多余的数据将被忽略。

返回值说明:

<ul data-tool="mdnice编辑器">
<li>
<p>该函数将返回实际被压入到队列中的数据长度。</p>
</li>
<li>
<p>当队列中的剩余长度富余时,返回值将等于参数 len 的值。</p>
</li>
<li>
<p>当队列中的剩余长度不足时,返回值为实际被压入到队列的数据长度。</p>
</li>
</ul>

<strong><font color="#004a85">七、弹出队列</font> </strong>

<strong>1、单数据弹出</strong>

将队列头部数据弹出队列使用 Queue_Pop 函数,需要注意的是,弹出的数据将从队列中删除,该函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">QUEUE_StatusTypeDef Queue_Pop(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdata)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要弹出数据的队列结构。</td>
</tr>
<tr>
<td>pdata</td>
<td>用于保存弹出数据变量的指针。</td>
</tr>
</tbody>
</table>

返回值说明:

该函数会返回一个 QUEUE_StatusTypeDef 枚举数据类型,返回值会根据队列状态返回以下几个值:

<table border="1" align="center">
<thead>
<tr>
<th>返回值</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>QUEUE_OK</td>
<td>数据弹出队列成功。</td>
</tr>
<tr>
<td>QUEUE_VOID</td>
<td>未弹出数据到队列中,原因队列为空。</td>
</tr>
</tbody>
</table>

参考代码:

<pre style="overflow-x:auto; background-color:#e9e9e9;">QUEUE_DATA_T temp;
if(QUEUE_OK = Queue_Pop(&qUartTx, &temp))
{
// temp 为队列弹出的数据
}
else
{
// 弹出数据失败
}</pre>

<strong>2、多数据弹出</strong>

若需要将多个数据弹出队列可以使用 Queue_Pop_Array 函数,原理上循环调用 Queue_Pop 函数来实现的,需要注意的是,成功弹出的数据将从队列中删除,函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">unsigned int Queue_Pop_Array(QUEUE_HandleTypeDef * hqueue, QUEUE_DATA_T * pdatas, unsigned int len)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要弹出数据的队列结构。</td>
</tr>
<tr>
<td>pdatas</td>
<td>用于保存弹出数据数组的首地址。</td>
</tr>
<tr>
<td>len</td>
<td>需要弹出数据数组的长度。</td>
</tr>
</tbody>
</table>

当需要弹出数据的长度大于队列中的数据长度时,弹出数组多余的空间将不会被赋值。

返回值说明:

<ul data-tool="mdnice编辑器">
<li>
<p>该函数将返回实际从队列中弹出的数据长度。</p>
</li>
<li>
<p>当队列中的数据长度足够时,返回值将等于参数 len 的值。</p>
</li>
<li>
<p>当队列中的数据长度不足时,返回值为实际从队列中弹出的数据长度。</p>
</li>
</ul>

<strong>3、单数据复制</strong>

当需要从队列头部获取数据,但又不希望数据从队列中删除时,可以使用 Queue_Peek 函数来实现,该函数的参数与返回值与 Queue_Pop 完全相同。

使用 Queue_Peek 和 Queue_Pop 函数的区别在于:

<ul data-tool="mdnice编辑器">
<li>Queue_Pop 得到队列中的数据后会删除队列中的数据。</li>
<li>Queue_Peek 得到队列中的数据后会保留队列中的数据。</li>
</ul>

<strong>4、多数据复制</strong>

当需要从队列头部获取多个数据,但又不希望数据从队列中删除时,可以使用 Queue_Peek_Array 函数来实现,该函数的参数与返回值与 Queue_Pop_Array 完全相同。

使用 Queue_Peek_Array 和 Queue_Pop_Array 函数的区别在于:

<ul data-tool="mdnice编辑器">
<li>Queue_Pop_Array 得到队列中的数据后会删除队列中的数据。</li>
<li>Queue_Peek_Array 得到队列中的数据后会保留队列中的数据。</li>
</ul>

<strong><font color="#004a85">八、其他功能</font> </strong>

<strong>1、清空队列</strong>

当需要清空队列数据时,无需弹出所有数据,只需要调用 Queue_Clear 即可快速清空指定队列,在创建队列时会调用此函数来初始化队列,因此对于刚创建完成的队列无需调用清空队列函数。

函数原型:

<pre style="overflow-x:auto; background-color:#e9e9e9;">void Queue_Clear(QUEUE_HandleTypeDef * hqueue)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要清空的队列结构。</td>
</tr>
</tbody>
</table>

<strong>2、获取队列数据数量</strong>

当需要获取队列中的数据长度时,调用 Queue_Count 函数,函数原型如下:

<pre style="overflow-x:auto; background-color:#e9e9e9;">unsigned int Queue_Count(QUEUE_HandleTypeDef * hqueue)</pre>

参数说明:

<table border="1" align="center">
<thead>
<tr>
<th>参数名</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>hqueue</td>
<td>需要获取数据长度的队列结构。</td>
</tr>
</tbody>
</table>

返回值说明:

<ul data-tool="mdnice编辑器">
<li>该函数将返回队列中的数据长度。</li>
<li>返回值范围在0到创建队列时的长度之间。</li>
</ul>

<strong><font color="#004a85">License</font> </strong>

Copyright © 2020 QueueForMcu Released under the GPL-3.0 License.

来源:<a href="https://mp.weixin.qq.com/s/JehE4zq4eKVbRghE5smHcg">嵌入式大杂烩</a&gt;
免责声明:本文为转载文章,转载此文目的在于传递更多信息,版权归原作者所有。本文所用视频、图片、文字如涉及作品版权问题,请联系小编进行处理(联系邮箱:cathy@eetrend.com)。