Example (1):
Assume RISC processor uses a five-stages instruction and a program has 10 instructions. If every instruction requires
5 clock cycles to execute. Each clock cycle is 5 microseconds.
Solution:
a) What is the execution time when the program is executed without pipelining?
𝑇𝑛𝑜𝑛_𝑝𝑖𝑝𝑙𝑖𝑛𝑒 = 𝑁 ∗ 𝐶 ∗ 𝑇𝑐
Where:
• 𝑇𝑛𝑜𝑛_𝑝𝑖𝑝𝑙𝑖𝑛𝑒 = Total execution time (in microseconds)
• 𝑁: Total number of instructions in the program = 10
• 𝐶 = Number of clock cycles required per instruction = 5
• 𝑇𝑐 : Clock Cycle time = 5 microseconds
𝑇𝑛𝑜𝑛_𝑝𝑖𝑝𝑙𝑖𝑛𝑒 = 10 ∗ 5 ∗ 5 = 250 𝜇𝑠
b) What is the execution time when the program is executed with pipelining?
𝑇𝑝𝑖𝑝𝑙𝑖𝑛𝑒 = (𝑁 + 𝐾 − 1) ∗ 𝑇𝑐
Where:
• 𝑁: Total number of instructions in the program = 10
• 𝐾: Number of pipeline stages = 5
• 𝑇𝑐 : Clock Cycle time = 5 microseconds
𝑇𝑝𝑖𝑝𝑙𝑖𝑛𝑒 = (10 + 5 − 1) ∗ 5 = 70 𝜇𝑠
Example (2):
Given an 8-bit ADC converter with a reference voltage Vref = 2.56 volts, calculate the digital output (Do—D7) if the
analog input is 1.7, 2.1 volts.
Solution:
𝑉𝑟𝑒𝑓 2.56
𝑆𝑡𝑒𝑝 𝑆𝑖𝑧𝑒 = = = 10 𝑚𝑉
256 256
When 𝑉𝐼𝑁 = 1.7 𝑉:
𝑉𝐼𝑁 1.7 ∗ 1000
𝐷𝑂𝑈𝑇 = = = 170 𝑖𝑛 𝐷𝑒𝑐𝑖𝑚𝑎𝑙
𝑆𝑡𝑒𝑝 𝑆𝑖𝑧𝑒 10
Which gives us 10101010 in binary for D7-D0.
When 𝑉𝐼𝑁 = 2.1 𝑉:
𝑉𝐼𝑁 2.1 ∗ 1000
𝐷𝑂𝑈𝑇 = = = 210 𝑖𝑛 𝐷𝑒𝑐𝑖𝑚𝑎𝑙
𝑆𝑡𝑒𝑝 𝑆𝑖𝑧𝑒 10
Which gives us 11010010 in binary for D7-D0.
Example (3):
Given a 4-bit DAC converter with a reference voltage VREF = 5 volts, calculate the analog output if the digital input
is 1010 (decimal = 10):
Solution:
1. Using Binary Weighted Resistor:
Decimal Digital Input: 4-bit DAC
D3 D2 D1 D0
10 1 0 1 0
R 2R 4R 8R
To op-amp summing node
𝐷𝑛−1 𝐷𝑛−2 𝐷0
𝑉𝑜 = −𝑉𝑅𝐸𝐹 ∗ ( 1
+ 2 + ⋯ + 𝑛)
2 2 2
𝐷3 𝐷2 𝐷1 𝐷0
𝑉𝑜 = −𝑉𝑅𝐸𝐹 ∗ ( + + + )
2 4 8 16
1 0 1 0
𝑉𝑜 = −5 ∗ ( + + + )
2 4 8 16
1 1
𝑉𝑜 = −5 ∗ ( + ) = −3.125 𝑉
2 8
2. Using R/2R Ladder:
𝐷𝑛−1 𝐷𝑛−2 𝐷0
𝑉𝑜 = 𝑉𝑅𝐸𝐹 ∗ ( 1 + 2 + ⋯ + 𝑛 )
2 2 2
𝐷3 𝐷2 𝐷1 𝐷0
𝑉𝑜 = 𝑉𝑅𝐸𝐹 ∗ ( + + + )
2 4 8 16
1 0 1 0
𝑉𝑜 = 5 ∗ ( + + + )
2 4 8 16
1 1
𝑉𝑜 = 5 ∗ ( + ) = 3.125 𝑉
2 8
Example (4):
TIMERO BLOCK DIAGRAM (16-BIT MODE):
We want to use the system clock, T0CS = 0.
We have Frequency of Oscillator (FOSC = 64 MHz), therefore the basic frequency is,
FOSC 64
Timer Frequecny = = = 16 𝑀𝐻𝑧
4 4
1
𝑃𝑒𝑟𝑖𝑜𝑑 = = 0.0000000625 𝑠 𝑜𝑟 62.5 𝑛𝑠
16 ∗ 106
Let's use the prescaler and divide the frequency by 256,
PSA T0PS
Bit 3 Bit 2 Bit 1 Bit 0
PSA T0PS2 T0PS1 T0PS0
0 1 1 1
The timer increments using a period 𝑃 = 62.5 𝑛𝑠,
Timer Increcments = Period ∗ 256
Timer Increcments = 62.5 ∗ 10−3 ∗ 256 = 16𝜇s
The timer overflows after,
Overflows = Timer Increments ∗ 65536
Overflows = 16 ∗ 10−6 ∗ 65536 = 1.048576 s
Example (5):
Given that a time delay of 0.1 msec is to be generated and a 48 MHz crystal oscillator is connected with PIC.
Timer is related to the internal frequency which is always FOSC/4.
Clock source frequency (Crystal):
FOSC = 48 MHz
Therefore, Timer frequency:
FOSC 48
FTimer = = = 12 MHz
4 4
Prescaler is not used.
FTimer = 12 MHz
So, the time interval:
1
TTimer =
FTimer
1
TTimer = = 0.0833 𝜇𝑠
12000000
This means that when Timer runs, it will take 0.0833 𝜇𝑠 to increment its value at every count.
To calculate the value to be filled in Timer rolling over register to generate 0.1 msec delay:
(2𝑛 ) − 𝐶𝑜𝑢𝑛𝑡
Delay =
𝐹𝑇𝑖𝑚𝑒𝑟
(2𝑛 ) − 𝐶𝑜𝑢𝑛𝑡 = 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay
(2𝑛 ) − 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay = 𝐶𝑜𝑢𝑛𝑡
𝐶𝑜𝑢𝑛𝑡 = (2𝑛 ) − 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay
𝐶𝑜𝑢𝑛𝑡 = (65536) − (12000000 ∗ 0.1 ∗ 10−3 )
𝐶𝑜𝑢𝑛𝑡 = 65536 − 1200 = 64336 𝑖𝑛 𝑑𝑒𝑐𝑖𝑚𝑎𝑙
𝐶𝑜𝑢𝑛𝑡 = FB50 𝑖𝑛 𝐻𝑒𝑥
𝐿𝑜𝑎𝑑 𝑇𝑀𝑅0𝐻 = 𝐹𝐵, 𝑎𝑛𝑑 𝑇𝑀𝑅0𝐿 = 50
Question (6):
An external 100 kHz clock signal is applied to the T0CKI (RA4) pin of a PIC18F45K22 microcontroller, and the internal
frequency (FOSC ) is 48 MHz. if the Timer0 Control Register (T0CON) is configured with the binary value 10010000.
Calculate the required Timer0 initial value to generate a time delay of 0.1 msec.
Solution:
1) T0CON (Timer 0 Control Register):
Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0
1 0 0 1 0 0 0 0
T0CS = 0 Clock source from system clock divided by 4 (FOSC/4).
PSA = 0 Clock source is divided from the prescale and the divisor is selected by bits T0PS (3 bits)
T0PS2 T0PS1 T0PS0 Prescale Value
0 0 0 1:2
TMR0ON = 1 Enable the Timer0.
T08BIT = 0 Timer0 is configured as a 16-bit timer.
T0SE = 1 Increment on high-to-low transition on T0CKI pin (Ignored when T0CS=0).
Crystal Oscillator Frequency:
FOSC = 48 MHz
Therefore, Timer frequency:
FOSC 48
FTimer = = = 12 MHz
4 4
Prescaler is used.
12
FTimer = = 6 MHz
2
So, the time interval:
1 1
TTimer = = = 0.1667 𝜇𝑠
FTimer 6000000
This means that when Timer runs, it will take 0.1667 𝜇𝑠 to increment its value at every count.
To calculate the value to be filled in Timer rolling over register to generate 0.1 msec delay:
(2𝑛 ) − 𝐶𝑜𝑢𝑛𝑡
Delay =
𝐹𝑇𝑖𝑚𝑒𝑟
(2𝑛 ) − 𝐶𝑜𝑢𝑛𝑡 = 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay
(2𝑛 ) − 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay = 𝐶𝑜𝑢𝑛𝑡
𝐶𝑜𝑢𝑛𝑡 = (2𝑛 ) − 𝐹𝑇𝑖𝑚𝑒𝑟 ∗ Delay
𝐶𝑜𝑢𝑛𝑡 = (65536) − (6000000 ∗ 0.1 ∗ 10−3 )
𝐶𝑜𝑢𝑛𝑡 = 65536 − 600 = 64936 𝑖𝑛 𝑑𝑒𝑐𝑖𝑚𝑎𝑙
𝐶𝑜𝑢𝑛𝑡 = FDA8 𝑖𝑛 𝐻𝑒𝑥
𝐿𝑜𝑎𝑑 𝑇𝑀𝑅0𝐻 = 𝐹𝐷, 𝑎𝑛𝑑 𝑇𝑀𝑅0𝐿 = 𝐴8
Example (1):
Circuit in Proteus:
Code in MikroC:
Example (2):
Circuit in Proteus:
Code in MikroC:
Example (3):
Circuit in Proteus:
Code in MikroC:
Example (4):
Circuit in Proteus:
Code in MikroC:
Example (5):
Circuit in Proteus:
Code in MikroC: