我正在开发一个嵌入式(avr)项目,基本上我想根据引脚被按下的时间打印出一些不同的东西。我无法弄清楚当值传递并满足 if 语句时会发生什么(按钮仍然按下,因此计数器递增)。
设置如下:
if overflows is between 7-48 (button pressed for 30ms-200ms), print out a '.'
if overflows is greater than 48 (button pressed for greater than 200ms), print out a '-'
if overflows is greater than 97 (button has not been pressed in over 400ms), print out a ' '
我当前的代码如下:
static inline void isr(char type) {
static unsigned int overflows = 0;
static unsigned char idx = 0;
if (type == 'e') { // edge captured
if (TCCR1B & 0x40) { // rising edge
if (overflows < 7) {
// do nothing
} else if (overflows < 49) {
buffer[idx++] = '.';
size++;
} else {
buffer[idx++] = '-';
size++;
}
}
overflows = 0; // restart counting overflows at each edge
} else { // overflow occured
overflows++;
if (buffer[idx-1] != ' ' && !(TCCR1B & 0x40) && overflows > 97) {
buffer[idx++] = ' ';
size++;
}
}
我不确定这是否正确,因为在“-
”之前似乎总是有一个“.
”,因为作为溢出值递增,满足<49条件。
有什么想法吗?
最佳答案
如果你想计算按下开关的次数,你可以使用while循环。 例如,
if(sw==0) //sw is switch connected with I/O pin
{
while(sw==0)
{
led=1; //LED is output
delay(); // use delay function
led=0;
delay();
count++;
}
}
通过使用while循环,您可以避免多次按下开关。如果您进行一次开关,计数就会加一。
关于c - C 多久/何时执行 if/else 语句中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35498551/