+ Reply to Thread
Results 1 to 3 of 3

Thread: Writing to external IC via I2C bus

  1. #1
    Tamil.selvam Newbie
    Join Date
    Jul 2006
    Posts
    8

    Default Writing to external IC via I2C bus

    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


  2. #2
    justDIY Good justDIY Good
    Join Date
    Mar 2005
    Location
    Michigan, USA
    Posts
    1,725

    Default

    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"
    If you don't have a planet, what good are gold bars?

    want to contact me directly? gmail gordonthree
    check out my project website: http://projects.dimension-x.net
    Favorite numbers:
    09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

  3. #3
    Tamil.selvam Newbie
    Join Date
    Jul 2006
    Posts
    8

    Default

    Hi justDIY,

    Thank you for your time and advice.

    Regards
    Tamil

+ Reply to Thread

Similar Threads

  1. external interrupt
    By neelam29 in forum Micro Controllers
    Replies: 4
    Latest: 11th March 2006, 01:50 PM
  2. external interrupt frequency counter pic
    By neelam29 in forum Micro Controllers
    Replies: 7
    Latest: 6th March 2006, 01:20 PM
  3. external crystal vs. internal timer/wdt
    By Steve P in forum Micro Controllers
    Replies: 7
    Latest: 29th August 2005, 07:54 PM
  4. ? on external oscillators
    By low_byte in forum Micro Controllers
    Replies: 5
    Latest: 21st March 2004, 11:37 PM
  5. Serial Communicatoin + External Interrupt
    By huanyong in forum Micro Controllers
    Replies: 2
    Latest: 20th October 2003, 07:26 AM

Tags for this Thread