Hi everyone,
i will try to explain my project the best as i can ^^.
It consist on the control of a MCP23017, based on some characters readed from Serial Port. For example i send from an external software source some characters like "23A7#" <--- This being ASCII characters.
The two first digits can be from 1 to 32, the third character can be 'A' or 'B', the fourth digit can be from 1 to 8, and the # is just to tell to my self the transmission ends.
I got 8 MCP23017 connected to 32 (8:1) Multiplexer which the have 4 selectors pins including the enable pin.
So with some characters i need to be able to control these selector pins. The connections are the next:
- Bank A Pins - Connected to Two multiplexers, (En, A0, A1, A2) in this order.
- Bank B Pins - Connected to Two multiplexers in the same order.
So each MCP23017 controls 4 Multiplexer, and each pin bank controls 2 mux. On bank 'A', high nible bits control Mux nº2, low nible bits control mux nº1. On bank 'B', high nible bits control mux nº4, low nible bits control mux nº3. This for each MCP23017 and multiplexers.
What i pretend to do with this operations is:
For the example "23A7#":
23 - Determines the number of the multiplexer, from there i should extract the address of the MCP23017 which controls it. And also i have to determine if the multiplexer is controlled by the high nible of a bank or the low nible.
'A' or maybe 'B' - Determines the bank, 0x12 or 0x13.
7 - Determines the bits i want to send to (A0, A1, A2) pins. (7 => 110) so send it displaced one position for low nible, and five for high nible.
--- Also i share my actual code, which is horrible and im trying to figure to do that and progress on that. If someone can share me a hand would be nice.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Wire.begin();
!!!!! All pins are configured as outputs but don't want to extend the code here more!!
}
void sendconfig(int muxnum, int banknum, int combnum)
{
byte combination[8] = {B000, B001, B010, B011, B100, B101, B110, B111};
byte address[8] = {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27};
byte bank[2] = {0x12, 0x13};
byte bankdata[16] = {bank1a, bank1b, bank2a, bank2b, bank3a, bank3b, bank4a, bank4b, bank5a, bank5b, bank6a, bank6b, bank7a, bank7b, bank8a, bank8b};
byte pinsx;
float addnumf = (muxnum / 4) + 0.75;
int addnum = round(addnumf) - 1;
int combnumx = combnum - 1;
float bdataf = (muxnum / 2);
int bdatax = round(bdataf);
int bdataevencheck = muxnum % 2;
if(bdataevencheck == 0){
pinsx = (bankdata[bdatax] | (combination[combnumx] << 5));
bankdata[bdatax] = pinsx;
} else{
pinsx = (bankdata[bdatax] | (combination[combnumx] << 1));
bankdata[bdatax] = pinsx;
}
byte addressx = address[addnum];
byte bankx = bank[banknum];
Wire.beginTransmission(addressx);
Wire.write(bankx);
Wire.write(pinsx);
Wire.endTransmission();
}
void loop() {
int mnum, bnum, cnum;
// put your main code here, to run repeatedly:
while(Serial.available()>0)
{
c = Serial.read();
SerialData += c;
if(c == 'A' || c == 'B'){
mnum = SerialData.toInt();
if(c == 'A'){
bnum = 0;
} else {
bnum = 1;
}
c = 0;
SerialData = "";
}
if(c=='#'){
cnum = SerialData.toInt();
sendconfig(mnum, bnum, cnum);
c = 0;
SerialData = "";
}
}
}
Thanks everyone for reading and i hope i can get some ideas or help ^^.