Another I2C Slave Routine Problem


Closed Thread
Results 1 to 5 of 5
  1. #1

    Default Another I2C Slave Routine Problem

    Hello Guys,

    I spent a few days reading the forum, checking and double checking, and I hit a dead end...

    I have two PICs 16F88, one master and one slave... I'm able to tell the slave to do something, but I'm not able to read a byte from the slave...

    This is the master, pretty simple:

    Code:
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    ' Master
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    
    @ device pic16f88, intrc_osc_noclkout	' system clock options
    @ device pic16f88, wdt_on			' watchdog timer
    @ device pic16f88, pwrt_on			' power-on timer
    @ device pic16f88, mclr_off			' master clear options (internal)
    @ device pic16f88, protect_off		' code protect off
    
    '-------------------------------------------------------------------------------
    
    DEFINE OSC 8
    OSCCON = %01110001 '8mhz
    
    '-------------------------------------------------------------------------------
    
    CMCON = 7
    ADCON1 = 0 ' Disable A/D converter
    ADCON0 = 0
    ANSEL = 0 ' all analog pins to digital
    
    '-------------------------------------------------------------------------------
    
    
    DEFINE I2C_HOLD 1
    DEFINE I2C_SLOW 1
    
    scl VAR PORTB.4      ' i2c clock input
    sda VAR PORTB.1     ' i2c data input
    
    valor VAR BYTE      ' value to read
    LED VAR PORTA.6
    
    '-------------------------------------------------------------------------------
    
    main:
            valor = 0
    
    '        I2CWrite sda, scl, $01, [31]
    '        High LED
    '        Pause 100
    '        I2CWrite sda, scl, $01, [30]
    '        Low LED
    '        Pause 100
    
            I2CRead sda, scl, $01, [valor]
            
            IF (valor = 24) Then
               High LED
    	 Pause 500
                 Low LED
            EndIF
     
    		GoTo main
    
    '-------------------------------------------------------------------------------
    
    End
    
    '-------------------------------------------------------------------------------
    And, this is the slave:

    Code:
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    ' Slave
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    
    @ device pic16f88, intrc_osc_noclkout	' system clock options
    @ device pic16f88, wdt_on				' watchdog timer
    @ device pic16f88, pwrt_on				' power-on timer
    @ device pic16f88, mclr_off				' master clear options (internal)
    @ device pic16f88, protect_off			' code protect off
    
    '-------------------------------------------------------------------------------
    
    DEFINE OSC 8
    OSCCON = %01110001 '8mhz
    
    '-------------------------------------------------------------------------------
    
    CMCON = 7
    ADCON1 = 0 ' Disable A/D converter
    ADCON0 = 0
    ANSEL = 0 ' all analog pins to digital
    
    '-------------------------------------------------------------------------------
    
    DEFINE I2C_HOLD 1
    DEFINE I2C_SLOW 1
    
    '-------------------------------------------------------------------------------
    
    SSPIF VAR PIR1.3	' SSP (I2C) interrupt flag
    BF VAR SSPSTAT.0	' SSP (I2C) Buffer Full
    R_W VAR SSPSTAT.2	' SSP (I2C) Read/Write
    D_A VAR SSPSTAT.5	' SSP (I2C) Data/Address
    CKP VAR SSPCON.4	' SSP (I2C) SCK Release Control
    SSPEN VAR SSPCON.5	' SSP (I2C) Enable
    SSPOV VAR SSPCON.6	' SSP (I2C) Receive Overflow Indicator
    WCOL VAR SSPCON.7	' SSP (I2C) Write Collision Detect
    
    '-------------------------------------------------------------------------------
    
    I2Caddress CON 1	' Make our address 1
    
    '-------------------------------------------------------------------------------
    
    SSPADD = I2Caddress ' Set our address
    SSPCON = $36 		' Set to I2C slave with 7-bit address
    
    '-------------------------------------------------------------------------------
    
    LEDR VAR PORTA.3 ' LED 1
    LEDL VAR PORTB.3 ' LED 2
    BZR  VAR PORTA.7 ' Buzzer
    RL6  VAR PORTB.6 ' Relay 1
    RL12 VAR PORTB.5 ' Relay 2
    
    '-------------------------------------------------------------------------------
    
    datain VAR BYTE		' Data in
    dataout VAR BYTE	' Data out
    
    '-------------------------------------------------------------------------------
    
    GoTo boot
    
    '-------------------------------------------------------------------------------
    
    i2cslave: ' I2C slave subroutine
              SSPIF = 0
    
              IF R_W = 1 Then i2crd ' Read data from us
    
              IF BF = 0 Then i2cexit ' Nothing in buffer so exit
    
              IF D_A = 1 Then i2cwr ' Data for us (not address)
    
              IF SSPBUF != I2Caddress Then i2cexit ' Clear the address from the buffer
    
              GoTo i2cexit
    
    '-------------------------------------------------------------------------------
    
    i2cwr: ' I2C write data to us
    
           datain = SSPBUF ' Put data into array
    
           GoTo i2cexit
    
    '-------------------------------------------------------------------------------
    
    i2crd: ' I2C read data from us
    
           SSPBUF = dataout ' Get data from array                 
    
           CKP = 1 ' Release SCL line
    
           GoTo i2cexit ' That's it
    
    '-------------------------------------------------------------------------------
    
    i2cexit:
             SSPOV = 0
    
             WCOL = 0
    
             Return
    
    '-------------------------------------------------------------------------------
    
    boot:
    
    Pause 500
    
    High RL12
    
    High RL6
    
    '-------------------------------------------------------------------------------
    
    main:
          dataout = 24
    
          IF SSPIF = 1 Then
             GoSub i2cslave
             Toggle LEDL
          EndIF
    
    
          IF datain = 121 Then	' on 12v
            High RL12
          EndIF
    
          IF datain = 120 Then	' off 12v
             Low RL12
          EndIF
    
          IF datain = 61 Then	' on 6v
             High RL6
          EndIF
    
          IF datain = 60 Then	' off 6v
             Low RL6
          EndIF
    
          IF datain = 31 Then	' on buzer
             High BZR
          EndIF
    
          IF datain = 30 Then	' off buzzer
             Low BZR
          EndIF
        
          GoTo main
    
    '-------------------------------------------------------------------------------
    
    End
    
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    Could someone please through some light over here... I really don't know what else to check...

    Thanks a lot!

    Daniel.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, Daniel

    Did you had a look to the " I2cMaster " and " I2CSlave " example programs given on Melabs site ???

    looks something is read from the slave chip.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Yes, all my code is based on those two files, and those two files don't work out of the box... I mean, the I2C routines don't work without adding a few more lines... And, I'm still in the dark...

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Anyone willing to share ideas or a working code?

    Thanks!

    Daniel.

  5. #5


    Did you find this post helpful? Yes | No

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. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  4. I2C slave
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th March 2008, 03:46
  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 : 1

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