如何在adafruit mcp23017.h库中使用多个mcp23017芯片

时间:2016-04-30 11:20:58

标签: arduino i2c

我一直在为我的arduino ATMega2560试验I2C和mcp23017 IO扩展器芯片,因为我宁愿在arduino上使用它自己的其他东西,我只是想弄清楚如何使用adafruit mcp23017.h库和cant弄清楚如何处理多个mcp23017芯片以及如何单独使用这些引脚这是我编辑的按钮库中的代码。

我希望能够解决单个芯片和引脚我不太确定在设置中,如果多个芯片连接并在代码中寻址,IO的引脚模式将从0到15顺序上升。例如,如果第一个芯片被寻址为0x20并且IO数量计数从0到15,如果我添加并寻址另一个芯片为0x21,则该计数将从0到15变为0 - 31.~如果您可以推荐或发送,请编辑一个更容易或可以帮助的图书馆。

#include <Wire.h>
#include "Adafruit_MCP23017.h"

//pin 1 and 0 are mcp pins not arduino IO pins

Adafruit_MCP23017 mcp;

void setup() {  
mcp.begin();      // use default address 0

mcp.pinMode(0, INPUT);
mcp.pinMode(1, OUTPUT);
Serial.begin(9600);

pinMode(13, OUTPUT);  // use the p13 LED as debugging
}



void loop() {
// The LED will 'echo' the button
digitalWrite(13, mcp.digitalRead(0)); //Writes pin 13 to the reading of pin    0
mcp.digitalWrite(1, mcp.digitalRead(0)); //Writes pin 1 to the reading of 0
if(mcp.digitalRead(1) == HIGH){ //if pin 1 == high serialprint led whent   high
Serial.println("Led whent HIGH");
}
}

3 个答案:

答案 0 :(得分:5)

每个芯片必须具有唯一的地址,根据Microchip's manual第8页,这是可行的。首先,您应该在硬件布局中设置不同的地址。

您还应为要使用的每个芯片创建一个Adafruit_MCP23017对象,并在代码中设置相应的地址。

在这种情况下,所有芯片的引脚将具有0-15范围内的地址。要更改引脚的状态,您应该引用特定的实例。

更新

这是你的起点

#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
Adafruit_MCP23017 mcp3;
#define addr1 0x00
#define addr2 0x01
#define addr3 0x02
void setup() {
 mcp1.begin(addr1);
 mcp2.begin(addr2);
 mcp3.begin(addr3);
}

void loop() {


}

答案 1 :(得分:2)

我使用MCP23017与7 adafruit library芯片,7x14引脚= 98引脚共享我的解决方案。

<强>连接:

addr 0 = A2 low , A1 low , A0 low  000
addr 1 = A2 low , A1 low , A0 high 001
addr 2 = A2 low , A1 high , A0 low  010
addr 3 = A2 low , A1 high , A0 high  011
addr 4 = A2 high , A1 low , A0 low  100
addr 5 = A2 high , A1 low , A0 high  101
addr 6 = A2 high , A1 high , A0 low  110
addr 7 = A2 high, A1 high, A0 high 111

Connect pin #12 of the expander to Analog 5 (i2c clock)
Connect pin #13 of the expander to Analog 4 (i2c data)
Connect pins #15, 16 and 17 of the expander to ground (address selection)
Connect pin #9 of the expander to 5V (power) // operation voltage is 1.8 to 5.5
Connect pin #10 of the expander to ground (common ground)
Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)

Input #0 is on pin 21 so connect a button or switch from there to ground

<强>代码:

#include <Adafruit_MCP23017.h>


Adafruit_MCP23017 mcp1; // chip 1
Adafruit_MCP23017 mcp2; // chip 2

#define addr1 7 // 7 = A2 high, A1 high, A0 high
#define addr2 0 // 0 = A2 low, A1 low, A0 low
void setup() 
  {
    Serial.begin(9600); 
    mcp1.begin(addr1);
    mcp2.begin(addr2);

    mcp1.pinMode(0, INPUT); //pin 21 on chip
    mcp2.pinMode(0, INPUT); //pin 21 on chip

  } 

  void loop()
  { 
      if(mcp1.digitalRead(0)== HIGH )
      Serial.println("HIGH"); 
      delay(1000);

      if(mcp2.digitalRead(0)== HIGH )
      Serial.println("HIGH 2"); 
      delay(1000);
  }

答案 2 :(得分:1)

我终于让我的代码工作了!最后它比我简单,我可能没有给出我的代码应该实现的足够好的解释这是我的代码在这里。

#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
void setup() {
mcp1.begin(0);
mcp2.begin(1);

mcp1.pinMode(0, INPUT);
mcp1.pinMode(1, OUTPUT);
mcp2.pinMode(1, OUTPUT);
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop() {
mcp2.digitalWrite(1, mcp1.digitalRead(0));
digitalWrite(13, mcp1.digitalRead(0));
mcp1.digitalWrite(1, mcp1.digitalRead(0));
if(mcp1.digitalRead(1) == HIGH){
Serial.println("Led whent HIGH");
}
}

问题是我只宣布了1 mcp并且感谢Vladimir Tsykunov我现在知道我需要一个mcp1和mcp2现在可以在我的功能代码中看到我将来遇到的问题我可能会遇到问题没有修改,但是对于这个项目它现在正在运作。

相关问题