I2C slave


Closed Thread
Results 1 to 4 of 4

Thread: I2C slave

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107

    Default I2C slave

    I'm trying to make an I2C slave using some code I found in this forum originally
    written for the 16F series.

    I'm using an 18F8722 and as you can see, I'm trying a "loopback" technique where
    I'm sending data using I2CWRITE and trying to receive it using the hardware I2C port.

    PORTF.3 is connected to PORTC.4 and
    PORTF.5 is connected to PORTC.3

    Both have 4.7K pull-ups.

    Some LEDs are connected to PortD. The code loops, but PIR1.3 is never set.

    Does anyone know what I should try next? The code is below.


    SDAS VAR PORTF.3
    SCLS VAR PORTF.5


    TRISC = %10111111
    TRISD = %00000000
    TRISF = %11111111

    ADCON1 = 0 ; No analog


    HSEROUT ["Alive",13,10]

    PORTD = 0 ' Shut off all LEDs

    SSP1STAT.7 = 1 ' Low speed mode
    SSP1STAT.6 = 0 ' Disable SMBbus-specific inputs


    ' Define used register flags
    SSPIF VAR PIR1.3 ' SSP (I2C) interrupt flag
    BF VAR SSP1STAT.0 ' SSP (I2C) Buffer Full
    R_W VAR SSP1STAT.2 ' SSP (I2C) Read/Write
    D_A VAR SSP1STAT.5 ' SSP (I2C) Data/Address
    CKP VAR SSP1CON1.4 ' SSP (I2C) SCK Release Control
    SSPEN VAR SSP1CON1.5 ' SSP (I2C) Enable
    SSPOV VAR SSP1CON1.6 ' SSP (I2C) Receive Overflow Indicator
    WCOL VAR SSP1CON1.7 ' SSP (I2C) Write Collision Detect



    ' Define VARs and types

    datain VAR BYTE ' Data in
    counter1 VAR BYTE
    I2Caddress VAR BYTE

    ' Initialize I2C slave mode

    I2CAddress = $0

    SSPADD = I2caddress ' Set our address
    SSPCON1 = %00110110 ' I2C slave with 7-bit address
    ' PIE1.3 = 1 ' SSP (I2C) interrupt enable '- don't need Ints for this test
    ' INTCON.6 = 1 ' PEIE = Peripheral Interrupt Enable bit
    ' INTCON.7 = 1 ' GIE enable
    SSPIF = 0
    SSPSTAT = $00



    mainloop:

    HSEROUT ["."] ' Indicate we are doing something

    IF SSPIF THEN
    SSPIF = 0 ' clear the flag
    datain = SSPBUF
    SSPBUF = datain
    CKP = 1
    GOSUB Blink5 ' let me know that i2c receive occurred
    ENDIF


    I2CWRITE SDAS,SCLS,$0,$0,[$55] ; Send some data, any data
    PAUSE 2000

    GoTo mainloop ' Do it forever

    Blink5:
    For counter1 = 1 to 5
    PORTD = $FF
    PAUSE 100
    PORTD = 0
    PAUSE 100
    NEXT Counter1
    RETURN


    END
    Charles Linquist

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Charles,
    If I get this correctly you are sending out and receiving on the same PIC . . .
    MeThinks that isn't going to work because by the time it gets around to receiving
    the info the output has already sent it. . . Now I maybe totally wrong. Have you tried using 2 PICs?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    I am sending and receiving on the same PIC. I'm sending using PBP bit-banging and I'm receiving using the hardware. Unless I'm mistaken, when I send a byte out using I2CWRITE, the hardware should grab it and set PIR1.3.

    Anyway, I have done the same thing using SEROUT and HSERIN. That works.

    BTW: I noticed that I had ADCON1 set to "0". I changed it to $FF (all digital) and it still doesn't work.
    Charles Linquist

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Charles,
    I never thought that would work, ' have to give that a try.
    Cheers
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  2. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  3. Another I2C Slave Routine Problem
    By DanPBP in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th February 2009, 05:50
  4. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  5. Please help with i2cslave i2c slave
    By cycle_girl in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 1st December 2005, 13:55

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts