ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

microEngineering Labs, Inc. | 1-719-520-5323 | Example Program - I2CMAST.pbp

PICBASIC PRO program to read and write to I2C slave. Writes a 1-byte offset value, pauses, then reads an 8 character string from the slave. Expects to find the ADC conversion value (- offset) in the 6th position of the string.
' Name        : I2CMAST.pbp
' Compiler    : PICBASIC PRO Compiler 2.6
' Assembler   : PM or MPASM
' Target PIC  : 40-pin 16F or 18F
' Hardware    : Non specific
' Oscillator  : 4MHz internal or external
' Keywords    : I2CREAD, I2CWRITE, LCDOUT
' Description : PICBASIC PRO program to read and write to I2C slave.
' Writes a 1-byte offset value, pauses, then reads an 8 character string
' from the slave. Expects to find the ADC conversion value (- offset) in
' the 6th position of the string.
'

' Define LCD registers and bits
Define LCD_DREG  PORTD
Define LCD_DBIT  4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG  PORTE
Define LCD_EBIT  1

Define I2C_HOLD 1

SCL VAR PORTC.3    ' Clock pin
SDA VAR PORTC.4    ' Data pin
a   VAR BYTE[8]    ' Holds 8 characters read from slave

   ADCON1 = 7      ' Set PORTA and PORTE to digital
   Low PORTE.2     ' LCD R/W line low (W)
   Pause 100       ' Wait for LCD to start up

   LCDOut $fe, 1, "I2CX"  ' Display I2CX on LCD
   Pause 1000

mainloop:
   I2CWrite SDA,SCL,$02,[127], bogus    ' Write offset to slave
   Pause 500               

   I2CRead SDA,SCL,$02,[STR a\8], bogus ' Read string from slave
      
   LCDOut $fe, 1, STR a\5,SDEC a[5]     ' Display result, preceded by 5 character
                                        ' string received from slave.
   Pause 500
   GoTo mainloop                        ' Do it forever

bogus:
   LCDOut $fe,1, "timed out"            ' I2C command timed out
   Pause 1000
   GoTo mainloop

   End

           

Download the program file.