- 打卡等级:偶尔看看I
- 打卡总天数:12
- 最近打卡:2024-10-07 14:22:23
注册会员
- 积分
- 95
|
本帖最后由 晓飛飛 于 2024-8-1 21:25 编辑
我想做一个有三个挡位的雨刮器 想用队列来记录他们的状态 这样好区分是加档还是减档 但是现在我这个代码好像有逻辑问题 求指导
#include "pad.h"
#include "usb_req_class.h"
#include <stdlib.h>
#include <stdio.h> // 用于输出调试信息
#define MAX_SIZE 2
int element;
BYTE xdata keyPadData[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
void Zelay20ms(void) //@24.000MHz
{
unsigned char data i, j, k;
_nop_();
_nop_();
i = 211;
j = 21111;
k = 23111;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void SendPadData(void) {
P1M0 = (P1M0 & ~0x0b) | 0xc0;
P1M1 &= ~0xcb;
P17=0;
P16=0;
}
// 循环队列结构体
typedef struct CircularQueue{
int *buffer; // 存储队列元素的数组
int front; // 队头指针
int rear; // 队尾指针
int size; // 队列长度
} CircularQueue;
// 初始化循环队列
void initCircularQueue(CircularQueue *q, int size) {
q->buffer = (int *)malloc(size * sizeof(int));
if (q->buffer == NULL) { // 检查内存分配是否成功
return;
}
q->front = 0;
q->rear = 0;
q->size = size;
}
// 判断循环队列是否为空
int isEmpty(CircularQueue *q) {
return q->front == q->rear;
}
// 判断循环队列是否已满
int isFull(CircularQueue *q) {
return ((q->rear + 1) % q->size) == q->front;
}
// 入队操作
void enqueue(CircularQueue *q, int element) {
if (isFull(q)) {
return;
}
q->buffer[q->rear] = element;
q->rear = (q->rear + 1) % q->size; // 新增:入队成功的输出
}
// 出队操作
int dequeue(CircularQueue *q) {
if (isEmpty(q)) {
return -1;
}
element = q->buffer[q->front];
q->front = (q->front + 1) % q->size; // 新增:出队成功的输出
return element;
}
int main() {
CircularQueue q;
initCircularQueue(&q, 2); // 队列长度为 2,只存储两个数
while (1) { // 持续监测引脚状态
P1M0 = (P1M0 & ~0x0b) | 0xc0;
P1M1 &= ~0xcb;
P17 = 0;
P16 = 0;
if (((P1&0X01) == 0) && (q.rear == 0)) { // P10 正接,队列空
P16 = 1;
P17 = 0;
Zelay20ms();
enqueue(&q, 10); // 尝试入队
}
if ((P1&0X02)==0 && (q.buffer[0] == 10)) { // P11正接 队头是P10
P16 = 0;
P17 = 1;
enqueue(&q, 11); // 将 11 加入队列
if ((q.rear + 1) % 2 == q.front) { // 如果队列已满
P16 = 0;
P17 = 1;
dequeue(&q); // 删掉队头
enqueue(&q, 11);} } // 将 11 加入队列
/*if ((P1&0X01)==0 && (q.buffer[0] == 10)) { // P10反接 队头是10
dequeue(&q); // P10 入队
P16=1;
P17=0;
}else if ((P1&0X08)==0) { // P13 接地
dequeue(&q); //踢出队首 P10
enqueue(&q, 13); // P13 入队
P16=1;
P17=0;
}else if((P1&0X02)==0 && (q.buffer[0] == 13)){ //P11接地 并且队头是P13这时P11反接
dequeue(&q); //踢出队首 P13
enqueue(&q, 12); //加入队尾
P17=1;
P16=0;
}else if((P1&0X01)==0 && (q.buffer[0] == 11)){ // P10反击接 并且队头为P11
dequeue(&q); //踢出队首
P17 =1;
P16 =0;*/
}
}
|
-
-
|