default speed for the PICBASIC PRO compiler. The final
schematic is shown in Figure 7.
The demonstration program I wrote initially writes
16 bytes of data to the EEPROM and then reads each byte
back one at a time. This is done over and over again in a
continuous loop, with each value displayed on an LCD
module. Because the sample version of the PICBASIC
PRO compiler only supports 31 commands, I needed a
simple way to display the data. The problem I found
driving a parallel LCD module was that it took too many
set-up lines and pushed me beyond 31 commands.
My second choice was a serial LCD module. With a
single SEROUT command and three connections — 5V,
ground, and signal — I could easily display the data read
from the EEPROM. I used a serial LCD module from
microEngineering Labs connected to a 2 x 40 LCD. The
LCD is connected to the MCU's C5 pin through the
expansion header on the side of the development board,
making this an easy connection. The expansion header
has 5V and ground connections, as well. The final setup is
shown in Figure 8. The PICkit 2 supplied enough current
to power the whole circuit through the USB connection.
No jumper wires, no soldering, and no separate
breadboard. You can't get much easier than that.
I wrote the software in blocks
to make it easier to explain the
operation. Combine all the blocks
together for the complete software
routine. I start off with a simple
description of the operation and then
set up the port settings. The labels
SI and SO are from the EEPROM
perspective. The LCD is on PORTC. 5.
I use a VAR or variable directive to
establish these labels, even though
it may seem like they could be
constants. The labels can't be
constants though, because the value
can contain a zero or one value, thereby requiring a
variable definition. As a final step, I preset the input and
output states of the pins by writing directly to the TRISC
register. A one makes the pin an input and a zero makes
the pin an output. The most significant bit is to the left.
■ FIGURE 7. Final
' Program to read and write to SPI EEPROM
' 25LC020 on PICkit Serial SPI Demo Board
' using PIC16F870.
' Write to the first 16 locations
' of 25LC020 serial EEPROM.
' Then Read first 16 locations back
' and send to LCD repeatedly.
' Port Settings ***
CS var PORTC.7 ' Chip select
SCK var PORTC.3 ' Clock pin
SI var PORTC.6 ' MOSI pin
SO var PORTC.4 ' MISO pin
LCX var PORTC.5 ' LCD
' C4 input, C7-C5, C3-C0 outputs TRISC =
The next section creates two byte variables that I will
use throughout the program. One is the "addr" variable
that contains the address of the data to be read or written.
The other is a general-purpose "B0"
variable that I use to hold the data
being written to or read from the
EEPROM. The 25LC020 EEPROM uses
eight bit communication. Check the
datasheet of the EEPROM you use in
your SPI communications because
some require a 16-bit address.
■ FIGURE 8. Final Project Setup.
'*** Variable Setup ***
addr var byte '
B0 var byte '
The "main" label marks the FOR-NEXT routine that loads the initial data
into the EEPROM. The addr variable is
used as a counter variable, and also as