system
October 27, 2014, 2:19pm
1
Hi,
I need help
I am using a mcp23016 Multiplexer in a ZX-RelayY16 board and I need to active the different relay, one by one.
I am using this code and it nearly works. However it does this sequence:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
I can't understand how mcp23016 works. Anyone can help me
Pierre
#include <Wire.h> // specify use of Wire.h library.
/* RELAY */
#define MCP23016_I2C_ADDRESS 0x20
#define GP0 0x00 // register addresses
#define GP1 0X01
#define OLAT0 0x02
#define OLAT1 0x03
#define IPOL0 0x04
#define IPOL1 0x05
#define IODIR0 0x06
#define IODIR1 0x07
#define INTCAP0 0x08
#define INTCAP1 0x09
#define IOCON0 0x0A
#define IOCON1 0x0B
int jj = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MCP23016_I2C_ADDRESS); // setup out direction registers
Wire.endTransmission();
}
void loop() {
Wire.beginTransmission(0x20); // set mcp23016 for all output
Serial.println(jj);
if (jj<8) {
Wire.write(GP1);
Wire.write(GP0);
}else{
Wire.write(GP0);
Wire.write(GP1);
}
if (jj==7) Wire.write(128);
if (jj==6) Wire.write(64);
if (jj==5) Wire.write(32);
if (jj==4) Wire.write(16);
if (jj==3) Wire.write(8);
if (jj==2) Wire.write(4);
if (jj==1) Wire.write(2);
if (jj==0) Wire.write(1);
if (jj==15) Wire.write(128);
if (jj==14) Wire.write(64);
if (jj==13) Wire.write(32);
if (jj==12) Wire.write(16);
if (jj==11) Wire.write(8);
if (jj==10) Wire.write(4);
if (jj==9) Wire.write(2);
if (jj==8) Wire.write(1);
Wire.endTransmission();
if (jj > 16) jj = -1;
jj++;
delay(1000); // }
} // end loop
Untested but it should be close:-
void loop() {
byte bitToSet = 0;
Wire.beginTransmission(0x20);
Serial.println(jj);
if (jj<8) {
Wire.write(GP0);
bitToSet = 1 << jj;
}else{
Wire.write(GP1);
bitToSet = 1 << (jj-8);
}
Wire.write(bitToSet);
Wire.endTransmission();
if (jj > 14) jj = -1;
jj++;
delay(1000); // }
} // end loop
system
October 30, 2014, 12:15pm
4
Thanks Grumpy, but still doesn't work... it's close yes but it does something like:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
....
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0
system
October 30, 2014, 1:54pm
5
I found ! It's not well optimmised, but it works
#include <Wire.h> // specify use of Wire.h library.
/* RELAY */
#define MCP23016_I2C_ADDRESS 0x20
#define GP0 0x00 // register addresses
#define GP1 0X01
#define OLAT0 0x02
#define OLAT1 0x03
#define IPOL0 0x04
#define IPOL1 0x05
#define IODIR0 0x06
#define IODIR1 0x07
#define INTCAP0 0x08
#define INTCAP1 0x09
#define IOCON0 0x0A
#define IOCON1 0x0B
int jj = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop() {
jj++;
Wire.beginTransmission(MCP23016_I2C_ADDRESS); // setup out direction registers
Wire.write(IODIR0); // pointer
Wire.write(GP0); // DDR Port0 all output
Wire.write(GP1); // DDR Port1 all input
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(1);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(2);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(128);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP0);
Wire.write(0);
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS); // setup out direction registers
Wire.write(IODIR0); // pointer
Wire.write(GP1); // DDR Port0 all output
Wire.write(GP0); // DDR Port1 all input
Wire.endTransmission();
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP1);
Wire.write(1);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP1);
Wire.write(2);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP1);
Wire.write(128);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(MCP23016_I2C_ADDRESS);
Wire.write(GP1);
Wire.write(0);
Wire.endTransmission();
} // end loop
pierromond:
Thanks Grumpy, but still doesn't work... it's close yes but it does something like:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
....
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0
yes looks like I forgot to clear the register when swapping over to the other one, problem is I was trying to keep it close to your code.
It's not well optimmised,
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.
You can say that again.