I2C Slave with a PIC


Results 1 to 40 of 130

Threaded View

  1. #19
    Join Date
    Jun 2011
    Location
    Philippines
    Posts
    223


    Did you find this post helpful? Yes | No

    Default Please help me understand how the application works

    Hello,
    While waiting for my Pic16F88(probable by next week) I tried to play with this code below with success in the simulation...
    master(Pic18F2620)
    Code:
    DEFINE OSC 40
    
    
    DEFINE I2C_HOLD 1
    
    
    DEFINE LCD_DREG         PORTB          
    DEFINE LCD_DBIT         0              
    DEFINE LCD_RSREG        PORTB         
    DEFINE LCD_RSBIT        4             
    DEFINE LCD_EREG         PORTB          
    DEFINE LCD_EBIT         5             
    DEFINE LCD_BITS         4           
    DEFINE LCD_LINES        2              
    DEFINE LCD_COMMANDUS    2000         
    DEFINE LCD_DATAUS       50           
    PAUSE 100
    LCDOUT $FE,1,$FE,2
    
    
    ADCON1=001111
    CMCON=000111
    
    
    scl             VAR PORTC.3
    sda             VAR PORTC.4                    
    
    
    i2c_address     VAR BYTE
    
    
    i2c_data        VAR BYTE[6]
    
    
    main:
    
    
    i2c_address=$02
    I2CREAD sda,scl,i2c_address,[STR i2c_data\6]
    LCDOUT $FE,$80,DEC i2c_data[0]
    LCDOUT " ",    DEC i2c_data[1]
    LCDOUT " ",    DEC i2c_data[2]
    LCDOUT " ",    DEC i2c_data[3]
    LCDOUT " ",    DEC i2c_data[4]
    LCDOUT " ",    DEC i2c_data[5]
    
    
    GOTO main
    Slave(Pic18F2620)
    Code:
    DEFINE OSC 40
    
    
    INCLUDE "DT_INTS-18.bas"  
    INCLUDE "ReEnterPBP-18.bas"
    
    
    ADCON1=001111                            
    CMCON=000111                      
    
    
    i2c_buffer_full     VAR SSPSTAT.0 
    i2c_read            VAR SSPSTAT.2
    i2c_release_scl     VAR SSPCON1.4 
    
    
    i2c_data            VAR BYTE[6]
    i2c_data_index      VAR BYTE
    
    
    dummy               VAR BYTE
    
    
    ;----------------------------------
    i var byte
    i=1
    
    
    SSPADD=$02          ' I2C Address: $0A
    SSPCON1=$36
    SSPCON2.0=1
    
    
    i2c_data[0]=6
    i2c_data[1]=5
    i2c_data[2]=4
    i2c_data[3]=3
    i2c_data[4]=2
    i2c_data[5]=1
    
    
    
    
    i2c_data_index=0
    
    
    ASM
    INT_LIST  macro  
            INT_Handler SSP_INT, _i2c_int_handler, PBP, yes
        endm
        INT_CREATE          
        INT_ENABLE SSP_INT
    endasm
    
    
    
    
    main:  
    
    
    goto main 
    
    
    
    
    i2c_int_handler:
    IF i2c_read then
        dummy=SSPBUF
        IF i2c_buffer_full=0 THEN
            SSPBUF=i2c_data[i2c_data_index]
            i2c_data_index=i2c_data_index+1
            IF i2c_data_index=6 THEN i2c_data_index=0
        ENDIF
    ENDIF
    i2c_release_scl=1 
    @ INT_RETURN
    As I understood, in the master pic the code below tries to communicate to the Slave having the address $02
    Code:
    i2c_address=$02
    I2CREAD sda,scl,i2c_address,[STR i2c_data\6]
    LCDOUT $FE,$80,DEC i2c_data[0]
    LCDOUT " ",    DEC i2c_data[1]
    LCDOUT " ",    DEC i2c_data[2]
    LCDOUT " ",    DEC i2c_data[3]
    LCDOUT " ",    DEC i2c_data[4]
    LCDOUT " ",    DEC i2c_data[5]
    and display data from Slave to the Master pic LCD. Then I tried to modify the setup and add another Slave#2 with address $0B like the this code below..
    Code:
    main:
    
    
    i2c_address=$02
    I2CREAD sda,scl,i2c_address,[STR i2c_data\6]
    LCDOUT $FE,$80,DEC i2c_data[0]
    LCDOUT " ",    DEC i2c_data[1]
    LCDOUT " ",    DEC i2c_data[2]
    LCDOUT " ",    DEC i2c_data[3]
    LCDOUT " ",    DEC i2c_data[4]
    LCDOUT " ",    DEC i2c_data[5]
    
    
    PAUSE 1000
    LCDOUT 254,1
    pause 500
    i2c_address=$0B
    
    
    I2CREAD sda,scl,i2c_address,[STR i2c_data\6]
    LCDOUT $FE,$80,DEC i2c_data[0]
    LCDOUT " ",    DEC i2c_data[1]
    LCDOUT " ",    DEC i2c_data[2]
    LCDOUT " ",    DEC i2c_data[3]
    LCDOUT " ",    DEC i2c_data[4]
    LCDOUT " ",    DEC i2c_data[5]
    
    
    PAUSE 1000
    
    
    GOTO main
    What I get is "6 5 4 3 2 1" and "0 0 0 0 0 0 " should be "1 2 3 4 5 6" for Slave#2
    Slave#2
    Code:
    DEFINE OSC 40
    
    
    INCLUDE "DT_INTS-18.bas"  
    INCLUDE "ReEnterPBP-18.bas"
    
    
    ADCON1=001111                            
    CMCON=000111                      
    
    
    i2c_buffer_full     VAR SSPSTAT.0 
    i2c_read            VAR SSPSTAT.2
    i2c_release_scl     VAR SSPCON1.4 
    
    
    i2c_data            VAR BYTE[6]
    i2c_data_index      VAR BYTE
    
    
    dummy               VAR BYTE
    
    
    ;----------------------------------
    i var byte
    i=1
    
    
    SSPADD=$0B          ' I2C Address: $0B
    SSPCON1=$36
    SSPCON2.0=1
    
    
    i2c_data[0]=1
    i2c_data[1]=2
    i2c_data[2]=3
    i2c_data[3]=4
    i2c_data[4]=5
    i2c_data[5]=6
    
    
    
    
    i2c_data_index=0
    
    
    ASM
    INT_LIST  macro  
            INT_Handler SSP_INT, _i2c_int_handler, PBP, yes
        endm
        INT_CREATE          
        INT_ENABLE SSP_INT
    endasm
    
    
    
    
    main:  
    
    
    goto main 
    
    
    
    
    i2c_int_handler:
    IF i2c_read then
        dummy=SSPBUF
        IF i2c_buffer_full=0 THEN
            SSPBUF=i2c_data[i2c_data_index]
            i2c_data_index=i2c_data_index+1
            IF i2c_data_index=6 THEN i2c_data_index=0
        ENDIF
    ENDIF
    i2c_release_scl=1 
    @ INT_RETURN
    I was only hoping that it would work that way(I guess not ), how can I add another Slave and get data from it? I appreciate any help and the time for sharing.

    Thanks in advance,
    tacbanon
    Name:  I2cerr1234.gif
Views: 3114
Size:  120.2 KB
    Last edited by tacbanon; - 26th April 2012 at 13:56.

Similar Threads

  1. Problem with PICto PIC I2C MASTER-SLAVE
    By juanen19 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th June 2013, 03:58
  2. PIC as I2C Slave
    By Mainul in forum General
    Replies: 4
    Last Post: - 5th January 2013, 14:23
  3. I2C Slave, config Vref - + in pic with ADC
    By sebapostigo in forum PBP Wish List
    Replies: 4
    Last Post: - 5th March 2007, 04:21
  4. Pic as an i2c slave
    By Robert Soubie in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th January 2007, 22:11
  5. Use pic as slave in I2C
    By robert0 in forum General
    Replies: 2
    Last Post: - 3rd February 2006, 20:26

Members who have read this thread : 2

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