首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用多个MCP23017芯片的寻址问题

使用多个MCP23017芯片的寻址问题
EN

Stack Overflow用户
提问于 2019-11-07 17:12:54
回答 1查看 443关注 0票数 0

我正在尝试并行使用多个mcp23017芯片,但并不是所有时间我都会使用相同数量的芯片。我的代码是针对一个地址完成的,默认地址是...但是为了使用多个地址,需要声明另一个mcp实例,每个实例用于所有现有地址?因为在这种情况下,如果我使用8个芯片,我的代码将非常非常大…我可以以某种方式使用一个循环,它搜索所有地址(完成),并只使用一个mcp实例的所有地址?

代码语言:javascript
复制
/*
Name : Program for MCP23017 uC with Arduino Board
Version : v1_01
Date : 26.06.2019
Author : 

ALL RIGHTS RESERVED

NOTE : Install the Adafruit MCP23017 library
          1. Open the Arduino IDE
          2. Select 'Sketch' -> 'Include Library' -> 'Manage Libraries'
          3. Search for '23017'
          4. Click 'Install' button for the 'Adafruit MCP23017 Arduino Library...'
*/

// v1_02 - change commands handling
// v1_03 - add I2C Scanner 

#include "Wire.h"
#include "Adafruit_MCP23017.h"

Adafruit_MCP23017 mcp; // Create mcp0 instance : Chip 0

int dly = 250;

void setup() 
{

   Serial.begin(9600);
   Serial.println("Continental Timisoara - FF PSS ECC IE TE");
   Wire.begin();

 /// Scan I2C addresses

  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.print(address);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

 /// -------------------



   mcp.begin(0); // Start mcp on Hardware address 0x20 ( all pins LOW )

   for(int i = 0; i <= 7; i++) 
   {
    mcp.pinMode(i, OUTPUT);
   }

   for(int i = 8; i <= 15; i++) 
   {
    mcp.pinMode(i, INPUT);
   }

}

void loop() {

  if(Serial.available()>0)
  {
      String letter = "";

  while(Serial.available()>0)
  {
  letter +=char(Serial.read());
  delay(100);
  }

   // =========================================================================================

    String Command = "";
    Command = letter.substring(2,3);

    String Reset = "";
    Reset = letter.substring(1,4);

    if (Command == "A")
    {

     String sOutput_Port = "";
     sOutput_Port = letter.substring(4,5);
     int iOutput_Port = sOutput_Port.toInt();

     mcp.digitalWrite(iOutput_Port,HIGH);
     int iInput_Port = iOutput_Port + 8;
     if(mcp.digitalRead(iInput_Port)==HIGH)
        {
        String Show = "Channel ";
        Show.concat(sOutput_Port);
        Show.concat(" is ON");

        Serial.println(Show);        
        }
        else
        {
        Serial.println("NO VALIDATION"); 
        }

    }


  if (Command == "I")
    {

     String sOutput_Port = "";
     sOutput_Port = letter.substring(4,5);
     int iOutput_Port = sOutput_Port.toInt();


     mcp.digitalWrite(iOutput_Port,LOW);
     int iInput_Port = iOutput_Port + 8;
     if(mcp.digitalRead(iInput_Port)==LOW)
        {
        String Show = "Channel ";
        Show.concat(sOutput_Port);
        Show.concat(" is OFF");

        Serial.println(Show);        
        }
        else
        {
        Serial.println("NO VALIDATION"); 
        }

    }


  if (Reset == "RST")
    {

           for(int i = 0; i <= 7; i++) 
            {
              mcp.digitalWrite(i,LOW); // Set GPA0 to LOW
            }

        Serial.println("GPA0 is RESET to LOW");

    }

// =========================================================================================

    }  // exit if(Serial.available()>0)

} // exit void loop()
EN

回答 1

Stack Overflow用户

发布于 2019-11-07 21:31:48

解决方案1:不要使用Adafruit_MCP23017库,编写自己的库。

解决方案2:向Adafruit_MCP23017.c (并向Adafruit_MCP23017.h声明)添加一个将更新i2caddr变量的函数。如下所示:

代码语言:javascript
复制
void Adafruit_MCP23017::setAddr(uint8_t addr) {
    if (addr < 8) {
      i2caddr = addr;
    }
}

这将允许您在运行时更改地址。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58745374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档