Jag har följt denna manual, men inget har fungerat för mig. Jag fort fortfarande den översta raden helt tjockmarkerad.
Jag lägger 3.3 volt på V0 och 5 volt på VDD. På min MCP23017 så ger den ut mellan 0 och 3.3 volt på GPIOA och GPIOB pinnarna. Det kanske måste vara 5 volt där också?
Jag har kollat om jag har kopplat rätt och det har jag. Jag har även jämfört med LED lampa om varje 1:a och 0:a stämmer och det gör det. Så kommunikationen mellan min CPU och MCP23017 är helt OK. Säger jag att t.ex. GPIOA7 ska vara hög så blir den hög.
Där har jag kopplat ihop följande hos MCP23017:
GPIOB0 -> RS
GPIOB1 -> R/W
GPIOB2 -> E
Och för A-sidan hos MPC23017
GPIOA7 -> D7
GPIOA6 -> D6
GPIOA5 -> D5
GPIOA4 -> D4
GPIOA3 -> D3
GPIOA2 -> D2
GPIOA1 -> D1
GPIOA0 -> D0
Jag har skrivit denna kod. Kolla vid for-loopen:
Kod: Markera allt
import java.io.IOException;
import com.pi4j.io.i2c.I2CDevice;
import se.danielmartensson.hardware.MCP3008.ReadMCP3008;
public class WriteMCP23017 {
private I2CDevice i2CDevice;
private ReadMCP3008 readMCP3008;
private int IODIRA = 0x00;
private int IODIRB = 0x01;
private int GPPUA = 0x0C;
private int GPPUB = 0x0D;
private int GPIOA = 0x12;
private int GPIOB = 0x13;
public WriteMCP23017(I2CDevice i2CDevice, ReadMCP3008 readMCP3008) {
this.i2CDevice = i2CDevice;
this.readMCP3008 = readMCP3008;
// Set B and A side to output and activate pull-up resistors
try {
this.i2CDevice.write(IODIRA, (byte) 0x00); // A-side output is low
this.i2CDevice.write(IODIRB, (byte) 0x00); // B-side output is low
this.i2CDevice.write(GPPUA, (byte) 0xFF); // Pull up for A side
this.i2CDevice.write(GPPUB, (byte) 0xFF); // Pull up for B side
/*
* Initi the LCD
*/
initLCD();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initLCD() {
// Set S
try {
// Set the D7->D0 bits
/*
* 0x30 = Init
* 0x38 = Function set
* 0x08 = Display off
* 0x01 = Clear display
* 0x06 = Entry mode set
*/
long sleepTime = 10;
byte[] D7_to_D0 = {0x30, 0x30, 0x30, 0x38, 0x08, 0x01, 0x06};
for(int i = 0; i < D7_to_D0.length; i++) {
i2CDevice.write(GPIOB, (byte) 0x00); // RS = 0, R/W = 0, E = 0
Thread.sleep(sleepTime);
i2CDevice.write(GPIOA, (byte) D7_to_D0[i]); // 0 0 1 1 * * * *
Thread.sleep(sleepTime);
i2CDevice.write(GPIOB, (byte) 0x0C); // RS = 0, R/W = 0, E = 1
Thread.sleep(sleepTime);
i2CDevice.write(GPIOA, (byte) 0x00); // 0 0 0 0 0 0 0 0 <--- Nollställ
Thread.sleep(sleepTime);
}
// Write out an S
i2CDevice.write(GPIOB, (byte) 0x01); // RS = 1, R/W = 0, E = 0
Thread.sleep(sleepTime);
i2CDevice.write(GPIOA, (byte) 0x53); // 0 1 0 1 0 0 1 1 = S
Thread.sleep(sleepTime);
i2CDevice.write(GPIOB, (byte) 0x05); // RS = 1, R/W = 0, E = 1
Thread.sleep(sleepTime);
i2CDevice.write(GPIOB, (byte) 0x01); // RS = 1, R/W = 0, E = 0
Thread.sleep(sleepTime);
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}