building assembler programs chapter five dr. gheith abandah1
TRANSCRIPT
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 Abandah 2
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 Abandah 3
Dr. Gheith Abandah 5
State Diagrams
Some machines tend to move from one state to another when a time period is completed or aspecific event occurs.
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 Abandah 6
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
nop ;1T
decfsz delcntr1,1 ;1T
goto del1 ;2T
return
Dr. Gheith Abandah 8
Using PCL Register for Lookup Table
....
movf sample_no,0
call table
movwf portb
....
table addwf pcl
retlw 23
retlw 3f
retlw 47
retlw 7f
retlw 0a2
retlw 1f
retlw 03
retlw 67
retlw 0c5
retlw 32
Dr. Gheith Abandah 10
W = 5
W = 1f
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 Abandah 11
Flashing LEDs Example – Page 2org 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 Abandah 12
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 Abandah 13
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 Abandah 14
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 Abandah 15
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 Abandah 16