Listing 1:
DEFINE LCD_DREG PORTB ‘Define PIC port used for LCD Data lines
DEFINE LCD_DBIT 4 ‘Define first pin of portb connected to LCD DB4
DEFINE LCD_RSREG PORTB ‘Define PIC port used for RS line of LCD
DEFINE LCD_RSBIT 3 ‘Define Portb pin used for RS connection
DEFINE LCD_EREG PORTB ‘Define PIC prot used for E line of LCD
DEFINE LCD_EBIT 0 ‘Define PortB pin used for E connection
DEFINE LCD_BITS 4 ‘Define the 4 bit communication mode to LCD
DEFINE LCD_LINES 2 ‘Define using a 2 line LCD
DEFINE LCD_COMMANDUS 2000 ‘Define delay time between sending LCD commands
DEFINE LCD_DATAUS 50 ‘Define delay time between data sent.
understand. Just type the code
shown in the code listing into
the MicroCode Studio editor
window as shown in Figure 5
and save it as LCD.bas. (The
LCD.bas code listing is available
on the Nuts & Volts website;
www.nutsvolts.com) After
saving it, verify that the little
window in the MCStudio screen
shows the 16F876A PIC. If not, select
it from the list. Now click on the compile icon or just press F9. The program
will be compiled and assembled into
a file named LCD.hex. Find that file
on your hard drive because we need it
for the next step.
HOW IT WORKS
Listing 2:
Pause 250
Start:
lcdout $fe, 1
lcdout $fe, 2
‘ Delay for LCD to power up
‘ Clear LCD
‘ Position cursor at home
‘***** Send “Hello World” to first line of LCD ******
lcdout “Hello World””‘ Send “Hello World” to the LCD
The beginning of the code listing
shows a list of DEFINEs. These set up
the LCD connections and establish the
communication. There are default values for these which define the connections to the LCD using PortA. Using
the default connections would reduce
the lines of code counted in the 31
limit because we could skip these definitions. I decided to use them for now,
but in future LCD projects, I’ll rewire it
to save code space (see Listing 1).
The main loop of code is below the
“Main Code” title block (see Listing 2).
We have to slow down the PIC by
pausing 250 milliseconds. This allows
the LCD to get ready to receive
command data. Some LCDs need
this, others don’t. After the pause, the
LCDOUT command sends command
control data to the LCD. Sending the
“$FE” first tells the LCD to accept the
next value as a command code. The
“1” is the command code to clear the
LCD screen. The “2” is the command
code to position the LCD prompt at
the first line, first character position.
This sets up the LCD to receive the
data we want to display.
The following LCDOUT command
sends the letters between quotes
(Hello World) as ASCII characters to
the LCD. The LCD will immediately
display that data as “Hello World”
starting at the first character block.
The program then pauses for one
second (PAUSE 1000) and finally
⇒ FIGURE 6. PIC Programmer Software.
jumps back to the “start” label using
the “Goto Start” command line and
repeats the process.
Notice we did all that in three
main LCDOUT commands, but the
DEFINES at the top along with the
“Start” label, PAUSEs, and GOTO
commands all count toward the 31
limit. As written, this program uses 17
command lines leaving 14 for
additional commands or labels.
Variables do not count towards the
31 so there are more things we can
do with 31 total commands. As
mentioned earlier, using the default
connections gives us more code
space. I’ll probably use the defaults in
future projects for that reason.
PROGRAMMING THE PIC
Now that we have written the
program and successfully compiled
and assembled it resulting in a binary
LCD.hex file, the next step is to start
up the PIC programmer software as
seen in Figure 6. Make sure the software is set up to the proper COM port
January 2006 77