I2C Slave with a PIC


Results 1 to 40 of 130

Threaded View

  1. #12


    Did you find this post helpful? Yes | No

    Default

    Sure! Let me know if you need anything else

    Master 16f877 - External 4Mhz Crystal:
    Code:
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    ' Master
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    
    DEFINE OSC 4
    
    'OPTION_REG.7 = 0
    
    ADCON1 = 7
    
    '------------------------------------------------------------------------------
    
    DEFINE I2C_SLOW 1
    
    TRISB.5 = 1
    TRISB.7 = 1
    
    '------------------------------------------------------------------------------
    
    scl VAR PORTB.5		' i2c clock input
    sda VAR PORTB.7     ' i2c data input
    
    '------------------------------------------------------------------------------
    
    I2Caddress VAR BYTE
    
    valor VAR BYTE
    
    '------------------------------------------------------------------------------
    
    ' Set LCD Data port
    DEFINE LCD_DREG PORTD
    ' Set starting Data BIT (0 OR 4) IF 4-BIT bus
    DEFINE LCD_DBIT 4
    ' Set LCD Register Select port
    DEFINE LCD_RSREG PORTC
    ' Set LCD Register Select BIT
    DEFINE LCD_RSBIT 6
    ' Set LCD Enable port
    DEFINE LCD_EREG PORTC
    ' Set LCD Enable BIT
    DEFINE LCD_EBIT 7
    ' Set LCD bus size (4 OR 8 bits)
    DEFINE LCD_BITS 4
    ' Set number of lines ON LCD
    DEFINE LCD_LINES 2
    ' Set command delay time in us
    DEFINE LCD_COMMANDUS 2000
    ' Set Data delay time in us
    DEFINE LCD_DATAUS 50
    
    '------------------------------------------------------------------------------
    
    Pause 500       ' Wait for LCD to startup
    LCDOut $fe, 1
    LCDOut $fe, "Daniel"
    Pause 500       ' Wait for LCD to startup
    
    valor = 0
    
    '------------------------------------------------------------------------------
    
    main:
          I2Caddress = $3
    
          LCDOut $fe, 1
          LCDOut "Writting 1 - LED ON..."
          Pause 500
    
          I2CWrite SDA, SCL, I2Caddress, [6], bogus ' Write offset to slave
          Pause 500
    
          LCDOut $fe, 1
          LCDOut "Writting 1 - LED OFF..."
          Pause 500
    
          I2CWrite SDA, SCL, I2Caddress, [8], bogus ' Write to slave
          Pause 500
    
          LCDOut $fe, 1
          LCDOut "Reading 1..."
          Pause 500
    
          I2CRead SDA, SCL, I2Caddress, [valor], bogus ' Read from slave
          LCDOut $fe, 1
          LCDOut "Value: ", DEC valor
          Pause 500
    
          '--------
    
          I2Caddress = $5
    
          LCDOut $fe, 1
          LCDOut "Writting 2 - LED ON..."
          Pause 500
    
          I2CWrite SDA, SCL, I2Caddress, [4], bogus ' Write to slave
          Pause 500
    
          LCDOut $fe, 1
          LCDOut "Writting 2 - LED OFF..."
          Pause 500
    
          I2CWrite SDA, SCL, I2Caddress, [10], bogus ' Write to slave
          Pause 500
    
          LCDOut $fe, 1
          LCDOut "Reading 2..."
          Pause 500
    
          I2CRead SDA, SCL, I2Caddress, [valor], bogus ' Read from slave
          LCDOut $fe, 1
          LCDOut "Value: ", DEC valor
          Pause 500
    
          '--------
    
          GoTo main       ' Do it forever
    
    '-------------------------------------------------------------------------------
    
    bogus:
    		LCDOut $fe,1, "Time out"	' I2C command timed out
    		Pause 500
    
    		GoTo main
    
    '-------------------------------------------------------------------------------
    
    End
    
    '-------------------------------------------------------------------------------
    '-------------------------------------------------------------------------------
    Last edited by DanPBP; - 19th February 2009 at 19:34.

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