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
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - SPIX.pbp

PICBASIC PRO program to read and write to SPI SEEPROMs Writes to the first 16 locations of an external serial EEPROM. Then reads first 16 locations back and send to LCD repeatedly Note: for SEEPROMs with word-sized address.
' Name        : SPIX.pbp
' Compiler    : PICBASIC PRO Compiler 2.60
' Assembler   : PM or MPASM
' Target PIC  : 40-pin 16F887
' Hardware    : LAB-X1 Experimenter Board
' Oscillator  : 4MHz external crystal
' Keywords    : LCDOUT, SHIFTIN, SHIFTOUT
' Description : PICBASIC PRO program to read and write to SPI SEEPROMs
' Writes to the first 16 locations of an external serial EEPROM. Then
' reads first 16 locations back and send to LCD repeatedly Note: for 
' SEEPROMs with word-sized address.
'

' 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

Include "modedefs.bas"

CS  Var PORTA.5  ' Chip select pin
SCK Var PORTC.3  ' Clock pin
SI  Var PORTC.4  ' Data in pin
SO  Var PORTC.5  ' Data out pin

addr Var Word    ' Address
B0   Var Byte    ' Data

   TRISA.5 = 0   ' Set CS to output
   ANSEL = %00000000 ' Make PORTA and PORTE digital
   ANSELH= %00000000 ' Make PORTA and PORTE digital
   Low PORTE.2   ' LCD R/W line low (W)
   Pause 100     ' Wait for LCD to start up

   For addr = 0 To 15  ' Loop 16 times
     B0 = addr + 100   ' B0 is data for SEEPROM
     Gosub eewrite     ' Write to SEEPROM
     Pause 10          ' Delay 10ms after each write
   Next addr

mainloop:
   For addr = 0 To 15  ' Loop 16 times
     Gosub eeread      ' Read from SEEPROM
     Lcdout $fe,1,#addr,": ",#B0 ' Display
     Pause 1000
   Next addr
   Goto mainloop

' Subroutine to read data from addr in serial EEPROM
eeread:
   CS = 0              ' Enable serial EEPROM
   Shiftout SI, SCK, MSBFIRST, [$03, addr.byte1, addr.byte0] ' Send read command and address
   Shiftin SO, SCK, MSBPRE, [B0] ' Read data
   CS = 1              ' Disable
   Return

' Subroutine to write data at addr in serial EEPROM
eewrite:
   CS = 0               ' Enable serial EEPROM
   Shiftout SI, SCK, MSBFIRST, [$06] ' Send write enable command
   CS = 1               ' Disable to execute command
   CS = 0               ' Enable
   Shiftout SI, SCK, MSBFIRST, [$02, addr.byte1, addr.byte0, B0] ' Send address and data
   CS = 1               ' Disable
   Return

   End

           

Download the program file.