我正在尝试并行使用多个mcp23017芯片,但并非每次都使用相同数量的芯片。我的代码是针对一个地址(默认地址)完成的,但是对于使用多个地址,则需要声明另一个mcp实例,每个实例用于所有现有地址?因为这样,如果我使用的是8芯片,我的代码将非常大...我可以使用某种方式搜索所有地址(完成)的循环,并且对所有地址仅使用一个mcp实例?
/*
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()
答案 0 :(得分:0)
解决方案1:不要使用Adafruit_MCP23017库,请自己编写。
解决方案2:将一个函数更新到Adafruit_MCP23017.c(并声明到Adafruit_MCP23017.h),该函数将更新i2caddr变量。像这样:
void Adafruit_MCP23017::setAddr(uint8_t addr) {
if (addr < 8) {
i2caddr = addr;
}
}
这将使您可以随时更改地址。