assemble the file into the binary 1s and 0s that make up the
.hex file the PICkit 2 will Flash into the MCU. By learning
assembly language, you are just skipping the compiler stage
as the rest of the process is the same.
Assembly language commands can look very cryptic, but
after you use them for a little while you will find they are
just a few letter abbreviations for what the commands accomplish. On the PIC16F690, there are 35 assembly language
instructions but in most cases, you will only use about half of
them. To really understand and use assembly language, you
really only need to memorize about 20 commands or less.
The biggest confusion I find beginners have with
assembly is the memory structure of the smaller PIC16F
family MCUs. The control registers are multiplexed in a bank
arrangement to share address lines. This means you have to
set or clear a bit in the Status register to jump to the proper
bank of memory and access certain registers. Once you see
it done though, it’s easy to follow and use. Compilers tend
to handle this for you, so you don’t have to deal with it.
LIGHT AN LED
I figure that the best way to show assembly language is
to jump right into a project. The project in Listing 1 is a
simple program that lights an LED connected to the PortC
pin 0 of the PIC16F690. The DS1 LED of the PICkit 2
development board already has the pin connected to a
resistor that is in-series with an LED, with the cathode connected to ground. A high on PortC pin 0 will light the LED;
a low will shut it off. If you’ve used microEngineering Labs’
PICBASIC PRO compiler (which I’ve mentioned in this column many times before), you know how easy performing
this function can be. A simple “High PortC.0” command will
get the job done. What is underneath that simple command
line, though, are several assembly language commands.
Listing 1 shows an example of an assembly language
routine to light the LED. As you will notice, it is a lot longer
than the simple PICBASIC PRO command line though, in
reality, it may not take up any more program memory
space. Some of the program structure in Listing 1 also
includes the setup and configuration information that
PICBASIC PRO handles with an include file. It is typical of
a compiler to hide a bunch of details in other include files
to make writing the software easier for you.
Let me step through the various sections of Listing 1, to
help you understand what is going on.
Right at the top of the program is a “#include” command that indicates which processor file to include. This .inc
include file has all the inner details of the PIC16F690, such
as register locations and names of the various special function registers the program may reference. This is very similar
to a .h header file in C language, and PICBASIC PRO has
.inc files that it calls in the background during compile time.
The next line I show is for the configuration fuses.
These set the various options, such as oscillator selection
(internal oscillator), watchdog timer (disabled), and master
clear pin (internal). There are other configuration fuses and
the definition of these rests in the p16F690.inc file. You can
actually leave this line off, but then you have to manually set
these fuses in the programmer’s software or the MPLAB
IDE configuration settings under the Configure >
Configuration Settings… menu option.
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF)
The “org 0” line really starts the assembly language program. The MCU will jump to location zero of memory after
a reset condition or power-up. Therefore, we have to tell the
program where to go by placing a command at memory
location zero. The org 0 directive tells the assembler to
place the next command at memory location zero. That
command is the “goto Start” command line. You’ve probably seen the GOTO command in many other languages, so
this is pretty universal and easy to understand. You can
already see that some of the assembly commands are not
tough to figure out at all. The label “Start” is just a label that
shows up further down in the program. So, when the MCU
is reset, the first thing it will do is jump to the label Start.
Another org directive is used to position the beginning of
the main program. Inside the PIC16F690, interrupts will automatically jump to memory location four, so the main loop of
code needs to be placed after the interrupt location. We do
that by simply creating an org 5 statement. Any commands
after the org 5 statement will be placed at memory location
five. The label Start is after the org 5, so that label equates to
memory location five but it’s not a command. The first command at location five is the bsf STATUS, RP0 command line.
bsf STATUS,RP0 ; select Register Page 1
The “BSF” is an assembly command that is an abbreviation
for Bit Set File register, or set a specific bit in a defined special
function register. In this case, the special function register is the
STATUS register. The RP0 in the command is the individual
bit location to set. The RP0 bit determines which bank of
memory to address. Remember that I mentioned the memory
in the smaller PIC MCUs is banked? The next command will
modify the TRISC register, which is located on Bank 1 of the
PIC16F690 memory map. So, we need to first switch over to
Bank 1 before operating on the TRISC register. To do that, we
set the RP0 bit of the STATUS register with the BSF command.
We want to make the PortC pin 0 an output to drive the
LED. That bit in the TRISC register needs to be cleared, to
make PortC pin 0 and output. Therefore, the command BCF
(Bit Clear File) is doing just that, clearing the proper bit in
the TRISC register.
bcf TRISC,0 ; make IO Pin C0 an output
We don’t need to access any other registers on Bank 1
of memory, so we need to reset program control back to
March 2008 15