Program description
What does it do?
The output program shows you how to light up the WAND board LEDs.
New instructions
During this activity, you will learn about these microcontroller instructions:
| goto | jump to (or continue running the program) from a label |
| movlw | 'move literal to W' - move a number (also known as a literal, or constant) into W (the working register) |
| movwf | 'move W to file register' - move the number in W (the working register) to a file register (a RAM location) |
| clrf | 'clear file register' - clear the contents of a file register (a RAM location) |
| sleep | stop the microcontroller clock, and shut down the processing unit. During sleep, port output pins remain in their last state. |
New directives
This program introduces many directives. Like instructions, directives are a part of the program. But, unlike instructions, which actually control the microcontroller, directives control the operation of the MPLAB assembler, the programmer or the downloader.
| include | inserts the named file into the program |
| __config | sets up the hardware features of the microcontroller (dependent on a programmer that can read these settings) |
| org | 'origin' - sets the program memory address for the next instruction |
| banksel | 'bank select' - inserts instructions to switch the PIC microcontroller to the bank of file register locations (RAM bank) containing the specified register |
| res | 'reserve memory' - reserves the specified number of program memory addresses |
| end | signifies the end of the program |
Output programming activity
Let's start WAND programming by making it turn on some LEDs. If you assembled your WAND from scratch, this is a good place to start because you can use the Output program to test the functionality of your circuit as well as your LEDs.
Program requirements
To use this program you will need:
• An assembled WAND board.
• A Windows PC with the MPLAB IDE software.
• A Microchip PICkit-2 or PICkit-3 USB programmer.
• Batteries to power the WAND.
Before you continue, you will need to know how to edit, assemble and simulate a program in MPLAB (see the MPLAB tutorial).
What you should know before starting
WAND related information
This program controls the LEDs connected to PORTC of the PIC microcontroller (see the WAND 2.0 schematic to examine the PORTC circuitry).
Microcontroller related information
PORTC is an 8-bit RAM register (known as a file register Microchip terminology) that connects the processing unit core to six of its external I/O (input/output) pins. PORTC is an 8-bit port, but since it connects to only six I/O pins it can control 6 individual circuits. Each of the PORTC pins can be set to be either a digital input or an output, as well as an analogue input, depending on the value of associated bits in the ANDSEL and TRISC registers.
TRISC (TRIState for port C) is, like PORTC, also an 8-bit file register (or, RAM memory location). Each bit of the TRISC register controls the input/output status of its associated PORTC I/O pin. When a value of 0 is written into a TRISC bit, it makes the corresponding PORTC pin an output. Conversely, when a value of 1 is written into a TRISC bit, it makes the corresponding PORTC pin an input. For example, writing the 8-bit number 00000111 into TRISC would cause the upper three PORTC pins (remember the two most significant bits don't connect externally) to become outputs, and the lower three PORTC pins to become inputs.
The PORTC and TRISC file registers are located in two different file register pages (or, RAM banks) inside the microcontroller (see the simplified PIC16F676 block diagram for an arthitectural overview of the microcontroller). The PIC contains a register known as the STATUS register, which controls access to the register banks, amongst other thing. In the Output program, we'll use the banksel directive (see New Directives at left for its description) to indirectly control the STATUS register, because it makes programming easier for us than controlling the STATUS register directly.
Another register that will see a lot of use is the W, or Working register (see the simplified block diagram, above). Almost all math, logic and data movement instructions in the PIC microcontroller involve the use of W. The Output program, for example, lights the LEDs by moving a pattern entered in the program (and stored in the program memory) to the W register first, and then from W to the PORTC file register. In PIC microcontrollers, any movement of data from the program memory to a RAM file register requires this same kind of two-step movement—first to move data into W, then to move the date to its destination file register.
Create the program
The entire OUTPUT.ASM program is shown below. Start a new MPLAB project, copy all of the OUTPUT.ASM code into the project, and assemble (make) the program.
;OUTPUT.ASM v2.0 Last modified on August 3, 2010 ;=============================================================================== ;Description: Output test program. Lights up PORTC LEDs. ;Start of MPLAB and processor configuration. include "p16f676.inc" ;Include processor definitions __config _CPD_OFF & _CP_OFF & _BODEN_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT ;End of MPLAB and processor configuration. org 3FFh ;Oscillator calibration location oscillator org 00h ;Start of program memory bsf STATUS,RP0 ;Select memory register page 1 call oscillator ;Store pre-programmed oscillator calibration movwf OSCCAL ;constant in OSCCAL register goto init ;Start program after Interrupt vector org 05h ;Continue after interrupt vector init ;Initialize ports A and C for digital I/O bcf STATUS,RP0 movlw 7 ;Disable comparator and make movwf CMCON ;RA0-2 digital I/O banksel ANSEL ;Switch to register bank 1 movlw 11010111b ;Disable Port A pull-ups, set TMR0 to movwf OPTION_REG ;internal clock with prescaler 256 clrf ANSEL ;Set all PORTA pins to digital movlw 11011111b ;Set RA5 as output and all movwf TRISA ;other PORTA pins as inputs clrf TRISC ;Set all PORTC pins as outputs banksel PORTC ;Return to PORTC register bank clrf PORTA ;Turn off phototransistor LED main movlw 00111111b ;Send light pattern to movwf PORTC ;Port C LEDs sleep ;Shut down the processor end
If the program does not assemble, or generates errors or warnings during assembly, check that you didn't accidentally miss any of the code, that the __config directives are entered exactly as shown, and that the project settings for the program disable case sensitivity and use decimal as the default radix (see the MPLAB tutorial).
Once the Output program builds successfully, program it into your microcontroller to see it work. Four lights should be lit—the top two (LED2 and LED3), and the bottom two (LED8 and LED9).
How the program works
You might want to reference the simplified PIC16F676 block diagram to help visualize what happens inside the microcontroller during the following explanations.
Comments and directives
This program, like most programs, starts with some comments. In MPLAB, any characters following a semi-colon (;) are treated as comments and are ignored by the assembler. Comments are included in programs to provide additional information for us humans, the programmers.
Test your knowledge
- Assembly code programs contain comments, labels, directives, instructions, and data. What is the difference between a directive and an instruction? What is the difference between a comment and a label?
- The Output program contains six different directives. List each directive and describe its function.
- The PIC16F886A microcontroller contains more than one kind of memory. What kind of memory are the file registers made of? What special features do the PORTA, PORTC, and PORTC file registers have?
- We refer to registers by name, but the microcontroller refers to registers by their numeric address. What are the addresses of the TRISC and PORTC registers?
- What is the W register, and in what part of the microcontroller is it located?
- What is the function of the TRISA, TRISC, and TRISC registers?
- What value would need to be written into the TRISC register to make the bottom six LEDs of the WAND board into outputs, and the other PORTC pins into inputs?
- Each program instruction takes one or more processor clock cycles to execute. Use the stopwatch in the MPLAB simulator to determine how many clock cycles each instruction takes to run. (Set the processor frequency to 4MHz to match the PIC in the WAND board.)
Apply your skills
- Modify the Output program to turn on all of PORTC LEDs (useful for testing your circuit board if you haven't already done so).
- Modify the Output program to repeatedly display two or more different light patterns on the LEDs and program it into your WAND (hint: create another output pattern, and use a goto instruction to repeat both patterns). Run the program and describe its output. Is it what you expected? Explain why not.

