This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PCF8574 source code

Does anybody have working example of driving PCF8574A for at89c2051?

I'm trying to blink a LED almous about 6 hours with a zero result... :-(

SDATA on P1_6, SCLK on P1_7

how to turn on/off LED on PCF A0 ?

Thanks...

  • Have you looked on the Pilips site for examples/Application Notes?

    Try putting "PCF8574" into your favourite internet search engine.

    There's an PCF8574 example near the bottom of this page: http://members.home.nl/bzijlstra/software/examples/asmex.htm

  • Hi,

    This very short routine bit-bangs a PCF8574 at address 0x70 (make sure yours is at that address, they have two different versions: 8574AT and 8574T with different addresses, and each has 8 hardware selectable ranges)! You can change the IIC slave-address, its a define in the code. The byte to be sent is also hard-coded, but could just as easily be sent to the routine along an address.

    The code will send an 8-bit value of 0xFEH to address 0x70H, and will work on any 8051.

    Just define SDA and SCL as open collector outputs, and add 4.7KOhm pull ups to these two lines. You may have to add NOP's to the wait routine if your 8051 runs faster than the 87LPC764 I used.

    The code is only a "couple" of bytes long, and doesen't rely on lengthly IIC state-machines, any hardware units, timers, or special interrupts etc. It's small and dirty and gets the job done.

    ;****************************************************************************
    ; Write a byte to the IIC expander
    ;
    ; Copyright (C) 2004 Said Jackson - Equator Technologies, Inc.
    ;****************************************************************************

    write_to_IIC_expander:

    ; Initial IIC bus state
    ;
    setb SDA ; If no one else is driving the bus,
    setb SCL ; it is now idle.

    jnb SDA, $ ; wait until bus is idle.
    jnb SCL, $

    ; Generate start signal
    ;
    clr SDA
    acall wait_halfbit
    clr SCL

    ; the 8574 we use is at address 0x70H:

    mov A,#070h ; address of IIC expander
    acall send_byte ; send IIC address

    ; receiver should respond with ACK.
    ; this code does not currently verify that the ACK has been received

    acall wait_halfbit ; skip ACK
    setb SCL ; idle the clock line (pretend to read ack)
    acall wait_halfbit

    clr SCL ; start databyte


    ; this is the actual byte to be sent, in our example its 0xFEH:

    mov A,#0FEh ; clear bit 0 of IIC expander only


    acall send_byte ; send IIC databyte

    ; receiver should respond with ACK.
    ; this code does not currently verify that the ACK has been received

    acall wait_halfbit ; skip ACK
    setb SCL
    acall wait_halfbit

    ; send stop bit to end transaction
    ;
    clr SCL ; start sending stop bit
    clr SDA ; stop bit
    acall wait_halfbit
    setb SCL ; drop clock. signals receiver to sample data
    acall wait_halfbit
    setb SDA ; drop data. bus is now idle.

    ret

    ;****************************************************************************
    ; send an 8 bit word to IIC bus
    ;****************************************************************************

    send_byte:
    mov R0,#08h ; setup count

    next_bit:
    acall wait_halfbit
    rlc A ; rotate next bit into carry
    mov SDA,C ; set IIC data to carry value
    setb SCL ; set IIC clock
    acall wait_halfbit ; wait
    clr SCL ; drop clock. signals receiver to sample data
    djnz R0,next_bit ; do next bit

    setb SDA ; idle the data line
    ret

    ;****************************************************************************
    ; wait for one half IIC bit period (5us at 100KHz) with a 12MHz clock on an 87LPC764
    ;****************************************************************************

    wait_halfbit:
    nop
    nop
    nop
    nop
    nop
    ret

  • Thank's a lot for your help... My code was working too, my problem was: I'm not connected "earth" between microproc. and my i2c device (two separate power supply)

    :-)