我正在使用arduino ide为atmega328p构建测试代码。我们可以使用简单的int变量设置数字引脚;
我怎样才能通过模拟引脚实现这一目标?
我试着查看arduino.h的核心代码库,但没有找到任何为模拟引脚定义的东西,所以我不知道如何处理这个问题。我已经解决了这个问题,但我不想在不理解这一点的情况下尝试。
//this we can do for digital pins
int pin = 0;
void someMethod(){
pinMode(pin,INPUT);
}
像A0
和A1
这样的值是小的正数,因此如果你愿意,你可以将它们存储在int
变量中。使用uint8_t
会更有意义。
你可以在这里看到如何为Arduino Uno(ATmega328P)定义这些名称:
https://github.com/arduino/ArduinoCore-avr/blob/master/variants/standard/pins_arduino.h
#define PIN_A0 (14)
#define PIN_A1 (15)
#define PIN_A2 (16)
#define PIN_A3 (17)
#define PIN_A4 (18)
#define PIN_A5 (19)
#define PIN_A6 (20)
#define PIN_A7 (21)
static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;