I was initially not getting any joy out of the OLED display and it remained black despite my efforts. However, I had stupidly placed the initialization of the PCA9546 multiplexer after the initialization of the OLED display. Since the OLED cannot be addressed until the mux is set to channel 1, no joy.
I did write a somewhat useful utility that enumerates all i2c device id's that can be seen on the Arduino i2c bus as well as on each of the four channels of the mux which others may find useful.
Debugging this did point out to me an error in my PCA9546 code and a couple of changes I would like to make.
1. I have mistakenly made the selectChannel() method private, when it should be public.
2. The channel argument to selectChannel() is currently a bit field. The bottom 4 bits indicate which of the four channels should be enabled. I believe I will change this to just take a channel number rather than a bit field. I also need a way to deselect all channels.
3. I need to provide a constructor for the PCA9546 class that will allow initialization without selecting any channel.
I will provide an updated listing of PCA9546 class with these changes in a separate post.
Here is the code for scanning for devices connected to the i2c bus and all channels of the PCA9546.
// i2c_scanner
//
#include <Wire.h>
#include "PCA9546.h"
PCA9546 *mux;
char buf[66];
void setup()
{
Wire.begin();
Serial.begin(115200);
Serial.println("i2c Scanner");
mux = NULL;
}
void printAddress(byte address)
{
if (address<16) Serial.print("0");
Serial.println(address, HEX);
}
void loop()
{
uint8_t nChannel;
for (nChannel = 0; nChannel <= 4; nChannel++)
{
Serial.print("Channel ");
Serial.println(nChannel, DEC);
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
if (nChannel > 0)
{
if (mux == NULL)
{
mux = new PCA9546(0x70, 1 << (nChannel - 1));
}
else
{
bool fRc = mux->selectChannel(1 << (nChannel - 1));
}
}
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
// Just for good measure, try again
if (error != 0)
{
delay(10);
Wire.beginTransmission(address);
error = Wire.endTransmission();
}
if (error == 0)
{
Serial.print("i2c device found at address 0x");
printAddress(address);
nDevices++;
}
else if (error == 4)
{
Serial.print("Unknown error at address 0x");
printAddress(address);
}
}
if (nDevices == 0)
Serial.println("No i2c devices found\n");
else
Serial.println("");
}
// Hang the script
while (1==1);
}
No comments:
Post a Comment