I2C的SCL和SDA的定義

以前一直認爲I2C的SCL和SDA針腳是能夠改成其餘模擬輸入的針腳,正好晚上閒來無事,翻了翻源碼,終於讓我找到了定義的位置:pins_arduino.h git

這個文件中定義了全部針腳的宏及對應關係: ui

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了,若是想換其餘針腳,能夠對應修改。 spa

在twi.c中對SCL和SDA的直接引用,下面紅色部分代碼: orm

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);
}

源碼

相關文章
相關標籤/搜索