Chp6 assembly language programming for pic   copy
Upcoming SlideShare
Loading in...5
×
 

Chp6 assembly language programming for pic copy

on

  • 5,396 views

 

Statistics

Views

Total Views
5,396
Views on SlideShare
5,396
Embed Views
0

Actions

Likes
4
Downloads
316
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Chp6 assembly language programming for pic   copy Chp6 assembly language programming for pic copy Presentation Transcript

  • Assembly Language Programming for PIC
  • 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 = 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 &quot;file_name&quot;  Example: #include <regs.h> #include &quot;subprog.asm&quot;
  • 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… 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 0xFF movwf PORTB end
  •  
  • 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 Block diagram
  • A Simple PIC Application cont… Circuit diagram
  • A Simple PIC Application cont… Flowchart
  • A Simple PIC Application cont… Assembly program
  • Assembling & Linking a PIC Program Editor Program Editor Assembler Program Linker Program Download to PIC’s ROM myfile.asm myfile.o myfile.err .lib additional library files .o additional object files .lkr linker script files myfile.out myfile.cod myfile.hex myfile.map myfile.lst
  • List File
    • 0000 0000 00001 allout EQU 00 ;Define Data Direction Code
    • 0000 0005 00002 porta EQU 05 ;Port A data register
    • 0000 0006 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
  • 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
        
  • 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
     
  • 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 PortB = 8-bit output RA0, RA1 = input
  • 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 13-bit Before any CALL 1 2 3 4 0004 13-bit After CALL DISPLAY 1 2 3 4 0004 0009 13-bit After CALL DELAY 1 2 3 4 0004 13-bit After DELAY RETURN 1 2 3 4 13-bit After DISLAY RETURN 1 2 3 4
  • 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
  • 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
  • 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
  • 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 counter
    • DELAY MOVLM 0xFF
    • MOVWF MYREG
    • AGAIN NOP
    • NOP
    • DECFSZ MYREG, F
    • GOTO AGAIN
    • RETURN
    Instruction cycle 1 1 1 1 1 2 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…
  • Program Data Table cont…
  • Program Data Table cont…
  • Exercise
    • A switch is connected to pin RB3. Write a program to check the status of the switch and perform the following:
    • If switch = 0, send letter ‘N’ to port B.
    • If switch = 1, send letter ‘Y’ to port B.
  • Exercise