Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Chp6 assembly language programming for pic copy


Published on

Published in: Education, Technology

Chp6 assembly language programming for pic copy

  1. 1. Assembly Language Programming for PIC
  2. 2. Introduction <ul><li>Communication between human & microcontroller. </li></ul>11 00xx 0010 0000 Program.hex (machine language) assembler/ translator programmer MOVLW 0x20 Program.asm
  3. 3. Representation Numbers in Assembler <ul><li>Hexadecimal: </li></ul><ul><li>MOVLW 99H, MOVLW 0x99, MOVLW 99, MOVLW h’99’ </li></ul><ul><li>ASCII: </li></ul><ul><li>MOVLW A’2’ </li></ul><ul><li>;WREG = 00110010 or 32 in hex </li></ul><ul><li>Decimal: </li></ul><ul><li>MOVLW D’12’, MOVLW .12 </li></ul><ul><li>;WREG = 00001100 = 0CH = 12 </li></ul><ul><li>Binary: </li></ul><ul><li>MOVLW B’10011001’ </li></ul><ul><li>;WREG = 1001101 or 99 in hex </li></ul>
  4. 4. Representation Numbers in Assembler cont… <ul><li>MOVLW 25 ;WREG = 25H </li></ul><ul><li>ADDLW 0x11 ;WREG = 25H + 11H = 36H </li></ul><ul><li>ADDLW 12H ;WREG = 36H + 12H = 48H </li></ul><ul><li>ADDLW H’2A’ ;WREG = 48H +2AH = 72H </li></ul><ul><li>ADDLW 2CH ;WREG = 72H + 2CH = 9EH </li></ul><ul><li>MOVLW E5H ;invalid, it must be MOVLW 0E5H </li></ul><ul><li>ADDLW C6H ;invalid, it must be ADDLW 0C6 </li></ul>
  5. 5. Review <ul><li>1. Give three ways for hex data representation in the PIC assembler? </li></ul><ul><li>2. Show how to represent decimal 99 in formats of (a) hex, (b) decimal, and (c) binary in the PIC assembler. </li></ul>
  6. 6. PIC Assembly Programming <ul><li>Basic elements: </li></ul><ul><li>Label </li></ul><ul><li>Instruction (mnemonic) </li></ul><ul><li>Operand(s) </li></ul><ul><li>Directive </li></ul><ul><li>Comment </li></ul><ul><li>Structure: </li></ul><ul><li>[label] mnemonic [operands] [;comment] </li></ul>
  7. 7. Labels <ul><li>Allows the program to refer to a line of code or section of program by name </li></ul><ul><li>Marco, branching, goto </li></ul>
  8. 8. Instructions <ul><li>The way we write an instruction (syntax) </li></ul>
  9. 9. Operands <ul><li>Instruction element for the instruction that is being executed </li></ul><ul><li>Registers / variables / constants </li></ul>
  10. 10. Comments <ul><li>Begin with semicolon (;) </li></ul><ul><li>Series of words that a programmer writes to make the program more clear & legible </li></ul>
  11. 11. Directives <ul><li>Similar to an instruction, but unlike an instruction it is independent on the microcontroller model </li></ul><ul><li>Represent a characteristic of the assembly language itself </li></ul>
  12. 12. Control Directives #DEFINE Exchange one part of text for another Syntax: #define<text> [<another text>] Example: #define turned_on 1 #define turned_off 0
  13. 13. 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;
  14. 14. Control Directives cont… EQU Defining assembler constant Syntax: <name_constant> equ <value> Example: five equ 5 six equ 6 seven equ 7
  15. 15. Control Directives cont… <ul><li>Using EQU for fixed data assignment </li></ul><ul><li>;in hexadecimal </li></ul><ul><li>DATA1 EQU 39 ;hex data is the default </li></ul><ul><li>DATA2 EQU 0x39 ;another way for hex </li></ul><ul><li>DATA3 EQU 39h ;another way for hex </li></ul><ul><li>DATA4 EQU H’39’ ;another way for hex </li></ul><ul><li>DATA5 EQU h’39’ ;another way for hex </li></ul><ul><li>;in binary </li></ul><ul><li>DATA6 EQU b’00110101’ ;binary (35 in hex) </li></ul><ul><li>DATA7 EQU B’00110101’ ;binary (35 in hex) </li></ul>
  16. 16. Control Directives cont… <ul><li>;in decimal </li></ul><ul><li>DATA8 EQU D’28’ ;decimal numbers (1C in hex) </li></ul><ul><li>DATA9 EQU d’28’ ;second way for decimal </li></ul><ul><li>;in ASCII </li></ul><ul><li>DATA10 EQU A’2’ ;ASCII characters </li></ul><ul><li>DATA11 EQU a’2’ ;another way for ASCII char </li></ul><ul><li>DATA12 EQU ‘2’ ;another way for ASCII char </li></ul>
  17. 17. Control Directives cont… <ul><li>Using EQU for SFR address assignment </li></ul><ul><li>COUNTER EQU 0x00 ;counter value 00 </li></ul><ul><li>PORTB EQU 0x06 ;Port B address </li></ul><ul><li>MOVLW COUNTER ;WREG = 00H </li></ul><ul><li>MOVWF PORTB ;Port B now has 00 too </li></ul><ul><li>INCF PORTB, F ;Port B has 01 </li></ul><ul><li>INCF PORTB, F ;Port B has 02 </li></ul><ul><li>INCF PORTB, F ;Port B has 03 </li></ul>
  18. 18. Control Directives cont… <ul><li>Using EQU for RAM address assignment </li></ul><ul><li>MYREG EQU 0x12 ;assign RAM location to MYREG </li></ul><ul><li>MOVLW 0 ;clear WREG (WREG = 0) </li></ul><ul><li>MOVWF MYREG ;clear MYREG (loc 12 has 0) </li></ul><ul><li>MOVLW 22H ;WREG = 22H </li></ul><ul><li>ADDWF MYREG, F ;MYREG = WREG + MYREG </li></ul><ul><li>ADDWF MYREG, F ;MYREG = WREG + MYREG </li></ul><ul><li>ADDWF MYREG, F ;MYREG = WREG + MYRG </li></ul>
  19. 19. 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
  20. 20. Control Directives cont… END End of program Syntax: End Example: . . movlw 0xFF movwf PORTB end
  21. 22. Review <ul><li>1. _______ are translated by the assembler into machine code, whereas _______ are not. </li></ul><ul><li>2. True or false. Assembly language is a high-level language. </li></ul><ul><li>3. Which of the following instructions produces opcode? List all that do. </li></ul><ul><li>(a) MOVLW 25H (b) ADDLW 12 </li></ul><ul><li>(b) ORG 2000H (d) GOTO HERE </li></ul><ul><li>4. True or false. Assembler directives are not used by the CPU itself. They are simply a guide to the assembler. </li></ul><ul><li>5. In Question 3, which one is an assembler directive? </li></ul>
  22. 23. A Simple PIC Application Block diagram
  23. 24. A Simple PIC Application cont… Circuit diagram
  24. 25. A Simple PIC Application cont… Flowchart
  25. 26. A Simple PIC Application cont… Assembly program
  26. 27. 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.lst
  27. 28. List File <ul><li>0000 0000 00001 allout EQU 00 ;Define Data Direction Code </li></ul><ul><li>0000 0005 00002 porta EQU 05 ;Port A data register </li></ul><ul><li>0000 0006 00003 portb EQU 06 ;Port B data register </li></ul><ul><li>00004 </li></ul><ul><li>0000 3000 00005 MOVLW allout ;Load W with Direction Code </li></ul><ul><li>0001 0066 00006 TRIS portb ;Send code to direction register </li></ul><ul><li>00007 </li></ul><ul><li>0002 0186 00008 reset CLRF portb ;Start output at 00000000 </li></ul><ul><li>0003 1C05 00009 start BTFSS porta ,0 ;Test R0 input button </li></ul><ul><li>0004 2802 00010 GOTO reset ;and reset Port B if pressed </li></ul><ul><li>0005 1885 00011 BTFSC porta ,1 ;Test R1 input button </li></ul><ul><li>0006 2803 00012 GOTO start ;and run count if pressed </li></ul><ul><li>0007 0A86 00013 INCF portb ;Increase output by 1 </li></ul><ul><li>0008 2803 00014 GOTO start ;Repeat main loop </li></ul><ul><li>00015 </li></ul><ul><li>00016 END ;Terminate Program </li></ul>
  28. 29. Flowchart
  29. 30. Review <ul><li>1. True or false. The extension for the source file is “asm”. </li></ul><ul><li>2. Which of the following files can be produced by the text editor? </li></ul><ul><li>(a) myprog.asm (b) myprog.o (c) myprog.hex </li></ul><ul><li>(d) myprog.lst (e) myprog.err </li></ul><ul><li>3. Which of the following files is produced by an assembler? </li></ul><ul><li>(a) myprog.asm (b) myprog.o (c) myprog.hex </li></ul><ul><li>(d) myprog.lst (e) myprog.err </li></ul>    
  30. 31. Review <ul><li>4. Which of the following files lists syntax errors? </li></ul><ul><li>(a) myprog.asm (b) myprog.o (c) myprog.hex </li></ul><ul><li>(d) myprog.lst (e) myprog.err </li></ul> 
  31. 32. Subroutine <ul><li>Subprogram that represents a set of instructions </li></ul><ul><li>begin with a label & end with the instruction return or retlw . </li></ul><ul><li>Executed when call subprogram_name is encountered in program. </li></ul><ul><li>Can be located anywhere in the program, regardless of the lines in which it is called </li></ul>
  32. 33. Subroutine cont… <ul><li>;MAIN program calling subroutines </li></ul><ul><li>ORG 0 </li></ul><ul><li>MAIN CALL SUBR_1 </li></ul><ul><li>CALL SUBR_2 </li></ul><ul><li>CALL SUBR_3 </li></ul><ul><li>HERE GOTO HERE ;stay here </li></ul><ul><li>;-------end of MAIN </li></ul><ul><li>; </li></ul><ul><li>SUBR_1 …… </li></ul><ul><li>…… </li></ul><ul><li>RETURN </li></ul><ul><li>;--------end of subroutine 1 </li></ul><ul><li>; </li></ul><ul><li>SUBR_2 …… </li></ul><ul><li>…… </li></ul><ul><li>RETURN </li></ul><ul><li>;--------end of subroutine 2 </li></ul><ul><li>; </li></ul><ul><li>SUBR_3 …… </li></ul><ul><li>…… </li></ul><ul><li>RETURN </li></ul><ul><li>;--------end of subroutine 3 </li></ul><ul><li>END ;end of the asm file </li></ul>
  33. 34. Example PortB = 8-bit output RA0, RA1 = input
  34. 35. Example
  35. 36. Example
  36. 37. Example <ul><li>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. </li></ul>
  37. 38. Example
  38. 39. 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
  39. 40. 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
  40. 41. Review <ul><li>1. How wide is the size of the stack in the PIC16? </li></ul><ul><li>2. With each CALL instruction, the stack pointer register, SP is ___________ (incremented, decremented). </li></ul><ul><li>3. With each RETURN instruction, the SP is ___________ (incremented, decremented). </li></ul>13-bit incremented decremented
  41. 42. Review <ul><li>4. On power-up, the PIC uses location ____ as the first location of the stack. </li></ul><ul><li>5. How deep is the size of the stack in the PIC16? </li></ul>1 8 levels
  42. 43. Macro <ul><li>A group of instruction performs a task that is used repeatedly </li></ul><ul><li>To reduce time to write code and possibility of errors </li></ul><ul><li>Its name is used as an instruction in any code </li></ul><ul><li>name MACRO dummy1, dummy2, …, dummyN </li></ul><ul><li>… </li></ul><ul><li>… </li></ul><ul><li>ENDM </li></ul>
  43. 44. Macro cont… <ul><li>MOVLF MACRO K, MYREG </li></ul><ul><li>MOVLW K </li></ul><ul><li>MOVWF MYREG </li></ul><ul><li>ENDM </li></ul><ul><li>1. MOVLF 0x55, 0x20 ;send value 55H to loc 20H </li></ul><ul><li>2. VAL_1 EQU 0x55 </li></ul><ul><li>RAM_LOC EQU 0x20 </li></ul><ul><li>MOVLF VAL_1, RAM_LOC </li></ul><ul><li>3. MOVLF 0x55, PORTB ;send value 55H to Port B </li></ul>
  44. 45. Local Directive <ul><li>To declare label or name in the body of macro </li></ul><ul><li>Rules to declare label in macro: </li></ul><ul><li>1. All labels in the label field must be declared LOCAL . </li></ul><ul><li>2. The LOCAL directive must be right after the MACRO directive. </li></ul><ul><li>3. The LOCAL directive can be used to declare all names and labels as follows: </li></ul><ul><li>LOCAL name1, name2, name3 </li></ul><ul><li>@ </li></ul><ul><li>LOCAL name1 </li></ul><ul><li>LOCAL name2 </li></ul><ul><li>LOCAL name3 </li></ul>
  45. 46. Local Directive cont… <ul><li>DELAY_1 MACRO V1, TREG </li></ul><ul><li>LOCAL BACK </li></ul><ul><li>MOVLW V1 </li></ul><ul><li>MOVWF TREG </li></ul><ul><li>BACK NOP </li></ul><ul><li>NOP </li></ul><ul><li>NOP </li></ul><ul><li>NOP </li></ul><ul><li>DECFSZ TREG, F </li></ul><ul><li>GOTO BACK </li></ul><ul><li>ENDM </li></ul>
  46. 47. Macro vs subroutine <ul><li>Macro: </li></ul><ul><li>Increase code size every time they are invoked </li></ul><ul><li>Subroutine: </li></ul><ul><li>Use stack space when it is called </li></ul><ul><li>Cause problem in nested calls </li></ul>
  47. 48. Execution Time
  48. 49. Execution Time cont… <ul><li>Find the size of the delay of the code snippet below if the crystal frequency is 4MHz: </li></ul><ul><li>MYREG EQU 0x08 ;use loc 08 as counter </li></ul><ul><li>DELAY MOVLM 0xFF </li></ul><ul><li>MOVWF MYREG </li></ul><ul><li>AGAIN NOP </li></ul><ul><li>NOP </li></ul><ul><li>DECFSZ MYREG, F </li></ul><ul><li>GOTO AGAIN </li></ul><ul><li>RETURN </li></ul>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
  49. 50. Program Data Table <ul><li>Allow access to elements of a frequently used table with minimum operations </li></ul><ul><li>Output predefined data bytes </li></ul><ul><li>Add an indexed pointer value to modify the program counter register </li></ul>
  50. 51. Program Data Table cont…
  51. 52. Program Data Table cont…
  52. 53. Program Data Table cont…
  53. 54. Exercise <ul><li>A switch is connected to pin RB3. Write a program to check the status of the switch and perform the following: </li></ul><ul><li>If switch = 0, send letter ‘N’ to port B. </li></ul><ul><li>If switch = 1, send letter ‘Y’ to port B. </li></ul>
  54. 55. Exercise