PDA

View Full Version : how to get PCF8574 and PIC16F876A work together



nomada
- 13th December 2004, 23:24
Hi, can anyone help me?

I need to expand the i/0 capabilities of a PIC16F876A and the usual (I supose) is to use an i/o expander like the Philips PCF8574 (or other Manufacturer/Type).

As I am a newcomer to the art of programing microcontrollers I've searched the forum for instances of I2C* and found a lot of usefull information on how to use I2C comunication and related devices.

However, when I tried to use "the real stuff" I found myself in a dead end and the reason probably resides in the errors of understanding the Philips datasheet about PCF8574 and/or the lack of programming skills.


Only for test purposes I've tried to "marry" the PIC16F876A and the PCF8574 with a small PBP program intended to switch on and off 8 leds, PCF has latched outputs, the code looks like this:

DEFINE OSC 10
ADCON1=7
DEFINE I2C_SLOW

TRISA=%00000001

SCL VAR PORTA.1 ' 4k7 pull-up
SDA VAR PORTA.0 ' 4k7 pull-up


start:

I2CWRITE SDA,SCL,%01000000,[%11111111] leds ON
PAUSE 5000

I2CWRITE SDA,SCL,%01000000,[%00000000] 'leds off
pause 5000

goto start

end


Ok! At this moment you can lough if after reading the code above I think it could work!


I´ve watched the PIC pins with my osciloscope and found that each five seconds both SDA and SCL generates a " signal burst" to ground.

I realise that something is wrong with the code (or the way I'm missing something in the PCF datasheet) because the leds remain as they came from the store and even when I disconect the leds I found (with osciloscope) that the logic level present at PCF i/o pins is somewhere between low and high and at each 5sec they reach high logic level.

Sorry about my english, thanks for reading
NOMADA

mister_e
- 14th December 2004, 00:51
before i read datasheet, can you modify your code as this


DEFINE OSC 10
ADCON1=7

SCL VAR PORTA.1 ' 4k7 pull-up
SDA VAR PORTA.0 ' 4k7 pull-up
Cont VAR BYTE
Cont = %01000000

start:

I2CWRITE SDA,SCL,Cont,[%11111111] 'leds ON
PAUSE 5000

I2CWRITE SDA,SCL,Cont,[%00000000] 'leds off
pause 5000

goto start


1. I remove I2C_SLOW define... not sure you need it.

2. i remove TRISA define. SDA is suppose to be bidirectionnal (if you read/write from external device), but you assign PORTA.0 (SDA) as an input so, when you trying to write to external, it's not sure that it will work as you request...

3. I add specific Variable for the control byte. It refer to the PBP book and this thread http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=587&highlight=I2CREAD

4. Also, be sure you didn't swap SCL & SDA pins too. If it doesn't work, some registers might be initialize... let me know, i'll take a look to datasheet.

nomada
- 15th December 2004, 00:49
Thanks mister_e

I implemented your sugestions but even though the PCF refuses to work.

I also started to suspect of PCF sample chip I received from Philips distributor, because is not normal the logic level never reaches 0 (ground) even without pull-up resistors.

So... I bought a new chip and... VOILÁ

I suppose that sample chips are like PORT WINE, some are good and some aren't.

Sometimes, no matter how much time you have spent in you life dealing with electronics,always keep forgetting that it may be some problem with materials. I need to pay more attention to this kind of things.

Once again thanks a lot for your kind assistance and ... please accept my season's greetings

NOMADA

mister_e
- 15th December 2004, 02:07
Hi nomada,

great to know everything's working good now. Yep it happen sometimes some SAMPLES can be burn or defect... as new one too. We often think, and it's normal, news part will work... This is why i always buy more than 5 to be sure of everything... but it can be also a batch problem... i never see it for now but, for sure it can happen.

Pleas accept my season's greeting, merry christmas, happy new year, Joyeux Noel, all the best for the years to come, etc :)