Microcontroller Lecture # 7
Microcontroller Lecture # 7
Time Delay
Instruction Pipeline
Branch penalty
Week 7
Biomedical Engineering Department
College of Engineering
Imam Abdulrahman Bin Faisal University, Saudi Arabia 1
5-Arithmetic, Logic
3-Branch, Call and Time 4-PIC I/O Port
Instructions, and
Delay Loop Programming
Programs
2
PIC 18 TIME DELAY AND
INSTRUCTION PIPELINE
❑ In the last section we used the DELAY subroutine.
❑ Branch penalty
Delay calculation for the PIC18
❑ Two Factors can affect the accuracy of the delay
❑ Take 1/4 of the crystal frequency, then take its inverse, as shown
in Example 3-14 (see next slide)
Example 3-14
❑ The following shows the crystal frequency for three different
PIC-based systems. Find the period of the instruction cycle in
each case.
(a) 4 MHz (b) 16 MHz (c) 20 MHz
Solution:
(a) 4/4 = 1 MHz; instruction cycle is 1/1 MHz = 1µs
(microsecond)
❑In some circumstances, the CPU must flush out the queue when
??
❑The CPU starts to fetch codes from the new memory location
and the code in the queue that was fetched previously is
discarded
Count…..
❑In this case, the execution unit must wait until the fetch unit
fetches the new instruction. This is called a branch penalty.
❑ Notice that BNZ takes two instruction cycles if it jumps back, and takes only
one when falling through the loop. That means the above number should be
1277 µs.
Example 3-17
❑ Find the size of the delay in the following program if the crystal
frequency is 4 MHz
Example 3-17
Loop inside a loop delay
❑ Example 3-18 :For a instruction cycle of 1 µs, find the time delay in the following
subroutine
Execution cycle
1 µsec
1 µsec
1 × 200 = 200µsec
1 × 200 = 200µsec
1 × 250 =250 × 200 = 50000 µsec
1 × 250 = 250× 200 = 50000 µsec
1 × 250 = 250 × 200 = 50000 µsec
2 × 250 = 500× 200 = 100000 µsec
1 × 200 = 200 µ𝑠𝑒𝑐
2 × 200 = 400 µsec
1µsec
𝟐𝟓𝟏𝟎𝟎𝟑
Total delay 𝟐𝟓𝟏𝟎𝟎𝟑 µ 𝒔𝒆𝒄 = = 𝟐𝟓𝟏. 𝟎𝟎𝟑 𝒎𝒊𝒍𝒊𝒔𝒆𝒄𝒐𝒏𝒅𝒔
𝟏𝟎𝟎𝟎
Loop inside a loop delay
❑ Example 3-18 :For a instruction cycle of 1 µs, find the time delay in the following
subroutine
𝟐𝟓𝟎𝟖𝟎𝟐
Total delay 𝟐𝟓𝟏𝟎𝟎𝟑 µ 𝒔𝒆𝒄 = = 𝟐𝟓𝟎. 𝟖 𝒎𝒊𝒍𝒊𝒔𝒆𝒄𝒐𝒏𝒅𝒔
𝟏𝟎𝟎𝟎
Example 3-19
❑ Find the time delay for the following subroutine, assuming a
crystal frequency of 4 MHz. Discuss the disadvantage of this
over Example 3-18
Solution (Example 3-19)
❑ The time delay inside the AGAIN loop is [200(13 + 2)] x 1 µs
= 3000 µs.
❑ This means the loop delay takes 34 bytes of ROM code space,
and gives us only a 3000 µs delay.