Lab 4

 Introduction to PIC18 Assembler

This is the grading form that will be used to grade your project.

Goal

In this lab you will write your first PIC assembly program. Also you will learn how to use the MPLAB IDE to develop PIC applications.

1. Initial Protoboard

First remove the wires and chips that you used in lab3. Make sure that your protoboard looks like the following picture. Do not remove the 7-segment display or any of the resistors.

Initial Protoboard

2. Adding a Third Push Switch

Add a third push switch to the protoboard. Connect the resistor of 10Kohms (brown-black-orange) and the ground connection as indicated in the picture. Also, move the large black and red wires to make it easier to position the third push button.

Adding third switch

3. Adding the Programming Header

You will need to add a programming header to the protoboard to be able to program your PIC18. The programming header needs to be glued with a glue gun to make sure that it does not move and it has good electrical contact.

The following picture shows the programming header. Bend the large terminals a little so it give an angle of about 100 degrees instead of the original 90 degrees.

Programming Header

Bending Header

Now attach the header to the protoboard at the position indicated in the picture.

Attaching Header

With the nail of your thumb push the header against the protoboard. Usig a glue gun, carefully put hot glue in the corner made by the header and the protoboard. Be careful not to burn yourself. It will be better if you let the lab instructor do this work for you. Wait for about a minute until the glue cools down. Disclaimer: Be careful. We are not responsible if you get burned with the glue.

Glue header

Also add  glue to the front of the header in the corner made by the header and the protoboard as indicated in the following picture. Do not use too much glue. Also be careful not to block other holes in the protoboard.

After header


4. Connecting the PIC18

You will now build the simplest configuration of the PIC18 chip as indicated in the following diagram:
Minimum PIC18

Now attach the PIC18 chip to the protoboard as indicated in the next picture. The pin1 as indicated in the bottom left corner in the chiupo should be connected to the C25 hole in the protoboard. After attaching the PIC18 to the protoboard, connect the +5V and ground to the PIC.

Adding the PIC

Connect the reset button and the power supply to the programming header.

connectingh header

Now connect the other connections from the programming header to the PIC18.

Finish PIC18 connections

Finally add the output LED to the PIC18. Now you have a complete PIC18 development circuit.

Output LED

5. Your first PIC18 Assembly Program.

You will use the Microchip MPLAB environment to assemble/compile your programs and transfer them to the PIC18. The computers in the lab LWSN160 already have the MPLAB IDE installed.

Run start->All  Programs->Microchip->MPLAB IDE v X.X->MPLAB IDE

You will get a screen like this one.

MPLAB Initial

Then go to Project->Project Wizard and click Next.

Step 1. For device select "PIC18F4550" and click next.

Step 2. In "Select a language toolsuite" choose "Microchip MPASM Toolsuite" and select Next.

Step 3. In "Create a new project", click in Browse and create a directory called "Lab4-pic" in your home directory where you are going to store your programs. Then in the "File Name" field write "hello" and select "Next".

Step 4. Do not select any files. Just click Next.

Then click Finish.

Now that you have created a new project file, you will write your first assembly language in PIC. Select File->New. Then copy and paste the following simple assembly program:



#include "P18f4550.INC"

CONFIG WDT=OFF; disable watchdog timer
CONFIG MCLRE = ON; MCLEAR Pin on
CONFIG DEBUG = ON; Enable Debug Mode
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG FOSC = INTOSCIO_EC;Internal oscillator, port function on RA6

org 0; start code at 0

Delay1 res 2 ;reserve space for the variable Delay1
Delay2 res 2 ;reserve space for the variable Delay2

Start:
CLRF PORTD ; Clear all D outputs
CLRF TRISD ; Make output all the bits in D
CLRF Delay1 ; Initialize both counters with 0s.
CLRF Delay2

MainLoop:
BTG PORTD,RD1 ;Toggle PORT D PIN 1 (20)

Delay:
DECFSZ Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
;Delay1 will be decremented 256 times before skipping
GOTO Delay
DECFSZ Delay2,1 ;Decrement Delay2 by 1, skip next instruction if Delay2 is 0
;Delay1 will be decremented 256 times before skipping.
GOTO Delay
GOTO MainLoop
end


Then select File->Save As and save the file as "hello.asm".

In the hello.mcw window, right click on the "Source Files" folder and select "Add Files". Then select the "hello.asm" file that you just created.

Now go to Project->Build All. If a dialog appears asking you for Absolute or Relocatable code, choose Absolute. Make sure that there are no errors.

 You will see in each computer in the lab a PICKit2 programmer device. Attach the PICKit2  to the programming header as ilustrated in the following picture.

Connecting the PICKIT2 to the protoboard

Select Programmer"->Select Programmer->PicKIT2. You should get  "PICkit 2 ready" in the output window.

Now select Programmer->Program to send your program to the PIC18 chip in the protoboard.

Then finally select Programmer->"Release from Reset" to restart the program. The LED should start blinking.

blinking

6. For Next Lab

For next lab you will drive the 7 segment display with the PIC18. Connect the 7-segment display to the pins RD0(seg a), RD1(seg b), RD2(seg c), RD3(seg d), RD4(seg e), RD5(seg f), RD6(seg g) as indicated in the following picture. Also, connect the second and third switches to RC0 and RC1.

pic to 7 seg display

another view


Now following the same steps indicated above, create a project called "display" that has the following code. Save the file as display.asm.


#include "P18f4550.INC"

CONFIG WDT=OFF; disable watchdog timer
CONFIG MCLRE = ON; MCLEAR Pin on
CONFIG DEBUG = ON; Enable Debug Mode
CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
CONFIG FOSC = INTOSCIO_EC;Internal oscillator, port function on RA6

org 0; start code at 0

Delay1 res 2 ; variable Delay1
Delay2 res 2 ; variable Delay2
Delay3 res 2 ; variable Delay3


Start:
CLRF PORTD ; Initialize with 0's output D.
CLRF TRISD ; Make port D output
CLRF Delay1; Clear delay variables
CLRF Delay2

SETF TRISC ; Make port c an input

MOVLW H'40' ; Initialize delay3 to 0x40. This is the delay used to rotate the segments.
MOVWF Delay3

BSF PORTD,RD0 ;Turn on bit 0 in RD0

MainLoop:
RRNCF PORTD ; Rotate bits in D. This causes the segments in display to shift.

MOVF Delay3,0 ; Reload Delay2 eith the value of Delay3. Delay2 controls the rate the
MOVWF Delay2 ; rotate takes place.

MOVLW H'F0' ; Test if Delay3 is at the maximum of 0xF0 or more. If that is the case, do not
CPFSLT Delay3 ; read the left switch.
goto noincrement

MOVLW 4 ; Read the left switch.
BTFSS PORTC,0 ; If the switch is 0 (gnd), then increase Delay3 by 4, otherwise skip the increment.
ADDWF Delay3,1

noincrement:

MOVLW H'05' ; Test if Delay3 is at the minimum pf 0x5 or less. If that is the case do not
CPFSGT Delay3 ; read the right switch.
goto Delay

MOVLW 4 ; Read the right switch.
BTFSS PORTC,1 ; If the switch is 0, then decrement Delay3 by 4, otherwise skip the decrement operation.
SUBWF Delay3,1

Delay:

DECFSZ Delay1,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
GOTO Delay
DECFSZ Delay2,1 ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0
GOTO Delay
GOTO MainLoop

end





Currently this code, rotates over all the segments from a to g and a again. You can decrease or increase the speed of the rotation by pressing the second and third buttons. You will modify this program to show the hexadecial digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f and 0 again in the 7 segment display one after the other while still controlling the speed with the second and third buttons.

Hint: You can define a variable "digit" in the same way that the "Display1" and "Display2" variables are defined. In the MainLoop increase this variable and when it reaches 15 reset the variable to 0. Also in MainLoop, do not rotate the PORTD register to rotate the segments. Instead, depending of the digit number, write in PORTD the combinations of 0s and 1's that will turn on the segments that correspond to that digit. For example, for 0 segments abcdef have to be turned on. That corresponds to bits 1(a)+2(b)+4(c)+8(d)=F and 1(e)+2(f)=3 or 0x3F. Therefore for digit 0 you have to write 0x3F into PORTD:

MOVLW H'3F'
MOVWF PORTD

You have to do the same for every digit from 0 to F depending on the value of the variable "digit". You can find a list of the PIC18 instructions in PIC18 Instruction Set.

You will bring to your lab the following next week.
  1. A working implementation that your instructor will grade.
  2. A printout of the assembly language program with your implementation.
You will find useful the following documentation about the PIC18:

PIC184550 Datasheet
PIC18 Instruction Set
Introduction to PIC18 Assembly
PIC18 tutorials and sample code
PicKit2 User Manual

The Video