You are on page 1of 55

Assembly Language Programming for PIC

Introduction

Communication between human & microcontroller.


Program.asm Program.hex (machine language)
11 00xx 0010 0000

assembler/ translator

programmer

MOVLW 0x20

Representation Numbers in Assembler

Hexadecimal:
MOVLW 99H, MOVLW 0x99, MOVLW 99, MOVLW h99

ASCII:
MOVLW A2 ;WREG = 00110010 or 32 in hex

Decimal:
MOVLW D12, MOVLW .12 ;WREG = 00001100 = 0CH = 12

Binary:
MOVLW B10011001 ;WREG = 1001101 or 99 in hex

Representation Numbers in Assembler cont


MOVLW 25 ADDLW 0x11 ADDLW 12H ADDLW H2A ADDLW 2CH MOVLW E5H ADDLW C6H ;WREG = 25H ;WREG = 25H + 11H = 36H ;WREG = 36H + 12H = 48H ;WREG = 48H +2AH = 72H ;WREG = 72H + 2CH = 9EH ;invalid, it must be MOVLW 0E5H ;invalid, it must be ADDLW 0C6

Review
1. Give three ways for hex data representation in the PIC assembler? 2. Show how to represent decimal 99 in formats of (a) hex, (b) decimal, and (c) binary in the PIC assembler.

PIC Assembly Programming


Basic elements:

Label Instruction (mnemonic) Operand(s) Directive Comment

Structure:
[label] mnemonic [operands] [;comment]

Labels

Allows the program to refer to a line of code or section of program by name Marco, branching, goto

Instructions

The way we write an instruction (syntax)

Operands

Instruction element for the instruction that is being executed Registers / variables / constants

Comments

Begin with semicolon (;) Series of words that a programmer writes to make the program more clear & legible

Directives

Similar to an instruction, but unlike an instruction it is independent on the microcontroller model Represent a characteristic of the assembly language itself

Control Directives
#DEFINE
Exchange one part of text for another

Syntax: #define<text> [<another text>] Example: #define turned_on 1 #define turned_off 0

Control Directives

cont

#INCLUDE Include an additional file in a program

Syntax: #include <file_name> #include "file_name" Example: #include <regs.h> #include "subprog.asm"

Control Directives
EQU

cont

Defining assembler constant

Syntax: <name_constant> equ <value> Example: five equ 5 six equ 6 seven equ 7

Control Directives
Using EQU for fixed data assignment
DATA1 DATA2 DATA3 DATA4 DATA5 EQU EQU EQU EQU EQU
;in hexadecimal 39 0x39 39h H39 h39 ;in binary b00110101 B00110101

cont

;hex data is the default ;another way for hex ;another way for hex ;another way for hex ;another way for hex

DATA6 DATA7

EQU EQU

;binary (35 in hex) ;binary (35 in hex)

Control Directives
DATA8 hex) DATA9 EQU EQU ;in decimal D28 d28 ;in ASCII A2 a2 2

cont

;decimal numbers (1C in ;second way for decimal

DATA10 EQU DATA11 EQU char DATA12 EQU char

;ASCII characters ;another way for ASCII ;another way for ASCII

Control Directives
Using EQU for SFR address assignment
COUNTER value 00 PORTB address MOVLW MOVWF INCF 01 INCF 02 INCF 03 EQU
EQU

cont

0x00
0x06

;counter
;Port B ;WREG = 00H ;Port B has ;Port B has ;Port B has

COUNTER PORTB ;Port B now has 00 too PORTB, F PORTB, F PORTB, F

Control Directives
MYREG EQU location to MYREG 0x12

cont

Using EQU for RAM address assignment


;assign RAM

MOVLW 0 ;clear WREG (WREG = 0) MOVWF MYREG ;clear MYREG (loc 12 has 0) MOVLW 22H ;WREG = 22H ADDWF MYREG, F ;MYREG = WREG + MYREG ADDWF MYREG, F ;MYREG = WREG + MYREG ADDWF MYREG, F ;MYREG = WREG + MYRG

Control Directives
ORG

cont

Defines an address from which the program is stored in C memory

Syntax: <label>org<value> Example: Start org 000 movlw 0xFF movwf PORTB

Control Directives
END
End of program

cont

Syntax: End Example:


. .

movlw 0xFF movwf PORTB end

Command

Operand

; Start main loop ;............................................................... reset start CLRF BTFSS GOTO BTFSC GOTO INCF MOVLW CALL GOTO END 06 05,0 reset 05,01 start 06 0FF delay start ;Clear Port B Data ;Test RA0 input button ;and reset port B if pressed ;Test RA1 input button ;and run count if pressed ;increment count at Port B ;Delay count literal ;Jump to subroutine 'delay' ;Repeat main loop always ;Terminate source code

Label

Comment

Review
1. _______ are translated by the assembler into machine code, whereas _______ are not. 2. True or false. Assembly language is a high-level language. 3. Which of the following instructions produces opcode? List all that do. (a) MOVLW 25H (b) ADDLW 12 (b) ORG 2000H (d) GOTO HERE 4. True or false. Assembler directives are not used by the CPU itself. They are simply a guide to the assembler. 5. In Question 3, which one is an assembler directive?

A Simple PIC Application


Input push buttons (active low) Output port B Output LEDs

Clear Count

Input port A

PIC 16F84
RC clock +5V CLKIN MCLR
Block diagram

A Simple PIC Application


10k 10k
4 18 14 13 12 11 10

cont

+5V

Count

17

Clear

10k
16

PIC 9 16F84 8
7 5 6

2n Clock circuit

220 0V

Circuit diagram

A Simple PIC Application


Program 1 Initialize Port
reset

cont

Clear output port


start Yes

Reset?
No No

Run?
Yes

Increment output

Flowchart

A Simple PIC Application

cont

Assembly program

Assembling & Linking a PIC Program


Editor Program
myfile.asm

Editor Assembler Program


myfile.err .lib additional library files myfile.o .o additional object files .lkr linker script files

Linker Program

myfile.out

myfile.cod

myfile.hex

myfile.map

myfile.lst

Download to PICs ROM

List File
00000000 00001 allout 00000005 00002 porta 00000006 00003 portb 00004 0000 3000 00005 0001 0066 00006 00007 0002 0186 00008 reset 0003 1C05 00009 start 0004 2802 00010 0005 1885 00011 0006 2803 00012 0007 0A86 00013 0008 2803 00014 00015 00016 EQU EQU EQU 00 05 06 ;Define Data Direction Code ;Port A data register ;Port B data register ;Load W with Direction Code ;Send code to direction register ;Start output at 00000000 ;Test R0 input button ;and reset Port B if pressed ;Test R1 input button ;and run count if pressed ;Increase output by 1 ;Repeat main loop ;Terminate Program

MOVLW allout TRIS portb CLRF BTFSS GOTO BTFSC GOTO INCF GOTO END portb porta,0 reset porta,1 start portb start

Flowchart
PIC Program Convert specification into algorithm/flowchart
Yes

Logical Error?
No

Edit/write source code

Download hex code to chip Assemble program Test in target hardware


Syntax error?
No Yes

Yes

Functional Error?
No

Test hex code in simulator

Done A

Review
1. True or false. The extension for the source file is asm.

2. Which of the following files can be produced by the text editor?


(a) myprog.asm (d) myprog.lst (b) myprog.o (e) myprog.err (c) myprog.hex

3. Which of the following files is produced by an assembler?


(a) myprog.asm (d) myprog.lst (b) myprog.o (e) myprog.err (c) myprog.hex

Review
4. Which of the following files lists syntax errors?
(a) myprog.asm (d) myprog.lst (b) myprog.o (e) myprog.err (c) myprog.hex

Subroutine
Subprogram that represents a set of instructions begin with a label & end with the instruction return or retlw. Executed when call subprogram_name is encountered in program. Can be located anywhere in the program, regardless of the lines in which it is called

Subroutine
;MAIN program calling subroutines ORG 0 MAIN CALL SUBR_1 CALL SUBR_2 CALL SUBR_3 HERE GOTO HERE ;-------end of MAIN ; SUBR_1 RETURN ;--------end of subroutine 1 ; SUBR_2 RETURN ;--------end of subroutine 2 ; SUBR_3 RETURN ;--------end of subroutine 3 END

cont

;stay here

;end of the asm file

Example
Program 1 Initialize Port
reset

PortB = 8-bit output RA0, RA1 = input

Clear output port


start Yes

Reset?
10k 10k

+5V
14 13 12 11 10

No

4 18

Run?
Yes

No

Count

17

Clear

10k
16

PIC 9 16F84 8
7 5 6

Increment output Delay

2n Clock circuit

220 0V

Example

Example

Example
Write a program to count up from 00 to FFH, save the count value at location 10H (GPR RAM address), then send the count value to SFR of Port B. Use one CALL subroutine for sending the data to Port B and another one for time delay. Put a time delay in between each issuing of data to Port B.

Example

Example
LOC OBJECT CODE VALUE LINE SOURCE TEXT

00000006 00000010 00000011

0000 3000 0001 0066 0002 0190 0003 2??? 0004 2???

0005 0006 0007 0008 0009

0A90 0810 0086 2??? 0008

00001 PORTB EQU 06H ;PortB data register 00002 COUNT EQU 10H ;GPR register 00003 MYREG EQU 11H 00004 00005 ORG 0H 00006 movlw B'00000000' 00007 tris PORTB 00008 00009 CLRF COUNT ;COUNT = 0 00010 BACK CALL DISPLAY 00011 GOTO BACK 00012 00013 ;increase value & send it to PORTB subroutine 00014 DISPLAY INCF COUNT,F ;count = count + 1 00015 MOVF COUNT,W 00016 MOVWF PORTB 00017 CALL DELAY 00018 RETURN ;return to caller 00019

Example
00020 ;delay subroutine 00021 0030 30FF 00022 DELAY 0031 0091 00023 0032 0000 00024 AGAIN 0033 0000 00025 0034 0000 00026 0035 0B91 00027 0036 2??? 00028 0037 0008 00029 00030 ORG MOVLW MOVWF NOP NOP NOP DECFSZ GOTO RETURN END 30H 0xFF MYREG ;put delay at address 30H ;WREG = 255 ;no operation wastes clock cycles

MYREG,F ;decrease until MYREG becomes 0 AGAIN ;repeat decrement process ;return to caller

Before any CALL


4 4

After CALL DISPLAY


4

After CALL DELAY


4

After DELAY RETURN


4

After DISLAY RETURN


3
2

3
2 1 13-bit

3
2 1

3
2

0004
13-bit

0009 0004
13-bit

2 1

0004
13-bit

1 13-bit

Review
1. How wide is the size of the stack in the PIC16?
13-bit

2. With each CALL instruction, the stack pointer incremented (incremented, register, SP is ___________ decremented). 3. With each RETURN instruction, the SP is decremented (incremented, decremented). ___________

Review
1 as the 4. On power-up, the PIC uses location ____ first location of the stack.

5. How deep is the size of the stack in the PIC16?


8 levels

Macro

A group of instruction performs a task that is used repeatedly To reduce time to write code and possibility of errors Its name is used as an instruction in any code
name MACRO ENDM dummy1, dummy2, , dummyN

Macro
MOVLF MACRO K, MYREG MOVLW K MOVWF MYREG ENDM
1. MOVLF 0x55, 0x20

cont

;send value 55H to loc 20H

2. VAL_1 EQU 0x55 RAM_LOC EQU 0x20 MOVLF VAL_1, RAM_LOC 3. MOVLF 0x55, PORTB ;send value 55H to Port B

Local Directive

To declare label or name in the body of macro Rules to declare label in macro:

1. All labels in the label field must be declared LOCAL. 2. The LOCAL directive must be right after the MACRO directive. 3. The LOCAL directive can be used to declare all names and labels as follows:
LOCAL @ LOCAL LOCAL LOCAL name1, name2, name3
name1 name2 name3

Local Directive
DELAY_1 MACRO LOCAL MOVLW MOVWF NOP NOP NOP NOP DECFSZ GOTO ENDM V1, TREG BACK V1 TREG

cont

BACK

TREG, F BACK

Macro vs subroutine
Macro: Increase code size every time they are invoked Subroutine: Use stack space when it is called Cause problem in nested calls

Execution Time
Label delay down Instruction MOVLW MOVWF DECFSZ GOTO RETURN Operand 0xFF timer timer down Time (cycles) 1 + 1 + (1x255) + (2x254) + 2 Total
If then and and Clock frequency Instruction Frequency Instruction Period Total Delay Time = = = = 4MHz 1MHz 1s 767 s

767

Execution Time

cont

Find the size of the delay of the code snippet below if the crystal frequency is 4MHz:
MYREG DELAY AGAIN EQU MOVLM MOVWF NOP NOP DECFSZ GOTO RETURN 0x08 0xFF MYREG Instruction cycle ;use loc 08 as counter 1 1 1 1 1 2 2

MYREG, F AGAIN

Time delay = [(255x5) + 1 + 1 + 2] x 1s = 1279 s The actual time delay should be 1278 s

Program Data Table


Allow

access to elements of a frequently used table with minimum operations Output predefined data bytes Add an indexed pointer value to modify the program counter register

Program Data Table


PCL PORTB STATUS timer point EQU EQU EQU EQU EQU 02 06 03 0C 0D

cont

;Register Label Equates...................................... ;Program Counter Low Register ;Port B Data Register ;STATUS Register ;GPR1 used as delay counter ;GPR2 used as table pointer

;************************************************************ ORG 000 GOTO start ;Jump to start of main program ;Define DELAY subroutine..................................... delay MOVLW MOVWF DECFSZ GOTO RETURN FF timer timer down ;Delay count literal ;loaded into spare register ;Decrement timer register ;and repeat until zero ;then return to the main program

down

Program Data Table


table ADDWF RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW RETLW PCL 000 001 003 007 00F 01F 03F 07F 0FF ;Add pointer to PCL ;0 LEDS on ;1 LEDS on ;2 LEDS on ;3 LEDS on ;4 LEDS on ;5 LEDS on ;6 LEDS on ;7 LEDS on ;8 LEDS on

cont

;Define Table of Output Codes....................................

;Initialize Port B(Port A defaults to inputs).................... start MOVLW TRIS b'00000000' PORTB ;Set Port B Data Direction Code ;and load into TRISB

Program Data Table


nexton MOVLW SUBWF BTFSC GOTO MOVF CALL MOVWF CALL INCF GOTO END 09 point,W STATUS,2 newbar point,W table PORTB delay point nexton

cont

;Main loop................................................... newbar CLRF point ;Reset pointer to start of table ;Check if all outputs done yet ;(note: destination W) ;and start a new bar ;if true... ;Set pointer to ;access table... ;and output to LEDs ;wait a while... ;Point to next table value ;and repeat... ;Terminate source code

Exercise
A switch is connected to pin RB3. Write a program to check the status of the switch and perform the following: (a) If switch = 0, send letter N to port B. (b) If switch = 1, send letter Y to port B.

Exercise

You might also like