changing the status of a single pin MCP23016/7

Hi
I'm new to using the MCP23016 chip
I'd like to know how to write to an single output pin WITHOUT changing any other pin, may involve writing to a single BIT in a register. The data sheet is mind boggling for a newbie,
eg what i would like to is, say i have several pins high and want to make one of them low or another high.
I'm lost and need to be found,
cheers
 

hippy

Ex-Staff (retired)
I'm not familiar with the chip so don't know if there is a way to do that directly or not but one solution may be to keep a copy of the output bits within your program, update the appropriate bit in a byte and then re-send the entire byte. Only the altered bit(s) will change on the output.
 

Chavaquiah

Senior Member
I don't think you can send the MCP23016 a command to change a single pin, so you have to control an entire port (8 pins) at a time.

You need to know the state of the pins you want to leave unchanged. Two options:
1) Keep a record on the Picaxe of the state of each pin (for instance, the last value you sent to the MCP23016);
2) Read the state of the port from the 23016 into a byte variable.

Once you know the current state, you can use logic to change any pin and then send the whole byte data to the 23016.

For instance, say you want to change GP0.5 to high. Pseudo code would be:

B0 = [current GP0 state] 'Read from MCP or from some variable
B0 = B0 OR %00100000 'Set bit 5 high, leave all other bits unchanged
WRITE B0 to MCP23016

To reset a bit (to zero it) leaving others unchanged, instead of OR you have to use AND with an inverted mask:
B0 = B0 AND %11011111
 
Thanks for your help

I was thinking along the lines of reading the byte from the register into a variable (eg b1)and then using a readtable command and adding or subtracting according to the pin i wanted to change

table 0,(1,2,4,8,16,32,64,128); dec value of the pins 0 to 7
readi2c $12, (b1) ;to obtain state of pins into b1
readtable b10, b2 ;put value of pin to be changed (b10) into b2
let b1 = b1+b2
writei2c $12,(b1);

I never though of using AND/OR
I'll give it a go
Again thank you very much
 

Chavaquiah

Senior Member
Addition and subtraction don't work too well for bit setting/resetting. Think about the following binary example: %00000001 + %00000001 = %00000010. Overflow carries over to the next bit and spoils the cooking. :D

When the bit you want to change is not predefined, a lookup table is one good solution. Either using TABLE and READ or LOOKUP (lookup this one on the manual!)

If you are using a X1 or X2 Picaxe, you can also see the << operator. For instance, to set bit 4:

LET b1 = 1 << 4 & b1
 
Hi
Sill working on getting my head around this chip
have tried both suggestions regarding changing pin state using AND/OR and also a lookup table
During simulation on the Picaxe editor (even thought the mcp23017 doesn't simulate) everything works just fine (as far as the maths goes) . BUT
when i run it in VSM it works fine for a second and then random pins activate and i quickly get this error
"{MCP23X17 READ } Register address $25 is invalid"
I never access this address, so I'm confused.
Is it possible that VSM doesn't emulate the mcp23017 properly?
Cheers


"Aliens are truly here, they're hiding as the authors of Data sheets, their language is incomprehensible to us simple humans"
 
Top