+ All Categories

asembly

Date post: 08-Nov-2014
Category:
Upload: ashwin-kumar-chandran
View: 26 times
Download: 5 times
Share this document with a friend
Description:
programming
Popular Tags:
55
Assembly Language Assembly Language Programming for Programming for PIC PIC
Transcript
Page 1: asembly

Assembly Assembly Language Language

Programming for Programming for PICPIC

Page 2: asembly

Introduction Communication between human &

microcontroller.

11 00xx 0010 0000

Program.hex (machine language)assembler/

translator programmer

MOVLW 0x20

Program.asm

Page 3: asembly

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

Page 4: asembly

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

Page 5: asembly

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.

Page 6: asembly

PIC Assembly Programming

Basic elements: Label Instruction (mnemonic) Operand(s) Directive Comment

Structure:

[label] mnemonic [operands] [;comment]

Page 7: asembly

Labels

Allows the program to refer to a line of code or section of program by name

Marco, branching, goto

Page 8: asembly

Instructions

The way we write an instruction (syntax)

Page 9: asembly

Operands

Instruction element for the instruction that is being executed

Registers / variables / constants

Page 10: asembly

Comments

Begin with semicolon (;) Series of words that a programmer writes to

make the program more clear & legible

Page 11: asembly

Directives

Similar to an instruction, but unlike an instruction it is independent on the microcontroller model

Represent a characteristic of the assembly language itself

Page 12: asembly

Control Directives

#DEFINE Exchange one part of text for another

Syntax:#define<text> [<another text>]

Example:#define turned_on 1#define turned_off 0

Page 13: asembly

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"

Page 14: asembly

Control Directives cont…

EQU Defining assembler constant

Syntax:<name_constant> equ <value>

Example:five equ 5six equ 6seven equ 7

Page 15: asembly

Control Directives cont…

Using EQU for fixed data assignmentUsing EQU for fixed data assignment;in hexadecimal

DATA1 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)

Page 16: asembly

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

Page 17: asembly

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

Page 18: asembly

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

Page 19: asembly

Control Directives cont…

ORGDefines an address from which the

program is stored in C memory

Syntax:<label>org<value>

Example:Start org   0×00

     movlw 0xFF movwf PORTB

Page 20: asembly

Control Directives cont…

END End of program

Syntax:End

Example:..

movlw 0xFFmovwf PORTBend

Page 21: asembly

; 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

Page 22: asembly

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?

Page 23: asembly

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

Page 24: asembly

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

Page 25: asembly

A Simple PIC Application cont…

Flowchart

Program 1

Initialize Port

Clear output port

Increment output

start

Reset?

Run?

reset

Yes

No

Yes

No

Page 26: asembly

A Simple PIC Application cont…

Assembly program

Page 27: asembly

Assembling & Linking a PIC Program

EditorProgram

Editor AssemblerProgram

LinkerProgram

Download to PIC’s ROM

myfile.asm

myfile.omyfile.err

.lib additionallibrary files

.o additionalobject files

.lkr linkerscript files

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

Page 28: asembly

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

Page 29: asembly

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

Page 30: asembly

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 (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

Page 31: asembly

Review

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

Page 32: asembly

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

Page 33: asembly

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

Page 34: asembly

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

Page 35: asembly

Example

Page 36: asembly

Example

Page 37: asembly

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.

Page 38: asembly

Example

Page 39: asembly

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

Page 40: asembly

Example

13-bit

Before any CALL

1

2

3

4

0004

13-bit

AfterCALL

DISPLAY

1

2

3

4

0004

0009

13-bit

AfterCALL DELAY

1

2

3

4

0004

13-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

Page 41: asembly

Review

1. 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

Page 42: asembly

Review

4. 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

Page 43: asembly

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

Page 44: asembly

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

Page 45: asembly

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

Page 46: asembly

Local Directive cont…

DELAY_1 MACRO V1, TREGLOCAL BACKMOVLW V1MOVWF TREG

BACK NOPNOPNOPNOPDECFSZ TREG, FGOTO BACKENDM

Page 47: asembly

Macro vs subroutine

Macro: Increase code size every time they are invoked

Subroutine:Use stack space when it is calledCause problem in nested calls

Page 48: asembly

Execution Time

Label Instruction Operand Time (cycles)

delay MOVLW 0xFF

MOVWF timer

down DECFSZ timer

GOTO down

RETURN

1+ 1

+ (1x255)+ (2x254)

+ 2

Total 767If Clock frequency = 4MHzthen Instruction Frequency = 1MHzand Instruction Period = 1sand Total Delay Time = 767 s

Page 49: asembly

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

Page 50: asembly

Program Data Table

Allow access to elements of a frequently used table with minimum operations

Output predefined data bytesAdd an indexed pointer value to modify the

program counter register

Page 51: asembly

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

Page 52: asembly

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

Page 53: asembly

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

Page 54: asembly

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.

Page 55: asembly

Exercise


Recommended