This preview shows page 1. Sign up to view the full content.
Unformatted text preview: decfsz instruction will execute for 256 times.
DELAY_L will become 0 after the 256th decrement. The
bra line will be skipped.
DELAY_H will be decremented to 1. Not zero. Branch
back to the start.
The first decfsz instruction will execute for 256 times.
DELAY_H will be decremented to 0. End program.
In total, the first decfsz instruction executes 2x256
times. Using Program Loops to Create Time Delays
• Suppose DELAY_L = 0 and DELAY_H = 2.
DelayLoop: decfsz DELAY_L
bra DelayLoop 1 instruction cycle (2 if skip)
2 instruction cycles
1 instruction cycle (2 if skip)
2 instruction cycles • The first two lines will run for 256 times, lasting
255*3+2 = 767 instruction cycles during each loop.
• The last two lines will run for 2 times, lasting 3+2 = 5
• There are 2 loops (set by DELAY_H).
• The whole delay loop will run for 767*2+5 = 1539
• If clock frequency = 4MHz (instruction freq. = 1MHz),
the whole loop lasts 1.539 msec.
the 4. Subroutine and Stack
4. 38 Subroutine
• A subroutine is a sequence • e.g., Lab 1
of instructions that can be
called from different places
in a program.
• Two reasons for creating
– The problem is too big: Easier
to divide the problem into
– There are several places in a
program that need to perform
the same operation.
• Subroutine is similar to branching. The
difference is that PIC needs to return to
the main program where the subroutine
View Full Document
- Fall '13