PDA

View Full Version : Read/write a single bit (MCP23018)



vladimir059@hot
- 28th June 2011, 03:36
:eek::eek:
Hello everybody.
I’m stack in a simple problem. My application required communication with I/O expander MCP23018.
I made it work except one thing. I need to read single bit and ignore all others. I need also to write a single bit and leave unchanged others. As command looks like this
i2cwrite sda,scl,CTRL_EE_W,[GPIOB,%00000011]
so, how can I change, for example 3 bit, and leave all other bits unchanged ( value of other bits unknown and might change)
I need also read a single bit and ignore value of others.
i2cread sda,scl,CTRL_EE_R,[IO_dat]
Any input welcome!
Vladimir

mister_e
- 28th June 2011, 03:53
Bitwise operation & masks

assuming VarA = %11110000

If i want to set a single bit, say Bit0, I can do it like
VarA.0=1

OR doing it with mask
VarA = VarA | %00000001

If you want to clear a bit, says bit 7, then you use
VarA.7=0

OR

VarA = VarA & %01111111

If I want to READ a single bit, say bit 2, then, I can use
AnotherVar = VarA.2

vladimir059@hot
- 28th June 2011, 04:06
Thank you, Steve.
It is all I need!!!
:)

mister_e
- 28th June 2011, 04:11
No problem dear Canadian brother ;)