Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Writing to external IC via I2C bus

Status
Not open for further replies.

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
 
it looks like you're using Proton Basic ... I recommend you use the compound form of the HBUSIN and HBUSOUT commands, which lets compiler generate all the handshaking code for you.

Also, the SDA and SCL declares are for the software i2c emulation, they don't apply to the hardware i2c (hbus) command.

the code you're using looks ok for the device you're working with, but sometimes doing the handshaking by hand is tricky... you paid good money for a high level compiler, let it do the work for you :)

Oh, use some sort of debounce routine or the built in button command for reading your switch ... reading it straight from a port like that in basic is very "twichy"
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top