Microcontroller - Md. Atiqur Rahman Ahad

Download Report

Transcript Microcontroller - Md. Atiqur Rahman Ahad

PIC18…
Ch. 3.1
Md. Atiqur Rahman Ahad
http://aa.binbd.com
3.1 Branch - Loop
• 2 ways to loop in PIC.
1. DECFSZ
 decrement fileReg,
 skip next instruction if 0
DECFSZ fileReg, d
- Place GOTO target
right below it to create a loop
AGAINX
ADDLW
3
COUNTX
GOTO
AGAINX
|
|
DECFSZ
Do
Exa. 3.1
Add 3 to WREG – ten times. Place the result in
SFR of PORTB.
COUNTX
AGAINX
EQU
MOVLW
MOVWF
MOVLW
ADDLW
DECFSZ
GOTO
MOVWF
0x25 ; Use location 25h for counter COUNTX
d’10’ ; WREG=10 [d=decimal] for counter
COUNTX ; count, COUNTX = 10
0
;clear WREG to 0. WREG = 0 now
3
;sum, WREG = 3, then added up…
COUNTX, F ;F, not W. If ‘W’, value to WREG
AGAINX ;repeat – until COUNTX becomes 0
PORTB ;send sum/result to PORTB SFR
• 2 ways to loop in PIC.
1. DECFSZ
2. BNZ
- branch if not zero
- from PIC18… PIC16 has no BNZ
COUNTX
EQU
0x25
MOVLW
MOVWF
MOVLW
; Use location 25h for counter COUNTX
d’10’
; WREG=10 [d=decimal] for counter
COUNTX ; count, COUNTX = 10
0
;clear WREG to 0. WREG = 0 now
AGAINX
ADDLW 3
;sum, WREG = 3, then added up…
DECFSZ COUNTX, F ;F, not W. If ‘W’, value to WREG
GOTO AGAINX ;repeat – until COUNTX becomes 0
AGAINX
ADDLW
DECF
3
;sum, WREG = 3, then added up…
COUNTX, F ;decrement counter
; decrement fileReg. Z=1 [zero flag] if fileReg = 0
BNZ
AGAINX
;branch/jump to AGAINX if Z=0
Q. Implement in Assembly lang.
- for(i=0; i<=10; i++)
- for(i=100; i>=0; i--)
Q. What is the max. number of times that the
loop in Exa. 3.1/3.2 can be repeated?
 Location COUNTX in fileReg is an 8-bit
register.
It can hold max. of FFh / 1111 1111b / 255
decimal
So, it can be repeated max. of 255 times. Why
not 256??
Q. How to repeat 255++ times?
Think about C prog.
for (…){
…
for (…){
…
}
}
Nested loop of 2/3/… times.
ENDLESS? Time cost or complexity [of ur algorithm] .
O(n2), O(nlogn), O(n3), …
Read other conditional jumps
– BC
– BNC
– BZ
– BNZ
– Etc.
- branch if C=1
… if no carry, / C=0
… if Z= 1
…
• All conditional jumps are short jumps
• The address of the target must be within 256
bytes of the contents of the program counter
(PC).
• Read – unconditional long jumps
– GOTO
– Branch
Function / subroutine
• CALL
- long call
• RECALL - relative call
– What is User-defined function? [think C prog]
– Why do we use User-defined function?
• Stack
– LIFO? FIFO?
• Pop / Push
Instructions …
…
CALL
UserDefinedFunction
;call of a subroutine
Instructions …
------------------------------------------------------------UserDefinedFunction
Instruction ;start of the subroutine
Instruction
…
RETURN ;finish of the subroutine
;return to caller