PDA

View Full Version : MCP23016 I/O expander



Demon
- 21st October 2005, 05:02
I am trying to wire an MCP23016 as output; driving LEDs. No matter what I try, no LEDs light up. Any idea what I am missing?

---------------------------------------------------------------
'define LOADER_USED 1
ADCON1 = 7 ' A/D off, all digital

ASM
@ DEVICE PIC16F877, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, PROTECT_OFF
ENDASM

define OSC 20

DEFINE LCD_DREG PORTA ' Set LCD data port
DEFINE LCD_DBIT 0 ' Set starting data bit
DEFINE LCD_RSREG PORTA ' Set LCD register select port
DEFINE LCD_RSBIT 4 ' Set LCD register select bit
DEFINE LCD_EREG PORTB ' Set LCD enable port
DEFINE LCD_EBIT 3 ' Set LCD enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size
DEFINE LCD_LINES 4 ' Set number of lines on LCD
define LCD_COMMANDUS 2000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds

DEFINE I2C_SLOW 1 ' Access I2C device above 8MHz
'DEFINE I2C_SCLOUT 1 ' Set I2C clock line as bipolar
DEFINE I2C_HOLD 1 ' Permit I2C device to hold clock line low

I2CSDA VAR PortC.4 ' Data line
I2CSCL VAR PortC.3 ' Clock line
I2CCTRL VAR BYTE ' I2C chip address
I2CLOC VAR BYTE ' I2C location byte
I2CBYTE1 VAR BYTE ' I2C 8 bit data string
I2CBYTE2 VAR BYTE ' I2C 8 bit data string

GOTO START

ERRORIO:
LCDOUT $FE,$94,"No ACK from I2C device"
RETURN

START: PAUSE 2000 ' Wait for LCD to initalize
LCDOUT $FE,1,"Program start"

' I2C device address: ID A2 A1 A0 R/W
' ---- -- -- -- ---
' 0100 0 0 0 0

I2CCTRL = %01000000
I2CLOC = $06 ' Set location to IODIR register pair
I2CBYTE1 = %00000000 ' Set pins 0-7 as output
I2CBYTE2 = %00000000 ' Set pins 8-15 as output
I2CWRITE I2CSDA,I2CSCL,I2CCTRL,I2CLOC,[I2CBYTE1,I2CBYTE2],ERRORIO
PAUSE 2000

I2CCTRL = %01000000
I2CLOC = $00 ' Set location to GPIO register pair
I2CBYTE1 = %00101010
I2CBYTE2 = %00000000
I2CWRITE I2CSDA,I2CSCL,I2CCTRL,I2CLOC,[I2CBYTE1,I2CBYTE2],ERRORIO

Finish:
LCDOUT $FE,$D4,"Process complete"
end
---------------------------------------------------------------


Yeah, I just noticed an error in my I2CWRITE comment. I meant pins in the sense of logical pins(GP0.0-GP0.7 and GP1.0-1.7).

I also did some trial & error with the I2C DEFINES, but no luck there. I even increased the PAUSE in case. The program terminates properly, so it seems as if I do get some sort of acknowledgement from the device.

I've triple-checked the wiring a zillion times. I followed the datasheet and placed the RC as specified.

I'm stumped...

Thanks!

Robert
:(

Melanie
- 21st October 2005, 18:28
I don't see you initialising this chip. Last I checked it had twelve registers... let's set them up the way you'd like to see them.... then and only then you can perform I/O.

Have you remembered to GROUND A2, A1 and A0 to match your IOCCTRL Byte?

Have you remembered to leave TP floating?

Have you remembered to GROUND all THREE Vss pins?

Have you remembered Rext and Cext for your external Clock, or alternatively cross-connected it to Fosc of your PIC?

Demon
- 21st October 2005, 21:14
"I don't see you initialising this chip. Last I checked it had twelve registers... let's set them up the way you'd like to see them.... then and only then you can perform I/O."

The default values for all the registers seem to be what I want except for the IODIR register pair. According to the datasheet, we are able to write to both registers in a single write operation. The 1st I2CWRITE is supposed to write all ZEROES to both registers (IODIR0 and IODIR1):

I2CCTRL = %01000000
I2CLOC = $06 ' Set location to IODIR register pair
I2CBYTE1 = %00000000 ' Set pins 0-7 as output
I2CBYTE2 = %00000000 ' Set pins 8-15 as output
I2CWRITE I2CSDA,I2CSCL,I2CCTRL,I2CLOC,[I2CBYTE1,I2CBYTE2],ERRORIO

----------------------------------------------------------------------

"Have you remembered to GROUND A2, A1 and A0 to match your IOCCTRL Byte?"

Yup.

----------------------------------------------------------------------

"Have you remembered to leave TP floating?"

Yup.

----------------------------------------------------------------------

"Have you remembered to GROUND all THREE Vss pins?"

Yup.

----------------------------------------------------------------------

"Have you remembered Rext and Cext for your external Clock, or alternatively cross-connected it to Fosc of your PIC?"

Yup, on the RC connection. I wanted to try your 2nd suggestion, but I can't see an Fosc pin on the 16F877.

----------------------------------------------------------------------

I've even connected all 16 I/O pins to LEDs, so I can easily see what is happening.

Robert
:(

Demon
- 22nd October 2005, 15:05
Is there any particular reason why MicroChip would NOT include in the datasheet for the MCP23016 a little diagram of pull-up resistors on the data and clock lines?

Who knows how long this has been working...

Robert
:(

Demon
- 22nd October 2005, 16:29
Hmm, I have a feeling the pull-up resistor is more a requirement for the PIC 16F877 than for the MCP23016...

Robert
:)

Melanie
- 22nd October 2005, 17:00
4K7 pull-up's ARE required on the I2C lines.

NavMicroSystems
- 22nd October 2005, 18:23
The I2C-BUS SPECIFICATION (http://www.semiconductors.philips.com/acrobat_download/literature/9398/39340011.pdf)

Demon
- 22nd October 2005, 20:13
Hmm, ok. I've gone through the datasheet more times than I can remember, but I never saw any mention of PU resistors on the clock/data lines. Is there any mention in it? Or are we 'expected' to know better?

The only reason I thought of them is from reading the PBP Pro manual.

Robert
:)

Melanie
- 22nd October 2005, 20:38
If you put a scope on your I2C Bus, you would have realised the waveform didn't meet the specification of Table 2-5 in the Datasheet. Some devices do not require PU Resistors, PICs do... the clue could also have been found in the PBP manual I2C commands where an example schematic is given connecting to an external EEPROM. 4K7 is a good value when connecting one or two slave devices with a relatively short track between them. You need to drop this value for more devices or greater track capacitance. Refer to Raph's pointer to the I2C Bus Spec.

Demon
- 23rd October 2005, 01:15
Yup, like I said, that's exactly where I found the answer.

Things worked great, until I got cocky. :D

Yeah, I also noticed that more devices screws up stuff. At least I have Ralph's link to help me out to figure the correct resistance value.

Robert
:)

Demon
- 23rd October 2005, 02:49
Hmm, still getting unreliable results.

Sorry, but the oscilloscope readings are still just a tad over my head. I was hoping to be able to diagnose this problem with more conventional techniques (if I can).

I placed my multimeter on 2000pF, the only pF setting I have, plugged 2 wires in the built-in holes (for capacitors) and then tested from SDA pin on PIC up to SDA pin on MCP.

I get 30pF, but now I realize that the wires I'm using could very well affect the reading. The probes only test A, V, Ohms and Hz, so I can't use those.

I had used 1.5K pull-up resistors and that had helped a lot. But it still has a way to go for reliable performance. I'll do as mentionned above and lower the resistor slowly and see if things improve.

Robert
:)

Demon
- 23rd October 2005, 02:55
One note about this circuit:

- PIC 16F877 mcu
- triple MCT6 opto-couplers
- 24AA512 eeprom
- dual MCP23016 (one used as input, one as output)

I use the MCT6 to switch between the SDA lines on the I2C devices. I used both sides of the MCT6 per device to permit bidirectional transfer.

The SCL line is shared amongst all the I2C devices.

I separated the input and output MCP23016 because I plan to have 8 of each later. I am only using one now to test the system.

Robert
:)

Demon
- 23rd October 2005, 03:11
A picture is better and easier to spot the mistakes.

Robert
:)

Demon
- 23rd October 2005, 03:29
Ok, I went down to 1K and things seemed worse. So I went the other way and tried 2.2K with perfect results.

The part that sucks about this is that the breadboard and PCB and NOT going to have identical capacitance. I may not know what I'm talking about, but I know that much. :D

It sure is nice to see the circuit act as it should though. At least now I have an idea of where to look if things act up when I print this circuit. Some trial&error with resistors at worse, if I'm lucky I'll learn how to use an oscilloscope properly by then.

Robert
:)

NavMicroSystems
- 23rd October 2005, 12:20
Robert,

you should really read the I2C Specs.

Your schematic will never work.
The SDA Line is Bidirectional!

I2C is a BUS, so why separating the SDA lines
if there are no more than 8 devices of the same type on the same bus?

If (for what reason ever) you want to isolate the bus use THIS (http://www.picbasic.co.uk/forum/attachment.php?attachmentid=253&d=1112646124) schematic.

Attached is a schematic that should work for you.

mister_e
- 23rd October 2005, 12:27
Heille salope, mon e-mail vidéotron ma chié dans les mains alors si tu essaie de me contacter c peine perdue.. j'attends encore après eux... HouHou service a la clientele ;)

Anyways. That's a strange design you have. What do you expect from your Opto-Coupler? A short-long history will be appreciated... at least for me the slow single brain cell.

Usually all SDA & SCL lines are connected together without anything else and use 2 pull-up resistors. that's it, that's all, no preservative, no aspartame, fat free. As usual, 0.1uF ceramice close to each I2C device is highly recommended.

On a design i'm working on NOW, i have 1 X PCF8574, 4 X 24LC64 on the same I2C bus and i use 4.7K without problems... as now

It appear in some case, on certain board design, that some I/O on certain PORT react weird depending the speed you mess with. Sometime reducing speed help. I can't remember on wich PIC i had a weird issue but i choose 2 I/O that are not one next to the oher (B.0, B1..) and it cure the problem. Seems to be another Read-Modify-Write issue.


__________________________________________________ ___________________________
EDIT: Sorry Ralph, didn't notice your post... as you know, i'm still trying to deal with my emotions since you know why ;)

mister_e
- 23rd October 2005, 12:55
Ralph,
maybe i'm the ony one or my computer miss some internet plug-in, but i can't see your schematic... are you using P82B96 or any other Philips in the PCA951X family?

I ask you that because my next design (on wich i'll probably spend ... few months maybe a year on), i'll have to deal with around 100 (maybe more) of those hot-swap I2C buffer to protect the main frame while the user change/add/swap control slots/modules.

Sorry Robert to be off topic :(

Demon
- 23rd October 2005, 13:29
Robert,

Hi Ralph. :)


you should really read the I2C Specs.

I did, that's where I got the idea to go down to 1.5K for the pull-up resistors. Page 40somethings had interesting values.


Your schematic will never work.

Wanna bet? :lol: It's working perfectly.

- I pull the MCT6 for the eeprom high.
- I can read data from memory and display it on an LCD.
- I pull the MCT6 back low.
- I pull the MCT6 for the output MCP23016 high (the first one).
- I can turn on whichever LEDs are identified on the eeprom data.

I am now in the process of activating the 2nd MCP23016 which will be used as input.


The SDA Line is Bidirectional!

I know, I said that on post #12.


I2C is a BUS, so why separating the SDA lines
if there are no more than 8 devices of the same type on the same bus?

I know. I gave notes on my circuit on post #12 to explain my reasoning. Right now I'm testing with one input and one output MCP23016, but I plan on having 8 input and 8 output later. Actually, I'm going to have even more than that. I'm going to add an MCT6 for each additionnal 'network' of 8 MCP23016 devices.


If (for what reason ever) you want to isolate the bus use THIS (http://www.picbasic.co.uk/forum/attachment.php?attachmentid=253&d=1112646124) schematic.

Link no workie, red X.


Attached is a schematic that should work for you.

No workie either.

Robert
:)

Demon
- 23rd October 2005, 13:54
Ralph,

A little more on using the MCT6 as a bidirectional switch in this circuit.

Note how the wiring to the eeprom is connected to the MCT6. A single wire from the eeprom is wired twice in opposite manner on a single MCT6.

Here, the best way to explain myself is to make a drawing.

Robert
:)

Demon
- 23rd October 2005, 13:58
Heille Steve, ouais, je t'ai envoye plusieurs emails sur Videotron. C'etait pas mal la meme chose que j'ai demande icitte, avec un peu de bla-bla.

(maudit clavier anglais, pas d'accents sur ce PC)

Robert
:)

mister_e
- 23rd October 2005, 15:09
Mr Demon,
Even if i don't see any advantage and/or need for the Optos, it also introduce, at least, a voltage drop between The PIC and the I2C device. No matter why it's working so so.

Schematic provided by Ralph will work. You just need to modify your I2CWRITE/I2CREAD statement to fit with the A0, A1, A2 jumpers.

On your SCL and SDA line, you should be able to fit all your I2C devices. As i remind MCPXXX don't have the same control word than your EEPROM so it shouldn't provide any conflict.

Qu'a Pitch?

NavMicroSystems
- 23rd October 2005, 16:06
Link no workie, red X.

No workie either.

Robert
:)

Rightclick --> Save Target As . . . .

NavMicroSystems
- 23rd October 2005, 16:29
I separated the input and output MCP23016 because I plan to have 8 of each later. I am only using one now to test the system.

Robert
:)

Since the MCP (like most other I2C Devices) has 3 address bits you can have up to 8 devices of the same type sharing one bus.

For more than 8 (up to 16) devices of the same type share SCL for all of them and have separate SDA for each set of 8.
So to communicate with 16 Devices of the same type you need three Ports on the PIC.

I don't use the MCP, I prefer the PCF8574.
There is an "A" version available that has a control-byte different from the "non A" version, so you can have up to 16 (8x"A" & 8x"non A") devices sharing one pair of SDA cnd SCL lines.

NavMicroSystems
- 23rd October 2005, 16:33
Ralph,
maybe i'm the ony one or my computer miss some internet plug-in, but i can't see your schematic... are you using P82B96 or any other Philips in the PCA951X family?

I ask you that because my next design (on wich i'll probably spend ... few months maybe a year on), i'll have to deal with around 100 (maybe more) of those hot-swap I2C buffer to protect the main frame while the user change/add/swap control slots/modules.

Sorry Robert to be off topic :(

Steve,

the schematic is tested and working fine,
the only thing a have changed in my final design are the opto's, the 6N139 was too slow for "High-Speed" I2C

NavMicroSystems
- 23rd October 2005, 17:04
Here, the best way to explain myself is to make a drawing.

Robert
:)

Robert,

I'm sorry, this still doesn't make any sense to me.
Why use opto's when you aren't isolating anything?

Steve,

this would be an application for the 4066's we still have on the shelf ;-)

Demon
- 23rd October 2005, 17:47
Mr Demon,

What is this Mister talk? I hope you're not doing the "vous" thing, 'cause if you are and you're just doing this to respect your elders, then I'm going to have to stroll over and hit you about the shoulders with my cane.

:P


Even if i don't see any advantage and/or need for the Optos, it also introduce, at least, a voltage drop between The PIC and the I2C device. No matter why it's working so so.

I forgot about that.


Schematic provided by Ralph will work. You just need to modify your I2CWRITE/I2CREAD statement to fit with the A0, A1, A2 jumpers.

I don't have those arts on hand at the moment.

:(


On your SCL and SDA line, you should be able to fit all your I2C devices. As i remind MCPXXX don't have the same control word than your EEPROM so it shouldn't provide any conflict.

Steve, I want to be able to have 8 MCP23016 for input, and another 8 for output, all on a single PIC. That's why I tried the MCT6 switch idea.


For more than 8 (up to 16) devices of the same type share SCL for all of them and have separate SDA for each set of 8.

Yeah, but the PIC 16F877 only has one SDA/SCL pair of pins. Is there another PIC that has multiple SDA lines (I didn't notice this feature on the MicroChip product list)?

Robert
:)

Demon
- 23rd October 2005, 17:51
Robert,

I'm sorry, this still doesn't make any sense to me.
Why use opto's when you aren't isolating anything?

Ralph,

The optos were used only as a means of being able to switch between more than 8 MCP23016. I can easily activate the MCT6 I wish with a single pin from the PIC. Don't look for any fancy reasons, there aren't any.

It was the only idea I could come up with the have more than 8 MCP23016 share a common SDA line (since I don't know how to have more than 1 SDA line per PIC).

Robert
:)

NavMicroSystems
- 23rd October 2005, 18:02
Yeah, but the PIC 16F877 only has one SDA/SCL pair of pins. Is there another PIC that has multiple SDA lines (I didn't notice this feature on the MicroChip product list)?

Robert
:)

Robert,

PBP's I2CWRITE / I2CREAD is "Software I2C" (not using the MSSP-Module), so you can use ANY I/O Pin.

See PBP Manual Section 5.34 & 5.35

Demon
- 23rd October 2005, 18:02
Awe ben tabarnoune!

Ralph, one of your comments made it seem like all I had to do was to use another pin as an SDA and just go on from there. So that's what I just tried and the darned thing works!

Ok, that's great. But why is the SDA line listed on a particular pin on the 16F877 datasheet?

I thought that since it was listed on RC4 that THAT was the only place it would work. Hence why I tried all these acrobatics to work with this limitation.

This brings up another question; will SDA work on all pins?

Robert
:)

Demon
- 23rd October 2005, 18:04
Ok, just saw your post come up.

Thanks very much Ralph and Melanie.

I was going to say thanks to that Mister_E comedian, but he's ignoring my emails. :P

Robert
:D

mister_e
- 23rd October 2005, 19:17
Dear vieux debris
Glad to see that there's some light up your head now. I2CWRITE/I2CREAD/SHIFTOUT.... SERIN/SEROUT... will work on any I/O since it's a software solution as Ralph said. It isn't the same as HSEROUT and HPWM wich use the specific PIC i/o.

The beauty of PBP against another Basic compiler is that you can use/do as many I2C bus as you have free I/O. Hey, even if we still heard here and there that PBP is a dead/dummy/..../...../..../ language, it's still usefull :)

Usually i don't share SCL line between I2C buss... kind of personnal safety feeling i guess... but it's working.

mister_e
- 23rd October 2005, 19:23
Steve,

this would be an application for the 4066's we still have on the shelf ;-)

Hehe CMOS blast from the past but still handy to have on-hand ...4051,4053,4066,4067 if my memory is still good. You know Ralph how i feel since that evenement... snif snif :(

Acetronics2
- 23rd October 2005, 19:59
Hi Steve

just forgotten 4016, 4052, 4097 , 4519 , 4554 ,and 4555...

due to age ????

Hi,hi

Alain

mister_e
- 23rd October 2005, 20:40
Oh at least caused by the age!!!

When you'll have my age, if you leave long enough, you'll understand :)

Of course nobody can't expect to be as old as Robert, but it's better like that after all.

O.K. not a chat room here!

Demon
- 23rd October 2005, 21:21
Steve, no money for you when I win the 6/49. Of course that means I have to buy a ticket first... :D

Ralph, can I ask why you are using those chips? Can you give a very brief description what is happening please?

Robert
:)

Demon
- 23rd October 2005, 21:48
Ok, I'm trying to read from the 2nd MCP23016 and it's not working smoothly. I display the word on an LCD, and bits seem to flicker sometimes. When I press a button, the corresponding bit turns on, but there are some 'free games' here and there.

Taken from page 15 of MCP23016 datasheet:

To read an MCP23016 register, the Master needs to follow the requirements shown in Figure 1-6. First, the
device is selected by sending the slave address and
setting the R/W bit to logic ‘0’. The command byte is
sent after the address and determines which register
will be read. A restart condition is generated and the
device address is sent again with the R/W bit set to
logic ‘1’. The data register defined by the command
byte will be sent first, followed by the other register in
the register pair.

This is what I am doing, a simple loop:

CYCLE:
' Set R/W bit to 0.
I2CCTRL = %01000000
I2CLOCB = $00
I2CWORD = %0000000000000000
I2CREAD I2CSDA,I2CSCL,I2CCTRL,I2CLOCB,[I2CWORD]
' Set R/W bit to 1.
I2CCTRL = %01000001
I2CREAD I2CSDA,I2CSCL,I2CCTRL,I2CLOCB,[I2CWORD]
LCDOUT $FE,$94,BIN16 I2CWORD
GOTO CYCLE

I've removed the MCT6 and use a separate SDA line pulled up with 4.7K for each I2C device.

Did I misunderstand the instructions?

Robert
:)

NavMicroSystems
- 23rd October 2005, 22:01
Ralph, can I ask why you are using those chips?
Robert


There are several reasons,
- more flexibility due to one interrupt per 8bits
- more flexibility in PCB Layout
- Price and availibility over here
...

Image (http://www.picbasic.co.uk/forum/attachment.php?attachmentid=136&d=1101334552) of one of my universal 16bit I2C I/O boards

NavMicroSystems
- 23rd October 2005, 22:03
Ok, I'm trying to read from the 2nd MCP23016 and it's not working smoothly. I display the word on an LCD, and bits seem to flicker sometimes. When I press a button, the corresponding bit turns on, but there are some 'free games' here and there.


Robert,

we don't want to guess anymore, please post your full code and schematic.

Demon
- 23rd October 2005, 22:15
'define LOADER_USED 1
ADCON1 = 7 ' A/D off, all digital

ASM
@ DEVICE PIC16F877, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, PROTECT_OFF
ENDASM

define OSC 20

DEFINE LCD_DREG PORTA
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTA
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
define LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50

DEFINE I2C_SLOW 1
DEFINE I2C_HOLD 1
I2CSCL VAR PortD.1
SDAISW1 VAR PortC.2

LOCGPIO CON $00
LOWWORD CON %0000000000000000

I2CCTRL VAR BYTE
I2CLOCB VAR BYTE
I2CWORD VAR WORD
CTRLIOW CON %01000000
CTRLIOR CON %01000001

GOTO START ' Bypass subroutines

'--------------------------- SUB-ROUTINES ----------------------------

RDISW1:
I2CREAD SDAISW1,I2CSCL,I2CCTRL,I2CLOCB,[I2CWORD]
PAUSE 12
RETURN

'-------------------------- MAIN PROGRAM -----------------------------

START:
PAUSE 2000
LCDOUT $FE,1,"Program start"

CYCLE:
I2CCTRL = CTRLIOW
I2CLOCB = LOCGPIO
I2CWORD = LOWWORD
GOSUB RDISW1
I2CCTRL = CTRLIOR
GOSUB RDISW1
LCDOUT $FE,$94,BIN16 I2CWORD
GOTO CYCLE

END


Circuit coming up.

Robert
:)

Demon
- 23rd October 2005, 22:31
Circuit:

Robert
:)

Demon
- 23rd October 2005, 22:42
I tried reading from the INTCAP instead of the GPIO with much better results.

Got more tests to do.

EDIT: Nope, not that, nevermind...

Robert
:)

NavMicroSystems
- 23rd October 2005, 22:49
Robert,

As I'm not using the MCP23016 I haven't read the entire datasheet.

But looking at your schematic there is one thing that may cause strange effects:

you have switches to VDD, but no PullDown Resistors.
So when the switches are open the MCP's inputs are floating.

(I would prefer to have the switches connected to GND and have PullUps to VDD)

I

Demon
- 24th October 2005, 01:01
ARGH!

Thanks Ralph! I can't believe I missed that. Pull-up resistors is one of those things that I watch out for in all switches.

But I've been so busy learning new stuff on this circuit that I've totally forgotten what I consider to be a basic thing. I was so sure I had misunderstood something in the MCP23016 datasheet, or wired an I2C device improperly, a pull-up resistor never dawned on me.

The circuit works beautifully now.

Robert
:)