THE SOFTWARE AND HOW IT WORKS
The software is fairly simple and is shown in Listing 1.
The program initially sets up the control registers in the
PIC16F690. PortC is made into an output and then initially
set to all zeros. The PIC16F690 defaults to analog-pin
mode, so the pins have to be made digital by clearing the
ANSEL register. The internal comparators are turned off by
clearing their respective registers.
control byte. The control byte for the 24LC01B is in the
datasheet and is (binary) %1010xxx, where xxx is the
setting for the A0, A1, and A2 pins of the EEPROM. You
can have more than one EEPROM connected to the I C
2
bus, so setting those pins to something other than ground
allows you to create a new control byte for that particular
part. If we had a second EEPROM hooked up to the bus,
its control byte could be %1010001. This would require
the A0 pin to be pulled to VDD instead of ground.
TRISC = 0
PORTC = 0
ANSEL = 0
CM1CON0 = 0
CM2CON0 = 0
A few constants and variables are created. The serial
communication pin is PORTC.0, which is also pin 8 in the
PICBASIC PRO PIC16F690 setup. The PICkit 2 acts as a
true UART hardware interface, even though it sends the
data back to the USB port. Therefore, we need the True
mode — or T2400 — for 2400 baud communication. The
N2400 is there just to work with a direct connection to
another PIC MCU instead of a PC.
For B0 = 0 To 15 ' Loop 16 times
I2CWRITE DPIN,CPIN,$A0,B0,[B0] ' Write each
' location's
' address to
' itself
Pause 10 ' Delay 10ms
' after each
' write
Next B0
SO
N2400
T2400
DPIN
CPIN
B0
B1
con
con
CON
var
var
var
var
8
4
0
PORTA.2 ' I2C data pin
PORTA.1 ' I2C clock pin
byte ' Temporary Byte
byte ' Temporary Byte
' Define serial output pin
' Set serial mode
Here is another example of why I like the PICBASIC PRO
compiler for the beginning programmer. The compiler has
I2CWRITE and I2CREAD commands to make this very
easy. The commands can use any digital port since they
"bit-bang" the I C signal. The command handles all the
2
timing and reading of the ACK bits. It also handles the
read or write bit in the control byte. The only things you
need to supply in the command line are the connection
designation for the data and clock pins, plus the
The program just “For-Loops” through setting each
memory address to the same value as the address (i.e.,
address 0 has data value 0; address 1 has data value 1,
etc.). This creates the initial data in the EEPROM. Next,
another For-Loop reads each address and sends the data
read to the PICkit 2 UART screen using the SEROUT
command. Figure 4 shows the UART screen and the 16
address locations read back from the EEPROM. The "#"
symbol in front of the B1 variable converts the decimal
value into an ASCII value for the UART. In other words, a
value of 15 is sent as two bytes — an ASCII "1" and ASCII
" 5" so it is displayed as 15, rather than the ASCII character
associated with decimal 15.
Figure 5 shows the MPLAB IDE window complete
with the PICkit 2 running. You need MPLAB IDE v. 8. 14
or later to use two PICkit 2 programmers at the same
time; otherwise, the software won't know which one goes
to the MPLAB IDE and which one goes to the UART. The
MPLAB IDE software or the PICkit 2 GUI will first ask
which PICkit 2 you want to connect to. I've covered how
you can store a unique name inside the PICkit 2's memory
to make it easier to select the correct programmer in past
articles. Check the
PICkit 2 manual for
instructions on how
to "set the unit ID."
The UART software
is under the tool
menu of the PICkit 2
standalone GUI
software that comes
with the programmer.
Both the MPLAB
IDE and PICkit 2
software can be
downloaded from
Microchip's website
■ FIGURE 4. UART Screen.
January 2009
■ FIGURE 5. MPLAB IDE Screen.
70