Have fun
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.
I have one chip as input one as outputs.
Chris