Tamil.selvam
New Member
Hi everyone....hope all is well.....I have two external I2C ICs (PCF8574P), lets call them X & Y. I'm need to WRITE data to X and READ from Y via an I2C bus. I can't seem to WRITE to X but I can READ from Y without any problem. Here is my code, I'm using a 16F877A.
Device 16F877A 'Device Type
XTAL = 20
ALL_DIGITAL = True 'Setting all inputs to digital
Declare SDA_PORT PORTC.4 'Ports for the I2C bus
Declare SCL_PORT PORTC.3
Declare HBUS_BITRATE 100 ' Bits per second
Dim ADC_Data As Byte 'ADC variables
Dim PIC_Out As Byte
Symbol BTN1 = PORTB.4 'Button ports
LCD_DTPIN = PORTD.4 'Initialising LCD
LCD_ENPIN = PORTE.1
LCD_RSPIN = PORTE.0
LCD_LINES = 2
TRISB = %00010000 ' Set PORTB 4 as input
PORTB_PULLUPS = On ' Pull-up Resistors for PortB Initiated
Main:
DelayMS 25
If BTN1 = 0 Then ' Check to see BTN1 is pressed
Clear PIC_Out ' clear Variable
HBStart ' Start Hardware Bus
HBusOut %01000010 ' Address of the target IC to which to WRITE
If SSPCON2.6 = %1 Then ' If there is no ack then print "no ack1"
Cls
Print "No Ack1"
DelayMS 500
Else
PIC_Out = %00000001 ' WRITE data 00000001 to target IC
HBusOut PIC_Out
Cls
Print "ACK OK" ' Print acknowlegment of completion of WRITE
DelayMS 500
End If
HBStop ' Stop Hardware data bus
Clear ADC_Data
HBStart
HBusOut %01000101 ' Address of the target IC from which to READ
If SSPCON2.6 = %1 Then ' If there is no ack then print "no ack"
Cls
Print "No Ack"
DelayMS 500
Else
ADC_Data = HBusIn ' Store READ data into ADC_Data
Cls
Print ADC_Data ' Print result
DelayMS 500
End If
HBStop
goto Main
When I press BNT1 the display comes up as "NO Ack1" then prints ADC_Data.
Can someone please let me know what I'm doing wrong. Thanks
Regards
Tamil
Device 16F877A 'Device Type
XTAL = 20
ALL_DIGITAL = True 'Setting all inputs to digital
Declare SDA_PORT PORTC.4 'Ports for the I2C bus
Declare SCL_PORT PORTC.3
Declare HBUS_BITRATE 100 ' Bits per second
Dim ADC_Data As Byte 'ADC variables
Dim PIC_Out As Byte
Symbol BTN1 = PORTB.4 'Button ports
LCD_DTPIN = PORTD.4 'Initialising LCD
LCD_ENPIN = PORTE.1
LCD_RSPIN = PORTE.0
LCD_LINES = 2
TRISB = %00010000 ' Set PORTB 4 as input
PORTB_PULLUPS = On ' Pull-up Resistors for PortB Initiated
Main:
DelayMS 25
If BTN1 = 0 Then ' Check to see BTN1 is pressed
Clear PIC_Out ' clear Variable
HBStart ' Start Hardware Bus
HBusOut %01000010 ' Address of the target IC to which to WRITE
If SSPCON2.6 = %1 Then ' If there is no ack then print "no ack1"
Cls
Print "No Ack1"
DelayMS 500
Else
PIC_Out = %00000001 ' WRITE data 00000001 to target IC
HBusOut PIC_Out
Cls
Print "ACK OK" ' Print acknowlegment of completion of WRITE
DelayMS 500
End If
HBStop ' Stop Hardware data bus
Clear ADC_Data
HBStart
HBusOut %01000101 ' Address of the target IC from which to READ
If SSPCON2.6 = %1 Then ' If there is no ack then print "no ack"
Cls
Print "No Ack"
DelayMS 500
Else
ADC_Data = HBusIn ' Store READ data into ADC_Data
Cls
Print ADC_Data ' Print result
DelayMS 500
End If
HBStop
goto Main
When I press BNT1 the display comes up as "NO Ack1" then prints ADC_Data.
Can someone please let me know what I'm doing wrong. Thanks
Regards
Tamil