简体   繁体   English

在Arduino Uno上读取图钉不起作用,需要有关AVR-GCC的帮助

[英]Reading a pin on Arduino Uno does not work, need help on avr-gcc

I am trying to control three LEDs on an Arduino Uno board. 我试图控制Arduino Uno板上的三个LED。 These LEDs are connected to the pin 3,4,5 of the port D as the outputs. 这些LED连接到端口D的引脚3、4、5作为输出。

The pin 2 of the port D is connected to a push button, and it is configured as the input. 端口D的针2连接到按钮,并且被配置为输入。

The whole circuit has bee tested with the default Arduino code and was fully functional. 整个电路已通过默认的Arduino代码进行了蜜蜂测试,并且功能齐全。 However, with the code given below (without arduino libraries), the input pin (pin 2 on port D) always reads HIGH, regardless the state of the push button. 但是,使用下面给出的代码(没有arduino库),无论按钮的状态如何,输入引脚(端口D上的引脚2)始终为HIGH。

Any help will be greatly appreciated. 任何帮助将不胜感激。

#include <avr/io.h>
#include <util/delay.h>

#define IS_LOW(reg, pin)  ((reg) & (1 << (pin)) == 0)
#define SET_BIT(reg, pin) do{(reg) |= 1 << (pin);} while(0)
#define CLEAR_BIT(reg, pin) do{(reg) &= ~(1 << (pin));} while(0)

int main (void)
{
    DDRD &= ~(1 << DDD2); //pin 2 on port D as INPUT
    DDRD |= 1 << DDD3;    //pin 3 on port D as OUTPUT
    DDRD |= 1 << DDD4;    //pin 4 on port D as OUTPUT
    DDRD |= 1 << DDD5;    //pin 5 on port D as OUTPUT

   while(1) 
   {
       if (IS_LOW(PIND, PD2))
       {
           SET_BIT(PORTD, PD3);
           CLEAR_BIT(PORTD, PD4);
           CLEAR_BIT(PORTD, PD5);
       }
       else
       {
           CLEAR_BIT(PORTD, PD3);
           CLEAR_BIT(PORTD, PD4);
           SET_BIT(PORTD, PD5);

           _delay_ms(250);

           SET_BIT(PORTD, PD4);
           CLEAR_BIT(PORTD, PD5);

           _delay_ms(250);
       }
    }
}

Unfortunately, the priority of & operator is lower than one of == operator. 不幸的是, &运算符的优先级低于==运算符之一。

You should define the IS_LOW macro like this: 您应该像这样定义IS_LOW宏:

#define IS_LOW(reg, pin) (((reg) & (1 << (pin))) == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM