Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Assembler Programs Chapter Five Dr. Gheith Abandah1.

Similar presentations


Presentation on theme: "Building Assembler Programs Chapter Five Dr. Gheith Abandah1."— Presentation transcript:

1 Building Assembler Programs Chapter Five Dr. Gheith Abandah1

2 Outline Building Structured Programs Conditional Branching and Working with Bits Subroutines Generating Time Delays and Intervals Indirect Addressing and PCL Using PCL Register for Lookup Table Flashing LEDs Example Dr. Gheith Abandah2

3 Building Structured Programs If you don’t watch it, assembly programs become spaghetti programs. It is better to develop structured programs; programs structured into routines with specific functions. Start by representing programs using: – Flow diagrams – State Diagrams Dr. Gheith Abandah3

4 4 Flow Diagrams Rectangle for process or action Diamond for decision

5 Dr. Gheith Abandah5 State Diagrams Some machines tend to move from one state to another when a time period is completed or a specific event occurs.

6 Conditional Branching and Working with Bits ; Testing and manipulating single bits movlw 00 ;clear all bits in port A and B movwf porta movwf portb loop bcf portb, 3 ;preclear port B, bit 3 btfss porta, 3 bsf portb, 3 ;but set it if button pressed ; bcf portb, 4 ;preclear port B, bit 4 btfss porta, 4 bsf portb, 4 ;but set it if button pressed goto loop end Dr. Gheith Abandah6

7 Subroutines Dr. Gheith Abandah7

8 Generating Time Delays and Intervals … call delay500 … ;500ms delay (approx) ;100 calls to delay5 delay500 movlw D'100' movwf delcntr2 del2 call delay5 decfsz delcntr2, 1 goto del2 return ;Delay of 5ms approx. ;Instr. Cycle T = 5us. ;200 iterations * 5T delay5 movlw D'200' movwf delcntr1 del1 nop ;1T decfsz delcntr1,1 ;1T goto del1 ;2T return Dr. Gheith Abandah8

9 Indirect Addressing and PCL Dr. Gheith Abandah9

10 Using PCL Register for Lookup Table.... movf sample_no,0 call table movwf portb.... tableaddwf pcl retlw 23 retlw 3f retlw 47 retlw 7f retlw 0a2 retlw 1f retlw 03 retlw 67 retlw 0c5 retlw 32 Dr. Gheith Abandah10 W = 5 W = 1f

11 Flashing LEDs Example – Page 1 ; ;specify SFRs pcl equ 02 status equ 03 porta equ 05 trisa equ 05 portb equ 06 trisb equ 06 ; pointer equ 10 delcntr1 equ 11 delcntr2 equ 12 ; Dr. Gheith Abandah11

12 Flashing LEDs Example – Page 2 org 00 ;Initialise start bsf status,5 ;select memory bank 1 movlw B'00011000' movwf trisa ;setup port A movlw 00 movwf trisb ;all port B bits output bcf status,5 ;select bank 0 ; Dr. Gheith Abandah12

13 Flashing LEDs Example – Page 3 ;The “main” program starts here movlw 00 ;clear all bits in port A movwf porta movwf pointer ;also clear pointer loop movf pointer,0 ;move pointer to W register call table movwf portb ;move W register to port B call delay incf pointer,1 btfsc pointer,3 ;test if pointer = 8 clrf pointer ;if yes, clear to start over goto loop Dr. Gheith Abandah13

14 Flashing LEDs Example – Page 4 ;Introduces delay of 500ms approx, for 800kHz clock delay movlw D'100' movwf delcntr2 outer movlw D'200' movwf delcntr1 inner nop nop decfsz delcntr1,1 goto inner decfsz delcntr2,1 goto outer return Dr. Gheith Abandah14

15 Flashing LEDs Example – Page 5 ;Lookup Table table addwf pcl retlw 23 retlw 3f retlw 47 retlw 7f retlw 0a2 retlw 1f retlw 03 retlw 67 ; end Dr. Gheith Abandah15

16 Summary It is important to devise good structures for programs as they are developed. Flow and state diagrams can help with this. A number of techniques assist in producing clear and well-structured programs. These include subroutines, look-up tables, macros and the use of Include files. The full 16F84A instruction set can be applied to build big and sophisticated programs. More complex programs require greater expertise in the use of simulation techniques. Dr. Gheith Abandah16


Download ppt "Building Assembler Programs Chapter Five Dr. Gheith Abandah1."

Similar presentations


Ads by Google