Miałem sporo pytań jak sprawdzić czy ekspander jest poprawnie podłączony i czy działają wejścia.
Poniżej prosty kod sprawdzający czy widać wszystkie 8 układów MCP23017 oraz po podaniu stanu niskiego na pin dostajemy informację o użytym wejściu po UART (USB).
Potrzebujemy bibliotekę do MCP , pobieramy ją z Gita https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
lub z
Adafruit_MCP23017 (790 pobrań )
Rozpakowujemy pliki do Arduino\libraries u mnie C:\Program Files (x86)\Arduino\libraries\Adafruit_MCP23017\
#include <Wire.h> #include "Adafruit_MCP23017.h" //Adafruit_MCP23017 *mcp[8]; Adafruit_MCP23017 mcp; Adafruit_MCP23017 mcp2; Adafruit_MCP23017 mcp3; Adafruit_MCP23017 mcp4; Adafruit_MCP23017 mcp5; Adafruit_MCP23017 mcp6; Adafruit_MCP23017 mcp7; Adafruit_MCP23017 mcp8; unsigned long previousMillis = 0; // will store last time was updated const long intervalMilis = 500; // interval at which to blink (milliseconds) byte pressTable[128][1]; void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); Serial.println("\nI2C Scanner"); i2cscanner(); mcp.begin(0); mcp2.begin(1); mcp3.begin(2); mcp4.begin(3); mcp5.begin(4); mcp6.begin(5); mcp7.begin(6); mcp8.begin(7); byte i=0; for(i=0;i<=15;i++){ mcp.pinMode(i, INPUT); mcp.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp2.pinMode(i, INPUT); mcp2.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp3.pinMode(i, INPUT); mcp3.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp4.pinMode(i, INPUT); mcp4.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp5.pinMode(i, INPUT); mcp5.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp6.pinMode(i, INPUT); mcp6.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp7.pinMode(i, INPUT); mcp7.pullUp(i, HIGH); } for(i=0;i<=15;i++){ mcp8.pinMode(i, INPUT); mcp8.pullUp(i, HIGH); } delay(1000); } void loop() { byte in; byte i; for(i=0;i<16;i++){ in = mcp.digitalRead(i); if(in == 0){ pressTable[i][0] = 1; } } for(i=0;i<16;i++){ in = mcp2.digitalRead(i); if(in == 0){ pressTable[i+16][0] = 1; } } for(i=0;i<16;i++){ in = mcp3.digitalRead(i); if(in == 0){ pressTable[i+32][0] = 1; } } for(i=0;i<16;i++){ in = mcp4.digitalRead(i); if(in == 0){ pressTable[i+48][0] = 1; } } for(i=0;i<16;i++){ in = mcp5.digitalRead(i); if(in == 0){ pressTable[i+64][0] = 1; } } for(i=0;i<16;i++){ in = mcp6.digitalRead(i); if(in == 0){ pressTable[i+80][0] = 1; } } for(i=0;i<16;i++){ in = mcp7.digitalRead(i); if(in == 0){ pressTable[i+96][0] = 1; } } for(i=0;i<16;i++){ in = mcp8.digitalRead(i); if(in == 0){ pressTable[i+112][0] = 1; } } unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= intervalMilis) { for(i=0;i<129;i++){ if(pressTable[i][0] == 1){ Serial.print("Press MCP: "); Serial.println(i); } } resetTable(); previousMillis = currentMillis; } } void resetTable(){ byte i; for(i=0;i<129;i++){ pressTable[i][0] = 0; } } void i2cscanner() { 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.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"); }
Jeśli wszystko podpięliśmy poprawnie dostaniemy informację o adresach MCP oraz informację o wciskanych wejściach (wejścia zwieramy do masy).
I2C Scanner Scanning... I2C device found at address 0x20 ! I2C device found at address 0x21 ! I2C device found at address 0x22 ! I2C device found at address 0x23 ! I2C device found at address 0x24 ! I2C device found at address 0x25 ! I2C device found at address 0x26 ! I2C device found at address 0x27 ! done Press MCP: 0 Press MCP: 1 Press MCP: 3 Press MCP: 4 Press MCP: 16 Press MCP: 17 Press MCP: 18 Press MCP: 19 Press MCP: 21 Press MCP: 32 Press MCP: 33 Press MCP: 34 Press MCP: 34 Press MCP: 35 Press MCP: 44 Press MCP: 45 Press MCP: 46 Press MCP: 46 Press MCP: 47 Press MCP: 53 Press MCP: 54 Press MCP: 55 Press MCP: 56