PCF8574 I2C I/O

General discussion on mikroBasic.
Post Reply
Author
Message
Bereton
Posts: 19
Joined: 22 Mar 2005 23:34
Location: Telford UK
Contact:

PCF8574 I2C I/O

#1 Post by Bereton » 10 May 2005 18:43

I have been 'Playing' with the PCF8574 chip today if it is any help to anyone I have included my working code. If anyone needs a schematic I have it in PDF format.

Have fun :wink:

Code: Select all

program pcf8574I2C

dim  AddrO as byte                 'Address of Output Device
dim  AddrI as byte                 'Address of Input Device
dim  outdata as byte               'Data to be sent out
dim  Indata as byte                'Data received
dim txt as char[16]

sub procedure out_data
    I2C_start               ' issue I2C start signal
    I2C_wr(addrO)           ' send byte via I2C  (Address of output 8574)
    I2C_wr(outdata)         ' send data (data to be written)
    I2C_stop                ' issue I2C stop sinal
    delay_ms(100)
end sub

sub procedure in_data
    I2C_start                ' issue I2C start signal
    I2C_wr(AddrI)            ' send byte (address of input 8574)
    indata = I2C_rd(0)       ' Read the data
    bytetostr(indata,txt)    ' Data to String for display
    lcd_out(portd,1,1,txt)   ' Display data on LCD from input 8574
    I2C_stop                 ' issue I2C stop sinal
    delay_ms(100)
end sub

main:                        ' General setup
     delay_ms(10)             ' Let LCD settle before lcd_config
     TRISD =   0             ' designate portd as output
     PORTD =   0             ' initialize portd
     Lcd_config(portd,2,3,0,7,6,5,4)'  initialize LCD to portd
     lcd_cmd(portd,LCD_CLEAR)       '  send command to LCD "clear display"
     lcd_cmd(portd,LCD_CURSOR_OFF)  '  send command cursor off
     lcd_cmd(portd,LCD_CLEAR)
     AddrO =   $4e           ' Set Out add. to 4E (must be even)
     AddrI =   $41           ' Set In add. to 41 (must be odd)
     I2C_init(100000)        ' initialize I2C module 100 kHz clk, full master mode
     outdata = 0             ' zero out data
     txt = "PCF8574P Test"
     lcd_out(portd,2,1,txt)  '  print txt to LCD, 2nd row, 1st column
fred:
     inc(outdata)            ' Increment Outdata
     out_data                ' Run out_data sub procedure
      txt = "    "
      lcd_out(portd,1,1,txt) ' Blank out Read display
      delay_ms(5)
      in_data                ' Run in_data sub procedure
    goto fred

end.

For those that do not know this is an I2C interfaced 8 bit I/O chip that can be used as input or output. Three address lines and an interupt out (not used in this example)
I have one chip as input one as outputs.

Chris
Get your motor running

plaasjaapie
Posts: 51
Joined: 23 Aug 2008 05:12

Re: PCF8574 I2C I/O

#2 Post by plaasjaapie » 21 Aug 2011 16:31

I could use that schematic. :D

Post Reply

Return to “mikroBasic General”