之前一直认为I2C的SCL和SDA针脚是可以改为其他模拟输入的针脚,正好晚上闲来无事,翻了翻源码,终于让我找到了定义的位置:pins_arduino.h
这个文件中定义了所有针脚的宏及对应关系:
static const uint8_t SS = 10;
static const uint8_t MOSI = 11; static const uint8_t MISO = 12; static const uint8_t SCK = 13;static const uint8_t SDA = 18; static const uint8_t SCL = 19; static const uint8_t LED_BUILTIN = 13; static const uint8_t A0 = 14; static const uint8_t A1 = 15; static const uint8_t A2 = 16; static const uint8_t A3 = 17; static const uint8_t A4 = 18; static const uint8_t A5 = 19; static const uint8_t A6 = 20; static const uint8_t A7 = 21;从这里能够看出,对于特殊针脚的一些定义,其中逻辑针脚是接着led的13继续定义的。而SDA和SCL与A4和A5定义为同一针脚,从这里可以看出,系统默认的就是A4和A5了,如果想换其他针脚,可以对应修改。在twi.c中对SCL和SDA的直接引用,下面红色部分代码:
void twi_init(void) { // initialize state twi_state = TWI_READY; twi_sendStop = true; // default value twi_inRepStart = false; // activate internal pullups for twi. digitalWrite(SDA, 1); digitalWrite(SCL, 1); // initialize twi prescaler and bit rate cbi(TWSR, TWPS0); cbi(TWSR, TWPS1); TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; /* twi bit rate formula from atmega128 manual pg 204 SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) note: TWBR should be 10 or higher for master mode It is 72 for a 16mhz Wiring board with 100kHz TWI */ // enable twi module, acks, and twi interrupt TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); }