c - C 多久/何时执行 if/else 语句中的内容?

标签 c embedded avr atmega

我正在开发一个嵌入式(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/

相关文章:

在 C 中使用远指针时更改代码大小

c - Bootstrap 。 ARM CORTEX M0+ 重定位中断表组件错误

python - 如何将Python数字转换为1字节C类型?

c - AVR C : Int to hexadecimal address

需要 C 标识符吗?

c - 错误 : expression result unused [-Werror, -Wunused-value] 矩阵 [i,z] = h,v;我不明白?

c - 如何在 32 位架构中将 64 位 unsigned long long 分配给 32 位结构

c++ - 通过引用将 int 数组传递给构造函数

小于 8 位的 C 变量

c - 我应该如何在心理上解析这个陈述?