0% found this document useful (0 votes)
0 views

Lecture 5

The document provides an overview of assembly language programming for PIC microcontrollers, detailing the structure of assembly programs, including labels, instructions, operands, directives, and comments. It explains various number representations (hexadecimal, decimal, binary, ASCII) and introduces control directives like #DEFINE, #INCLUDE, and EQU for defining constants and including files. Additionally, it covers subroutines, macros, and the process of assembling and linking a PIC program, along with examples and reviews to reinforce understanding.

Uploaded by

vinod SALUNKHE
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture 5

The document provides an overview of assembly language programming for PIC microcontrollers, detailing the structure of assembly programs, including labels, instructions, operands, directives, and comments. It explains various number representations (hexadecimal, decimal, binary, ASCII) and introduces control directives like #DEFINE, #INCLUDE, and EQU for defining constants and including files. Additionally, it covers subroutines, macros, and the process of assembling and linking a PIC program, along with examples and reviews to reinforce understanding.

Uploaded by

vinod SALUNKHE
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 54

Assembly Language

Programming for PIC


Introduction
 Communication between human &
microcontroller.

Program.asm

Program.hex
assembler/ (machine language)
translator 11 00xx 0010 0000 programmer

MOVLW 0x20
Representation Numbers in
Assembler
 Hexadecimal:
MOVLW 99H, MOVLW 0x99, MOVLW 99, MOVLW h’99’
 ASCII:
MOVLW A’2’
;WREG = 00110010 or 32 in hex
 Decimal:
MOVLW D’12’, MOVLW .12
;WREG = 00001100 = 0CH = 12
 Binary:
MOVLW B’10011001’
;WREG = 1001101 or 99 in hex
Representation Numbers in
Assembler cont…

MOVLW 25 ;WREG = 25H


ADDLW 0x11 ;WREG = 25H + 11H = 36H
ADDLW 12H ;WREG = 36H + 12H = 48H
ADDLW H’2A’ ;WREG = 48H +2AH = 72H
ADDLW 2CH ;WREG = 72H + 2CH = 9EH

MOVLW E5H ;invalid, it must be MOVLW 0E5H


ADDLW C6H ;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 cont…

EQU Defining assembler constant

Syntax:
<name_constant> equ <value>

Example:
five equ 5
six equ 6
seven equ 7
Control Directives cont…

Using EQU for fixed data assignment


;in hexadecimal
DATA1 EQU 39 ;hex data is the default
DATA2 EQU 0x39 ;another way for hex
DATA3 EQU 39h ;another way for hex
DATA4 EQU H’39’ ;another way for hex
DATA5 EQU h’39’ ;another way for hex

;in binary
DATA6 EQU b’00110101’ ;binary (35 in hex)
DATA7 EQU B’00110101’ ;binary (35 in hex)
Control Directives cont…

;in decimal
DATA8 EQU D’28’ ;decimal numbers (1C in hex)
DATA9 EQU d’28’ ;second way for decimal

;in ASCII
DATA10 EQU A’2’ ;ASCII characters
DATA11 EQU a’2’ ;another way for ASCII char
DATA12 EQU ‘2’ ;another way for ASCII char
Control Directives cont…

Using EQU for SFR address assignment

COUNTER EQU 0x00 ;counter value 00


PORTB EQU 0x06 ;Port B address
MOVLW COUNTER ;WREG = 00H
MOVWF PORTB ;Port B now has 00 too
INCF PORTB, F ;Port B has 01
INCF PORTB, F ;Port B has 02
INCF PORTB, F ;Port B has 03
Control Directives cont…

Using EQU for RAM address assignment

MYREG EQU 0x12 ;assign RAM location to MYREG

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 cont…

Defines an address from which the program


ORG is stored in C memory

Syntax:
<label>org<value>

Example:
Start org 0×00
movlw 0xFF
movwf PORTB
Control Directives cont…

END End of program

Syntax:
End

Example:
.
.
movlw 0xFF
movwf PORTB
end
Command Operand
; Start main loop
;...............................................................

reset CLRF 06 ;Clear Port B Data

start BTFSS 05,0 ;Test RA0 input button


GOTO reset ;and reset port B if pressed
BTFSC 05,01 ;Test RA1 input button
GOTO start ;and run count if pressed

INCF 06 ;increment count at Port B


MOVLW 0FF ;Delay count literal
Label CALL delay ;Jump to subroutine 'delay'

GOTO start ;Repeat main loop always


END ;Terminate source code

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?
Assembling & Linking a PIC
Program
Editor
Program
myfile.asm

Editor
Assembler
Program
myfile.err myfile.o
.o additional
.lib additional
object files
library files Linker
Program .lkr linker
script files

myfile.out myfile.cod myfile.hex myfile.map myfile.lst

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

A
Convert specification into
algorithm/flowchart
Logical
Error?
Yes
Edit/write source code No

Download hex code to chip


Assemble program

Test in target hardware


Syntax
error? Yes
Yes
Functional
No
Error?
Test hex code in simulator No

Done
A
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 cont…

;MAIN program calling subroutines


ORG 0
MAIN CALL SUBR_1
CALL SUBR_2
CALL SUBR_3
HERE GOTO HERE ;stay 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 ;end of the asm file
Example
Program 1

Initialize Port PortB = 8-bit output


RA0, RA1 = input
reset

Clear output port


start

Yes
Reset?
No

No
Run?
Yes

Increment output

Delay
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 LINE SOURCE TEXT
VALUE

00000006 00001 PORTB EQU 06H ;PortB data register


00000010 00002 COUNT EQU 10H ;GPR register
00000011 00003 MYREG EQU 11H
00004
00005 ORG 0H
0000 3000 00006 movlw B'00000000'
0001 0066 00007 tris PORTB
00008
0002 0190 00009 CLRF COUNT ;COUNT = 0
0003 2??? 00010 BACK CALL DISPLAY
0004 2??? 00011 GOTO BACK
00012
00013 ;increase value & send it to PORTB subroutine
0005 0A90 00014 DISPLAY INCF COUNT,F ;count = count + 1
0006 0810 00015 MOVF COUNT,W
0007 0086 00016 MOVWF PORTB
0008 2??? 00017 CALL DELAY
0009 0008 00018 RETURN ;return to caller
00019
Example
00020 ;delay subroutine
00021 ORG 30H ;put delay at address 30H
0030 30FF 00022 DELAY MOVLW 0xFF ;WREG = 255
0031 0091 00023 MOVWF MYREG
0032 0000 00024 AGAIN NOP ;no operation wastes clock cycles
0033 0000 00025 NOP
0034 0000 00026 NOP
0035 0B91 00027 DECFSZ MYREG,F ;decrease until MYREG becomes 0
0036 2??? 00028 GOTO AGAIN ;repeat decrement process
0037 0008 00029 RETURN ;return to caller
00030 END

After After After After


Before CALL CALL DELAY DISLAY
any CALL DISPLAY DELAY RETURN RETURN
4 4 4 4 4
3 3 3 3 3
2 2 2 0009 2 2
1 1 0004 1 0004 1 0004 1
13-bit 13-bit 13-bit 13-bit 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


register, SP is ___________
incremented(incremented,
decremented).

3. With each RETURN instruction, the SP is


___________
decremented (incremented, decremented).
Review
1 the first
4. On power-up, the PIC uses location ____ as
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 dummy1, dummy2, …, dummyN




ENDM
Macro cont…

MOVLF MACRO K, MYREG


MOVLW K
MOVWF MYREG
ENDM

1. MOVLF 0x55, 0x20 ;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 name1, name2, name3
@
LOCAL name1
LOCAL name2
LOCAL name3
Local Directive cont…

DELAY_1 MACRO V1, TREG


LOCAL BACK
MOVLW V1
MOVWF TREG
BACK NOP
NOP
NOP
NOP
DECFSZ TREG, F
GOTO BACK
ENDM
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 Instruction Operand Time (cycles)

delay MOVLW 0xFF


1
MOVWF timer + 1
down DECFSZ timer + (1x255)
GOTO down + (2x254)
RETURN + 2

Total 767
If Clock frequency = 4MHz
then Instruction Frequency = 1MHz
and Instruction Period = 1s
and Total Delay Time = 767 s
Execution Time cont…

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

Time delay = [(255x5) + 1 + 1 + 2] x 1s = 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 cont…

;Register Label Equates......................................

PCL EQU 02 ;Program Counter Low Register


PORTB EQU 06 ;Port B Data Register
STATUS EQU 03 ;STATUS Register
timer EQU 0C ;GPR1 used as delay counter
point EQU 0D ;GPR2 used as table pointer

;************************************************************
ORG 000
GOTO start ;Jump to start of main program

;Define DELAY subroutine.....................................

delay MOVLW FF ;Delay count literal


MOVWF timer ;loaded into spare register

down DECFSZ timer ;Decrement timer register


GOTO down ;and repeat until zero
RETURN ;then return to the main program
Program Data Table cont…

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

table ADDWF PCL ;Add pointer to PCL


RETLW 000 ;0 LEDS on
RETLW 001 ;1 LEDS on
RETLW 003 ;2 LEDS on
RETLW 007 ;3 LEDS on
RETLW 00F ;4 LEDS on
RETLW 01F ;5 LEDS on
RETLW 03F ;6 LEDS on
RETLW 07F ;7 LEDS on
RETLW 0FF ;8 LEDS on

;Initialize Port B(Port A defaults to inputs)....................

start MOVLW b'00000000' ;Set Port B Data Direction Code


TRIS PORTB ;and load into TRISB
Program Data Table cont…

;Main loop...................................................
newbar CLRF point ;Reset pointer to start of table

nexton MOVLW 09 ;Check if all outputs done yet


SUBWF point,W ;(note: destination W)
BTFSC STATUS,2 ;and start a new bar
GOTO newbar ;if true...

MOVF point,W ;Set pointer to


CALL table ;access table...
MOVWF PORTB ;and output to LEDs

CALL delay ;wait a while...

INCF point ;Point to next table value


GOTO nexton ;and repeat...

END ;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
PIC18 Addressing Modes
-Register direct: Use an 8-bit value to specify a data register.

movwf 0x20,A ; the value 0x20 is register direct mode

-Immediate Mode : A value in the instruction to be used as an operand

addlw 0x10 ; add hex value 0x10 to WREG

movlw 0x30 ; load 0x30 into WREG

-Inherent Mode: an implied operand

CLRW ; Clears WREG (WREG = 0x00). The operand is implicit.

DAW ; decimal adjust working register;

RET ; Returns from a subroutine. No operand required.


-Indirect Mode: A special function register (FSRx) is used as a
pointer to the actual data register.

Ex.

LFSR 0, 0x30 ; Load FSR0 with address 0x30 (pointing to memory location)
MOVF INDF0, W ; Move data from the location pointed by FSR0 into WREG
PIC18 Instruction Examples

Data Movement Instruction

lfsr FSR1,0xB00 movf ; place the value 0xB00 in FSR1

PRODL,W movff ; copy PRODL into WREG

; copy data register 0x100 to data register 0x300


0x100,0x300

movwf PRODL,A swapf ; copy WREG to PRODL

PRODL,F movb ; swap the upper and lower 4 bits of PRODL

; load 3 into BSR


3

movlw 0x10 ; WREG  0x10


The PIC18 Microcontroller

Add Instructions

addwf 0x20,F,A ; add data register and WREG and place sum in WREG

addwfc ; add WREG, PRODL, and carry and leave sum


; in WREG
PRODL,W,A
addlw 0x5 ; increment WREG by 5

Subtract Instructions

subfwb PRODL,F ; PRODL  [WREG] – [PRODL] – borrow flag

subwf PRODH,W ; WREG  [PRODH] – [WREG]

subwfb 0x10,F,A ; 0x10  [0x10] – [WREG] – borrow flag

sublw 0x10 ; WREG  0x10 – [WREG]


The PIC18 Microcontroller

RISC CISC

Simple instruction set Complex instruction set

Regular and fixed instruction format Irregular instruction format

Simple address modes Complex address modes

Pipelined instruction execution May also pipeline instruction execution

Separated data and program memory Combined data and program memory

Most operations are register to register Most operations can be register to memory

Take shorter time to design and debug Take longer time to design and debug

Provide large number of CPU registers Provide smaller number of CPU registers

You might also like