Code for interfacing PCF8574 for LCD

Immaculan

New Member
Hi All,

This is a response to a code request from Jurjen posted in another thread.
As I had my laptop stolen I lost a lot of my source code, but not to fear I set up a new PCF8574 - LCD test bed this morning and came up with the working code shown below.
Bear in mind this is with the PCF8574 not the PCF8574A part.

Out 0 is wired to the E input of the LCD.
Out 2 is wired to the RS of the LCD.
I am using the full 8 bit data path of the PCF8574.
All 3 of the addresses are tied low to keep it simple.

EEPROM 1,("THis Is A TEst")

main:

i2cslave %01000000, i2cslow, i2cbyte
writei2c 0, (%00000001) 'Clear Command
low 2 'Set R/S Low pulsout 0,1 'Pulse E to send
pulsout 0,1 'Pulse E Again
high 2
pause 200
writei2c 0, (%00001100) 'Function set 8bit
low 2
pulsout 0,1
pulsout 0,1
high 2
pause 20
writei2c 0, (%00000001) 'Clear Command
low 2
pulsout 0,1
pulsout 0,1
high 2
pause 20

for b6 = 1 to 14 '14 is the length of my string
read b6,b2 'Read Mem into b2
writei2c 0, (b2) 'Write it to the PCF8574
high 2 'Set R/S High for data
pulsout 0,1 'Pulse E to write to lcd
low 2
next b6
 

evanh

Senior Member
Sorry to here you've lost so much work. :(

Had a bag full of a project stolen once, nothing of any value to them - just my time trying to reconstruct the drawings, notes and what not else ...

Evan
 

kranenborg

Senior Member
Hi Immaculan,

many thanks for repoducing the code and testing it! In this way we can really see how the part should be used. This is even more important as the PCF8574 seems to have a widespread use and appears to be easily available

As you use the following statement for output:

writei2c 0,(data),

this usage suggests that the address byte (equal to zero here) is actually used as a command byte to define all PCF8574 i/o lines as outputs.

Naturally the question pops up what to do if (part of) the i/o lines should be used as inputs as well; The Philips datasheet states that the particular lines should be written HIGH before data can be read. As I get it still these lines can also be used as outputs and are set HIGH (so-called 'quasi-bidirectional i/o)

Therefore let's imagine the following configuration for the pcf8574:

* i/o lines 5-7 to be used as inputs
* i/o lines 0-4 to be used as outputs

Then the following code snippet should work to read the three inputs in b0, while at the same time preserving the state of the outputs as reflected in register b1:

REM b0 register, will contain input in bits 5-7
REM b1 register, contains previously output data (and output settings should not be changed by this read operation)

b1 = b1 OR %11100000
readi2c b1, (b0)

Thus, since the readi2c command first writes the contents of b1 to the PCF8574 (thinking of it as an "address", but it is now used as an i/o direction command), i/o lines 5-7 are set as inputs (or equivalently as HIGH outputs), and their state is subsequently read into b0 and can be tested (the bit5, bit6 and bit7 variables). As bits 0-5 of b1 are not changed, the output lines remain unchanged as well. Note that *remembering' the output state in b1 is necessary when using the PCF8574 for combined input/output

Note that the effect of using

readi2c %11100000,(b0)

instead of

readi2c b1, (b0)

would be that any output lines set HIGH would be set LOW, which might not always be desirable.

I think the presented code for input should definately work; Although I have not seen any timing diagrams for the readi2c command, I have looked carefully at the 24lc256 timings for a sequential read (which can be applied with the readi2c command), and I am pretty confident that the code above applies it correctly. Still, somebody able to test this? (I just migrated to Sweden and currently do not have all tools unpacked/available)

A last remark; as noted by technical, the latest picaxes that support the readi2c command without addressbyte can read the pcf8574 directly ...

Again, many thanks for your efforts; I'll update my website on this matter very soon

Best regards,
Jurjen Kranenborg
http://www.kranenborg.org/ee/picaxe

 

Edited by - kranenborg on 8/7/2005 12:21:25 PM
 

Keith Smith

New Member
Hi Immaculan,
I was wondering which picaxe you are using, I have tryed to use the 18x (firmware 8.5) with the 8574, and I have had no success. My code dose not look a lot different to yours.
 

Immaculan

New Member
Hi Keith,

I am using an 18x with firmware 8.4
I had trouble with the PCF8574A part so used the PCF8574 and found this to work for me. The difference is in the slave address length. Also make sure your SCA and SCL lines are not mixed up and the pull up resistors are in place.
One thing I also did to make sure my LCD worked, (Salvaged from an old fax machine) was to hook it up to the 18x directly in 4bit mode and use the routines in the rev-ed manual. Once I had confirmed the LCD was indeed good i moved on to the more complex I2c stuff.
I hope this helps, If you need my circuit diagram I can email it to you.
Cheers
Jamie
 
Top