如何把字符void AddToBuff(char X){//mov x to charbuff f = 0 ; CharBuff[n] = X ;//n is position if (n == 8){//if full uart_disable(); while(1){ if(gpio_get(P_B1) == 0){ uart_enable(); n = 0; uart_tx(' '); f = 1; break; } } } m = n; n++; } void ReadChar(char c){ if (c >= 'A' && c <= 'Z'){ X = c; } },传入这个队列int queue_is_full(Queue *queue) { return ((queue->tail + 1) % queue->size) == (queue->head % queue->size); } int queue_is_empty(Queue *queue) { return queue->tail == queue->head; }

时间: 2024-03-10 18:45:42 浏览: 18
可以将字符添加到队列中,然后通过队列操作将其传递给另一个函数。具体实现如下: 1. 在 AddToBuff 函数中,将字符 X 入队,即调用 queue_enqueue 函数: ``` void AddToBuff(char X){ if(!queue_is_full(&char_queue)) { queue_enqueue(&char_queue, X); } } ``` 2. 在 ReadChar 函数中,从队列中读取字符,即调用 queue_dequeue 函数: ``` void ReadChar() { uint8_t item; if(!queue_is_empty(&char_queue)) { queue_dequeue(&char_queue, &item); if(item >= 'A' && item <= 'Z') { X = item; } } } ``` 需要注意的是,为了让 AddToBuff 和 ReadChar 函数可以访问同一个队列,需要将队列定义为全局变量,并在程序初始化时进行初始化: ``` Queue char_queue; int main() { queue_init(&char_queue); ... return 0; } ```
相关问题

如何为这个charbuff定义head和tail.void AddToBuff(char X){//mov x to charbuff f = 0 ; CharBuff[n] = X ;//n is position if (n == 8){//if full uart_disable(); while(1){ if(gpio_get(P_B1) == 0){ uart_enable(); n = 0; uart_tx(' '); f = 1; break; } } } m = n; n++; } void ReadChar(char c){ if (c >= 'A' && c <= 'Z'){ X = c; } } void ReadFromBuff(int m){//read buff to m R = CharBuff[m]; }

要为CharBuff定义head(头)和tail(尾),可以在AddToBuff函数中添加两个变量来跟踪头和尾的位置。 例如,可以添加两个名为head和tail的变量来分别表示CharBuff数组中第一个有效字符的位置和下一个要添加字符的位置。在AddToBuff函数中,如果CharBuff数组已经存满了8个字符,则可以将head的值加1,表示第一个字符已经被移除。然后,可以将新的字符存储到CharBuff数组的尾部,并将tail的值设置为新的尾部位置。 下面是修改后的代码: ```c int head = 0; int tail = 0; void AddToBuff(char X) { if (tail == head && f == 1) { // if full uart_disable(); while (1) { if (gpio_get(P_B1) == 0) { uart_enable(); head = 0; tail = 0; uart_tx(' '); f = 0; break; } } } CharBuff[tail] = X; tail = (tail + 1) % 8; if (tail == head) { f = 1; } } void ReadFromBuff(int m) { int index = (head + m) % 8; R = CharBuff[index]; } ``` 这里使用了%运算符来实现循环队列。在AddToBuff函数中,如果CharBuff数组已满,则将f设置为1,表示队列已满。在ReadFromBuff函数中,使用head和m计算出实际的索引位置,以从CharBuff数组中读取数据。

如何为这个charbuff定义void AddToBuff(char X){//mov x to charbuff f = 0 ; CharBuff[n] = X ;//n is position if (n == 8){//if full uart_disable(); while(1){ if(gpio_get(P_B1) == 0){ uart_enable(); n = 0; uart_tx(' '); f = 1; break; } } } m = n; n++; } void ReadChar(char c){ if (c >= 'A' && c <= 'Z'){ X = c; } } void ReadFromBuff(int m){//read buff to m R = CharBuff[m]; }

这段代码定义了两个函数:AddToBuff和ReadFromBuff,还有一个ReadChar函数用于读取字符并将大写字母存储到变量X中。 AddToBuff函数的作用是将字符X存储到CharBuff数组中的第n个位置,然后将n加1。如果CharBuff数组已经存满了8个字符,那么就禁用UART通信并等待用户按下P_B1引脚。当用户按下P_B1引脚时,启用UART通信,将n重置为0,发送一个空格字符并将f设置为1。最后,将m设置为n。 ReadFromBuff函数的作用是从CharBuff数组中读取第m个位置的字符,并将其存储到R变量中。

相关推荐

You are required to write a C program to: • Initialize GPIO peripherals • Initialise UART peripheral for receiving ASCII characters ‘A’ to ‘Z’ at baud 9600 • Initialise an internal array to hold 10 characters with head and tail: CharBuff • Repeat the following:o When data is received on the serial communication port, read ASCII character X, o If received character X is a capital letter add it to CharBuff, else ignore. o While CharBuff is not empty, transmit the morse code of the oldest stored character by blinking the LED (code provided for you). o When CharBuff is full, disable UART RX. o If UART RX is disabled, pushing the button P_B1 will activate it; otherwise, pushing the button does not affect your programme. You are recommended to use interrupt to control UART receiving data and coordinate the operation between CharBuff and P_LD2. 在我的代码基础上完成以上任务#include #include <gpio.h> #include "delay.h" #include "uart.h" #include <stm32f4xx.h> /* ***************NOTE*********************** YOU CAN USE THE IN-UILT FUNCTION delay_ms(HOW_LONG) TO CAUSE A DELAY OF HOW_LONG MILLI SECONDS ******************************************* */ //placeholder /*void uart_rx_isr(uint8_t rx){ }*/ #define MAX 10 int uart_rx_enabled = 1; char CharBuff[MAX]; int head = 0; int tail = 0; int is_full() { return (tail + 1) % MAX == head; } int is_empty() { return head == tail; } void add_to_buffer(char c) { if (!is_full()) { CharBuff[tail] = c; tail = (tail + 1) % MAX; } else { uart_rx_enabled = 0; //uart_disable(); } } void uart_rx_isr(uint8_t c){ if (c >= 'A' && c <= 'Z') { if (!is_full()) { CharBuff[tail] = c; tail = (tail + 1) % MAX; } else { uart_rx_enabled = 0; //uart_disable(); } } } char remove_from_buffer() { char c = CharBuff[head]; head = (head + 1) % MAX; if (uart_rx_enabled == 0 && !is_full()) {//The buffer is not full after removing a char uart_rx_enabled = 1;//enable the Uart RX uart_enable(); } return c; } int main(void) { // Initialise GPIO. gpio_set_mode(P_LD2, Output); gpio_set_mode(P_B1, PullUp); // hardware/peripheral initialisation uart_init(9600); uart_enable(); uart_set_rx_callback(uart_rx_isr);//This callback function is triggered when data is received through the serial port while(1){ if(!is_empty()){ gpio_set(P_LD2, LED_ON); char c = remove_from_buffer(); } } } // ******* ARM University Program Copyright © ARM Ltd 2016 ****************** // ******* Queen Mary University of London Copyright Matthew Tang 2021 ******

最新推荐

recommend-type

6-10.py

6-10
recommend-type

基于机器学习的入侵检测系统+源码+说明.zip

基于机器学习的入侵检测系统+源码+说明.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望