Upload
mkazree
View
7.791
Download
3
Embed Size (px)
Citation preview
Assembly Assembly Language Language
Programming for Programming for PICPIC
Introduction Communication between human &
microcontroller.
11 00xx 0010 0000
Program.hex (machine language)assembler/
translator programmer
MOVLW 0x20
Program.asm
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 = 25HADDLW 0x11 ;WREG = 25H + 11H = 36HADDLW 12H ;WREG = 36H + 12H = 48HADDLW H’2A’ ;WREG = 48H +2AH = 72HADDLW 2CH ;WREG = 72H + 2CH = 9EH
MOVLW E5H ;invalid, it must be MOVLW 0E5H
ADDLW C6H ;invalid, it must be ADDLW 0C6
Review1. 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 ProgrammingBasic 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 5six equ 6seven equ 7
Control Directives cont…
Using EQU for fixed data assignmentUsing EQU for fixed data assignment;in hexadecimalDATA1 EQU 39 ;hex data is the defaultDATA2 EQU 0x39 ;another way for hexDATA3 EQU 39h ;another way for hexDATA4 EQU H’39’ ;another way for hexDATA5 EQU h’39’ ;another way for hex
;in binaryDATA6 EQU b’00110101’;binary (35 in hex)DATA7 EQU B’00110101’;binary (35 in hex)
Control Directives cont…
;in decimalDATA8 EQU D’28’ ;decimal numbers (1C in hex)DATA9 EQU d’28’ ;second way for decimal
;in ASCIIDATA10 EQU A’2’ ;ASCII charactersDATA11 EQU a’2’ ;another way for ASCII charDATA12 EQU ‘2’ ;another way for ASCII char
Control Directives cont…
Using EQU for SFR address assignmentUsing EQU for SFR address assignment
COUNTER EQU 0x00 ;counter value 00PORTB EQU 0x06 ;Port B addressMOVLW COUNTER ;WREG = 00HMOVWF PORTB ;Port B now has 00 tooINCF PORTB, F ;Port B has 01INCF PORTB, F ;Port B has 02INCF PORTB, F ;Port B has 03
Control Directives cont…
Using EQU for RAM address assignmentUsing 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 = 22HADDWF MYREG, F ;MYREG = WREG + MYREGADDWF MYREG, F ;MYREG = WREG + MYREGADDWF MYREG, F ;MYREG = WREG + MYRG
Control Directives cont…
ORG Defines an address from which the program 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 0xFFmovwf PORTBend
; Start main loop;...............................................................
reset CLRF 06 ;Clear Port B Data
start BTFSS 05,0 ;Test RA0 input buttonGOTO reset ;and reset port B if pressedBTFSC 05,01 ;Test RA1 input buttonGOTO start ;and run count if pressed
INCF 06 ;increment count at Port BMOVLW 0FF ;Delay count literalCALL delay ;Jump to subroutine 'delay'
GOTO start ;Repeat main loop alwaysEND ;Terminate source code
Label
Command Operand
Comment
; Start main loop;...............................................................
reset CLRF 06 ;Clear Port B Data
start BTFSS 05,0 ;Test RA0 input buttonGOTO reset ;and reset port B if pressedBTFSC 05,01 ;Test RA1 input buttonGOTO start ;and run count if pressed
INCF 06 ;increment count at Port BMOVLW 0FF ;Delay count literalCALL delay ;Jump to subroutine 'delay'
GOTO start ;Repeat main loop alwaysEND ;Terminate source code
Label
Command Operand
Comment
Review1. _______ 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
PIC16F84
Inputport A
Outputport B
RCclock
+5V
OutputLEDs
Input push buttons(active low)
Clear
Count
CLKIN
MCLR
PIC16F84
Inputport A
Outputport B
RCclock
+5V
OutputLEDs
Input push buttons(active low)
Clear
Count
CLKIN
MCLR
Block diagram
A Simple PIC Application cont…
Clockcircuit
10kClear
Count
2n
10k 10k
2200V
+5V
PIC16F84
144
18
17
16
5 6
7
8
9
10
11
12
13
Clockcircuit
10kClear
Count
2n
10k 10k
2200V
+5V
PIC16F84
144
18
17
16
5 6
7
8
9
10
11
12
13
Circuit diagram
A Simple PIC Application cont…
Flowchart
Program 1
Initialize Port
Clear output port
Increment output
start
Reset?
Run?
reset
Yes
No
Yes
No
A Simple PIC Application cont…
Assembly program
Assembling & Linking a PIC Program
EditorProgram
Editor AssemblerProgram
LinkerProgram
Download to PIC’s ROM
myfile.asm
myfile.omyfile.err.lib additional
library files.o additionalobject files.lkr linkerscript files
myfile.out myfile.cod myfile.hex myfile.map myfile.lst
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 Code0001 0066 00006 TRIS portb ;Send code to direction register 00007 0002 0186 00008 reset CLRF portb ;Start output at 000000000003 1C05 00009 start BTFSS porta,0 ;Test R0 input button0004 2802 00010 GOTO reset ;and reset Port B if pressed0005 1885 00011 BTFSC porta,1 ;Test R1 input button0006 2803 00012 GOTO start ;and run count if pressed0007 0A86 00013 INCF portb ;Increase output by 10008 2803 00014 GOTO start ;Repeat main loop 00015 00016 END ;Terminate Program
FlowchartPIC Program
Convert specification into algorithm/flowchart
Edit/write source code
Assemble program
Syntax error?
Test hex code in simulator
LogicalError?
A
A
Download hex code to chip
Test in target hardware
FunctionalError?
Done
YesYes
Yes
No
No
No
Review1. 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 (b) myprog.o (c) myprog.hex(d) myprog.lst (e) myprog.err
3. Which of the following files is produced by an assembler?(a) myprog.asm (b) myprog.o (c) myprog.hex(d) myprog.lst (e) myprog.err
Review4. Which of the following files lists syntax errors?
(a) myprog.asm (b) myprog.o (c) myprog.hex(d) myprog.lst(e) myprog.err
SubroutineSubprogram that represents a set of
instructionsbegin 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 subroutinesORG 0
MAIN CALL SUBR_1CALL SUBR_2CALL 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 3END ;end of the asm file
ExampleProgram 1
Initialize Port
Clear output port
Increment output
start
Reset?
Run?
reset
Yes
No
Yes
No
Delay
PortB = 8-bit outputRA0, RA1 = input
Clockcircuit
10kClear
Count
2n
10k 10k
2200V
+5V
PIC16F84
144
18
17
16
5 6
7
8
9
10
11
12
13
Clockcircuit
10kClear
Count
2n
10k 10k
2200V
+5V
PIC16F84
144
18
17
16
5 6
7
8
9
10
11
12
13
Example
Example
ExampleWrite 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
ExampleLOC 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 0H0000 3000 00006 movlw B'00000000'0001 0066 00007 tris PORTB 00008 0002 0190 00009 CLRF COUNT ;COUNT = 00003 2??? 00010 BACK CALL DISPLAY0004 2??? 00011 GOTO BACK 00012 00013 ;increase value & send it to PORTB subroutine0005 0A90 00014 DISPLAY INCF COUNT,F ;count = count + 10006 0810 00015 MOVF COUNT,W0007 0086 00016 MOVWF PORTB0008 2??? 00017 CALL DELAY0009 0008 00018 RETURN ;return to caller 00019
Example
13-bit
Before any CALL
1
2
3
4
000413-bit
AfterCALL
DISPLAY
1
2
3
4
00040009
13-bit
AfterCALL DELAY
1
2
3
4
000413-bit
After DELAY
RETURN
1
2
3
4
13-bit
After DISLAY
RETURN
1
2
3
4
00020 ;delay subroutine 00021 ORG 30H ;put delay at address 30H0030 30FF 00022 DELAY MOVLW 0xFF ;WREG = 2550031 0091 00023 MOVWF MYREG0032 0000 00024 AGAIN NOP ;no operation wastes clock cycles0033 0000 00025 NOP0034 0000 00026 NOP0035 0B91 00027 DECFSZ MYREG,F ;decrease until MYREG becomes 00036 2??? 00028 GOTO AGAIN ;repeat decrement process0037 0008 00029 RETURN ;return to caller 00030 END
Review1. How wide is the size of the stack in the PIC16?
2. With each CALL instruction, the stack pointer register, SP is ___________ (incremented, decremented).
3. With each RETURN instruction, the SP is ___________ (incremented, decremented).
13-bit
incremented
decremented
Review4. On power-up, the PIC uses location ____ as the
first location of the stack.
5. How deep is the size of the stack in the PIC16?
1
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, MYREGMOVLW KMOVWF MYREGENDM
1. MOVLF 0x55, 0x20 ;send value 55H to loc 20H
2. VAL_1 EQU 0x55RAM_LOC EQU 0x20MOVLF 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 name1LOCAL name2LOCAL name3
Local Directive cont…
DELAY_1 MACRO V1, TREGLOCAL BACKMOVLW V1MOVWF TREG
BACK NOPNOPNOPNOPDECFSZ TREG, FGOTO BACKENDM
Macro vs subroutineMacro: Increase code size every time they are invoked
Subroutine:Use stack space when it is calledCause problem in nested calls
Execution Time Label Instruction Operand Time (cycles)
delay MOVLW 0xFFMOVWF timer
down DECFSZ timerGOTO downRETURN
1+ 1
+ (1x255)+ (2x254)
+ 2
Total 767If Clock frequency = 4MHzthen Instruction Frequency = 1MHzand Instruction Period = 1sand 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:
MYREG EQU 0x08 ;use loc 08 as counterDELAY MOVLM 0xFF
MOVWF MYREGAGAIN NOP
NOPDECFSZ MYREG, FGOTO AGAINRETURN
Instruction cycle
1111122
Time delay = [(255x5) + 1 + 1 + 2] x 1s = 1279 sThe actual time delay should be 1278 s
Program Data TableAllow access to elements of a frequently
used table with minimum operationsOutput predefined data bytesAdd an indexed pointer value to modify the
program counter register
Program Data Table cont…
;Register Label Equates......................................
PCL EQU 02 ;Program Counter Low RegisterPORTB EQU 06 ;Port B Data RegisterSTATUS EQU 03 ;STATUS Registertimer EQU 0C ;GPR1 used as delay counterpoint EQU 0D ;GPR2 used as table pointer
;************************************************************ORG 000GOTO start ;Jump to start of main program
;Define DELAY subroutine.....................................
delay MOVLW FF ;Delay count literalMOVWF timer ;loaded into spare register
down DECFSZ timer ;Decrement timer registerGOTO down ;and repeat until zeroRETURN ;then return to the main program
Program Data Table cont…
;Define Table of Output Codes....................................
table ADDWF PCL ;Add pointer to PCLRETLW 000 ;0 LEDS onRETLW 001 ;1 LEDS onRETLW 003 ;2 LEDS onRETLW 007 ;3 LEDS onRETLW 00F ;4 LEDS onRETLW 01F ;5 LEDS onRETLW 03F ;6 LEDS onRETLW 07F ;7 LEDS onRETLW 0FF ;8 LEDS on
;Initialize Port B(Port A defaults to inputs)....................
start MOVLW b'00000000' ;Set Port B Data Direction CodeTRIS 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 yetSUBWF point,W ;(note: destination W)BTFSC STATUS,2 ;and start a new barGOTO newbar ;if true...
MOVF point,W ;Set pointer toCALL table ;access table...MOVWF PORTB ;and output to LEDs
CALL delay ;wait a while...
INCF point ;Point to next table valueGOTO 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