Help with I2C please
I'm trying to use MCP23017 i/o expander with I2C with SX/B.
I think there is something wrong with my main code where I read the switches connected to PORTB of MCP23017.
Main:
· ' main code here
· 'read switches on GPIOB
· Reg = gpiob
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSTART SDA
· I2CSEND SDA, Rd_23017, b
· I2CRECV SDA, i2cData,·1··········· 'I've tried both 1 and 0, neither works.
· inVal = i2cData
If I comment above statement, then the following statement works
'output value read to LED
· Reg = OLATA
· i2cData = inVal··· 'works if I·comment out·section above and use i2cData = %11001010 for example
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSEND SDA, i2cData, b
· I2CSTOP SDA
Can someone please tell me what is wrong?
I attach my file here.
I think there is something wrong with my main code where I read the switches connected to PORTB of MCP23017.
Main:
· ' main code here
· 'read switches on GPIOB
· Reg = gpiob
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSTART SDA
· I2CSEND SDA, Rd_23017, b
· I2CRECV SDA, i2cData,·1··········· 'I've tried both 1 and 0, neither works.
· inVal = i2cData
If I comment above statement, then the following statement works
'output value read to LED
· Reg = OLATA
· i2cData = inVal··· 'works if I·comment out·section above and use i2cData = %11001010 for example
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSEND SDA, i2cData, b
· I2CSTOP SDA
Can someone please tell me what is wrong?
I attach my file here.
Comments
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol5/col/nv109.pdf
You'll also want to keep in mind that I2C functions are time-oriented (like PAUSE, SERIN, SEROUT) so they can chew through a lot of your code space -- this means it's best to encapsulate them in custom subroutines and functions. I've attached an I2C template with code I use in my projects. I've also attached an update template (drop into your \templates folder) as the one you're using is quite old.
Also, in Jon's mention of the similar device, he shows pulling the (unused) SPI clock line low when using I2C. Also for reasons that are unclear, the datasheet for your device shows 1k pullups for SDA/SCL (kinda low, but not unheard of). Do you have the reset pin of the device pulled high? Just throwing out ideas here....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Jonny Mac, thans for the files. I will study them. YES, I was shocked to see how much memory such a paltry code of mine used up. I definitely need to examine ways to make it more efficient, memory wise.
Jonny Mac, thans for the files. I will study them. YES, I was shocked to see how much memory such a paltry code of mine used up. I definitely need to examine ways to make it more efficient, memory wise.
'Use: byteVal = I2C_IN AckBit
What is AckBit? Is it a global variable?
For example, if I want to receive a byte from I2C device then give NAK to device, is this correct usage:
byteVal = I2C_IN Nak
If my assumption is wrong, can you please give an example?
- - - - -
For I2C_OUT, is this correct usage?
ackNak = 0 'want Ack
I2C_OUT byVal
IF ackNak = 1 THEN.... (check to see if ACK is correctly received, OPTIONAL)
Thanks.