gpt4 book ai didi

c - 如何在按下按钮时返回初始屏幕?

转载 作者:行者123 更新时间:2023-12-05 02:38:09 25 4
gpt4 key购买 nike

我在连接到 8051 微 Controller 的 LCD 中有三个屏幕,当按下按钮时它们会滚动到不同的屏幕,但是当再次按下按钮时我想返回到初始屏幕。我该怎么做,谢谢?

#include <stdio.h>
#include <string.h>
#include <stdint.h>

void change_screen(void);
int count = 0;

void change_screen(void)
{
if (modebutton == 1) { // when button is pressed
if (count == 0) {
count++;
delay(100);
display_screen1();
modebutton=0;
}
else if (count == 1) {
count++;
delay(100);
display_screen2();
modebutton=0;
}
else if (count == 2) {
display_screen3();
count = 0;
}
}
}

最佳答案

您可以为此使用余数运算符 %

例子:

void change_screen(void) {
static unsigned count = 0;

if(modebutton) {
delay(100);
modebutton = 0;

switch(count) {
case 0: display_screen1(); break; // first fourth ...
case 1: display_screen2(); break; // second fifth
case 2: display_screen3(); break; // third sixth
}

count = (count + 1) % 3; // 0,1,2 then 0,1,2 etc...
}
}

关于c - 如何在按下按钮时返回初始屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69735143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com