Microprocessor Mid-Term Notes (Complete)
Microprocessor Mid-Term Notes (Complete)
To calculate the necessary mer counts for a specific delay, the formula can be derived as:
For a delay of 187.5 ns, 3 me periods are needed, each las ng 62.5 ns:
62.5ns × 3 = 187.5ns
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐝𝐞𝐥𝐚𝐲
𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 𝐧𝐞𝐞𝐝𝐞𝐝 =
𝑪𝒍𝒐𝒄𝒌 𝑷𝒆𝒓𝒊𝒐𝒅
Problem1: Suppose you need a delay of 1 millisecond (ms) in a microcontroller mer
applica on. Given that the system clock frequency (F_CPU) is 16 MHz, calculate the mer
count needed to achieve this delay.
is given,
Required delay = 1ms,
f = 16MHz 𝑇 = = = 62.5𝑛𝑠
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐝𝐞𝐥𝐚𝐲
𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 𝐧𝐞𝐞𝐝𝐞𝐝 =
𝑪𝒍𝒐𝒄𝒌 𝑷𝒆𝒓𝒊𝒐𝒅(𝑻)
𝟏 1
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞𝒎𝒂𝒙 = 𝟐𝒃𝒊𝒕𝒔 × =2 × = 256 × 62.5𝑛𝑠 = 𝟏𝟔𝝁𝒔
𝒇 16𝑀𝐻𝑧
𝟏 1
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞𝒎𝒂𝒙 = 𝟐𝒃𝒊𝒕𝒔 × =2 × = 65536 × 62.5𝑛𝑠 = 𝟒. 𝟎𝟗𝟔𝒎𝒔
𝒇 16𝑀𝐻𝑧
So, it is not possible to get longer delays. For longer delays, coun ng directly with the system clock doesn’t work
well because the mer reaches its limit too fast. To fix this, we use pre-scalers (which slow down the clock)
Using Pre-scaler to Calculate Timer Count
To ensure safe operation and longer delays, a pre-scaler is used to slow down the timer
clock.
So, the counter needs to count approximately 14.6 cks (𝒓𝒐𝒖𝒏𝒅𝒆𝒅 𝒃𝒂𝒔𝒆𝒅 𝒐𝒏 𝒓𝒆𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏).
or simply:
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲 × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐅𝐫𝐞𝐪𝐮𝐞𝐧𝐜𝐲(𝒇)
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔)
Maximum Possible Delay for Timers
Maximum delay for Timer0 (8-bit) at 16 MHz (using pre-scaler 1024max):
We Know,
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞 = 𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭𝐬 × 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞𝒎𝒂𝒙 = 𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭𝐬𝒎𝒂𝒙 × 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝐓)
𝟏 1024
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞𝒎𝒂𝒙 = 𝟐𝒃𝒊𝒕𝒔 × =2 × == 256 × 64𝜇𝑠 = 𝟏𝟔. 𝟑𝟖𝟒𝒎𝒔
𝒇 16𝑀𝐻𝑧
𝒑𝒔
𝟏 𝑝𝑠
𝐃𝐞𝐥𝐚𝐲 𝐓𝐢𝐦𝐞𝒎𝒂𝒙 = 𝟐𝒃𝒊𝒕𝒔 × =2 × = 65536 × 64𝜇𝑠 = 𝟒. 𝟏𝟗𝟒𝒔
𝒇 16𝑀𝐻𝑧
𝒑𝒔
Note: For delays longer than 4 seconds, use Timer1 in combina on with nested if statements or
overflow coun ng to extend the delay range further.
Problem2: Make an LED blink every 2 milliseconds while using Arduino system frequency
(F_CPU) 16 MHz, using mer to generate the delay without any applica on of delay()
func on. Use a pre-scaler value of 1024
is given,
Required delay = 2ms,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
ps = 1024
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟐𝒎𝒔
= − 𝟏 = 𝟑𝟎. 𝟐𝟓 ≈ 𝟑𝟏
𝟏𝟎𝟐𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
#define PIN_USED 8
int milisec = 2;
int prescaler = 1024;
int clock_freq = 16000000 / prescaler;
float clock_period = 1.0 / (float)clock_freq;
int count = (milisec * 0.001 / clock_period) - 1;
void setup() {
pinMode(PIN_USED, OUTPUT);
TCCR0A = 0b00000000; WGM01 and WGM00 set to zero for normal mode
TCCR0B = 0b00000101; CS02, CS01, CS00 set to 1,0,1 for pre-scalar 1024
TCNT0 = 0; // Timer0
}
void loop() {
if (TCNT0 >= count) {
Problem3: Make an LED blink every 2 Seconds while using Arduino system frequency
(F_CPU) 16 MHz, using mer to generate the delay without any applica on of delay()
func on. Use a pre-scaler value of 1024
is given,
Required delay = 2s,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
ps = 1024
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟐𝒔
= − 𝟏 = 𝟑𝟎. 𝟐𝟓 ≈ 𝟑𝟏𝟐𝟓𝟎
𝟏𝟎𝟐𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
#define PIN_USED 8
void setup() {
pinMode(PIN_USED, OUTPUT);
TCCR1A = 0b00000000; WGM01 and WGM00 set to zero for normal mode
TCCR1B = 0b00000101; CS02, CS01, CS00 set to 1,0,1 for pre-scalar 1024
TCNT1 = 0; // Timer1
}
void loop() {
if (TCNT1 >= count) {
Problem4: You're working on an Arduino project running at 16 MHz. You want to create
a delay of 1 millisecond using Timer0 with a prescaler of 1024, without using the delay()
func on.
is given,
Required delay = 1ms,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
ps = 1024
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟏𝒎𝒔
= − 𝟏 = 𝟏𝟒. 𝟔𝟐𝟓 ≈ 𝟏𝟓
𝟏𝟎𝟐𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
Declaring the mer func on to count 1 ms of delay
while (1) {
if (TCNT0 >= 16) { // Checking if 1 millisecond has passed (for 16MHz/1024 setup)
TCNT0 = 0;
count++;
}
return 0;
}
Problem5: If you want to make an LED of blue color blink every 2 seconds while using
an Arduino system frequency of 16 MHz using a mer to generate delay without any
applica on of the built-in delay() func on, select a suitable mer of the Arduino Uno for
your applica on. Use a pre-scaler value of 256. Determine the necessary register set-up
required for the program.
is given,
Required delay = 2s,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
𝑝𝑠 = 256
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟐𝒔
= − 𝟏 = 𝟏𝟐𝟒𝟗𝟗𝟗
𝟐𝟓𝟔 × 𝟔𝟐. 𝟓𝒏𝒔
❌ Can we use Timer0 or Timer1?
✅ Solution:
Use a subroutine (timer function) that gives 1 ms delay and call it 2000 times to make
2 seconds.
Use Timer0 in Normal Mode (simple up-counting, overflow detection).
#define YELLOW_PIN 10
int green_on = 5000; // delay is given in ms void loop() {
int green_off = 4000; // delay is given in ms pinMode(GREEN_PIN, INPUT);
TCCR0A = 0b00000000;
int delay_timer (int milliseconds){ TCCR0B = 0b00000100;
int count = 0; TCNT1 = 0x0000;
while(1) { }
if(TCNT1 >= _____) {
TCNT1=0;
count++;
void loop() {
if (count == milliseconds) {
digitalWrite(BLUE_PIN, HIGH);
count=0;
delay(green_on);
break; }
}
digitalRead(GREEN_PIN, LOW);
}
delay_timer(blue_off);
return 0;
}
}
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟑𝒎𝒔
= − 𝟏 = 𝟒𝟓. 𝟖𝟕𝟓 ≈ 𝟒𝟔
𝟏𝟎𝟐𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
So, The output of the program is:
Make a green LED on pin 5 blink in the following pattern:
ON for 5 seconds
OFF for 4 seconds
Repeat forever (until power off)
The number of counts required for a 5 s (5000 ms) delay in each cycle for the LED ON me = = 1667.
The number of counts required for a 4 s (4000 ms) delay in each cycle for the LED OFF me = = 1334.
So, we use Timer0 or Timer1?
Timer0 → 8-bit → max 256
Timer1 → 16-bit → max 65536
Timer1 is suitable since (1667|1334) < 65536
is given,
Required delay = 4s,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
𝑝𝑠 = 1024
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟒𝒔
= − 𝟏 = 𝟔𝟐𝟒𝟗𝟗
𝟏𝟎𝟐𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
Name Value
TCCR1A 0b00000000
TCCR1B 0b00000101
void setup() {
pinMode(PIN_USED, OUTPUT);
TCCR1A = 0b00000000; WGM01 and WGM00 set to zero for normal mode
TCCR1B = 0b00000101; CS02, CS01, CS00 set to 1,0,1 for pre-scalar 1024
TCNT1 = 0; // Timer1
}
Problem8: A simple IC test kit using the available ATMega328P MCU test kit will generate
2 pulse waves with a period of 400 ns and 400 ms to test the IC chips. The student decided
to generate these pulses using the mer modules in the ATMega328P. The MCU is running
at 16 MHz and there are 3 mer modules (Timer0, Timer1, and Timer2) available of which
Timer0 and Timer2 are 8-bit mers and Timer1 is a 16-bit mer. The available pre-scalers
are 1, 8, 64, 256, and 1024. Select appropriate mers to be used for this test kit. Compute
the necessary mer counts to generate the stated pulse wave periods using the selected
mers.
is given,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
𝐴𝑣𝑎𝑖𝑙𝑎𝑏𝑙𝑒 𝑝𝑠 = 1, 8, 64, 256, 1024
For 400 ns Period Pulse (very fast),
Required delay = 400 ns,
Let, 𝑝𝑠 = 1(𝑛𝑜 𝑝𝑟𝑒𝑠𝑐𝑎𝑙𝑖𝑛𝑔)
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟒𝟎𝟎𝒏𝒔
= − 𝟏 = 𝟓. 𝟒 ≈ 𝟔
𝟏 × 𝟔𝟐. 𝟓𝒏𝒔
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔) × 𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
𝟒𝟎𝟎𝒎𝒔
= − 𝟏 = 𝟐𝟒𝟗𝟗𝟗
𝟐𝟓𝟔 × 𝟔𝟐. 𝟓𝒏𝒔
Table 1 Clock select bits and corresponding prescaler values for Timers.
CSx2 CSx1 CSx0 Prescaler
0 0 1 1
0 1 0 8
0 1 1 64
1 0 0 256
1 0 1 1024
void setup_timer0_normal_mode() {
pinMode(5, OUTPUT);
// Load timer count for 400ns (i.e., count = 6 → load TCNT0 = 256 - 6 = 250)
TCNT0 = 250;
}
Problem10: Prepare the setup func on for the pins from which the two pulses of
Problem8 are available Using the following template. Hint: Timer should be run in normal
mode.
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
TCCR......= 0b................; // {COMnA1}{COMnA0}{COMnB1}{COMnB0}-1-{WGMn1}{WGMn0}
TCCR......= 0b................; // {FOCnA}{FOCnB}-1-{WGMn2}{CSn2}{CSn1}{CSn0}
TCCR......= 0b................; // {COMnA1}{COMnA0}{COMnB1}{COMnB0}-1-{WGMn1}{WGMn0}
TCCR......= 0b................; // {FOCnA}{FOCnB}-1-{WGMn2}{CSn2}{CSn1}{CSn0}
}
Table 1 Clock select bits and corresponding prescaler values for Timers.
CSx2 CSx1 CSx0 Prescaler
0 0 1 1
0 1 0 8
0 1 1 64
1 0 0 256
1 0 1 1024
Since Setup Func on for 2 Pulses of Problem8 (Timer0, Timer1 in Normal Mode)
void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
ISR(TIMER1_COMPA_vect) {
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Invert LED state
}
void loop() {
// Main loop is empty, blinking is handled by the ISR
}
AVR microcontrollers have mul ple mers (Timer0, Timer1, Timer2), and each mer can
generate two types of interrupts: Compare Match A and Compare Match B.
o TIMER1_COMPA_vect: This vector is used for Timer1 Compare Match A interrupt.
o TIMER1_COMPB_vect: This vector is used for Timer1 Compare Match B interrupt.
o TIMER2_COMPA_vect: This vector is used for Timer2 Compare Match A interrupt.
o TIMER2_COMPB_vect: This vector is used for Timer2 Compare Match B interrupt.
o TIMER0_COMPA_vect: This vector is used for Timer0 Compare Match A interrupt.
o TIMER0_COMPB_vect: This vector is used for Timer0 Compare Match B interrupt.
We use these vectors to trigger different ac ons depending on which mer’s compare match
has occurred.
Problem1: A space research facility is planning to shoot high-frequency pulses into space for
research. These pulses are to be shot at a 100 ns interval. The facility has an ATMega328P
MCU available and decided to use it to count the me. Every 100 ns a mer interrupt will be
triggered by the MCU to shoot the pulse. It has been decided that the Timer2 interrupt will
be used. If the MCU is running at 16 MHz, prepare a program that triggers the Timer2
interrupt pin every 100 ns. The available pre-scalers for Timer2 are 1, 8, 64, 256, and 1024.
[Hint: Bits 0 to 2 of the TCCR2B register must be set. These bits correspond to the pre- scaler
to be used. The pre-scaler selec on bits are shown in Table 1. Output Compare Register,
OCR2B holds the necessary Timer2 count to achieve the required delay. Bit 1 from TIMSK2 is
OCIE2B - this bit must be set to enable a comparison match with the OCR2B register. Any bits
not men oned here are to be considered as 0].
Table 1 Clock select bits and corresponding pre-scaler values for Timers.
CSx2 CSx1 CSx0 Pre-scaler
0 0 1 1
0 1 0 8
0 1 1 64
1 0 0 256
1 0 1 1024
is given,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
𝐴𝑣𝑎𝑖𝑙𝑎𝑏𝑙𝑒 𝑝𝑠 = 1, 8, 64, 256, 1024
Required interrupt interval = 100 ns
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐝𝐞𝐥𝐚𝐲
𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 𝐧𝐞𝐞𝐝𝐞𝐝 =
𝑪𝒍𝒐𝒄𝒌 𝑷𝒆𝒓𝒊𝒐𝒅 (𝑻)
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
2. 𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 (𝐓𝐂/𝐎𝐂𝐑𝟐𝐁) = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔)×𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
o For 𝑃𝑆 = 1:
𝟏𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = 𝒓𝒐𝒖𝒏𝒅(𝟎. 𝟔) = 𝟏
𝟏 × 𝟔𝟐. 𝟓𝒏𝒔
o For 𝑃𝑆 = 8:
𝟏𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = −𝟎. 𝟗𝟗𝟖 = 𝟎 [Since Nega ve value? → Not possible!]
𝟖 × 𝟔𝟐. 𝟓𝒏𝒔 Assume it 0
𝐀𝐃 = (𝟎 + 𝟏) × (𝟖 × 𝟔𝟐. 𝟓 𝒏𝒔) = 𝟓𝟎𝟎 𝒏𝒔
o For 𝑃𝑆 = 64:
𝟏𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = −𝟏 = 𝟎
𝟔𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
So, Higher prescalers only make the interval longer. Therefore, prescaler = 1 gives
us the closest possible interval (125 ns).
Step 4: Observa on:
No prescaler gives 𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 exactly 100 ns.
Solu on: Use the smallest possible prescaler (1) and accept a slight error.
C Program:
#include <avr/io.h>
void timer2_init() {
ISR(TIMER2_COMPB_vect) {
void isr_f() {
var_b = false;
digitalWrite(3, HIGH);}
void setup() {
attachInterrupt(digitalPinToInterrupt(5), isr_f, CHANGE);
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
}
void loop() {
if (var_b) {
// interrupt has occurred
}
Table 1 Clock select bits and corresponding pre-scaler values for Timers.
CSx2 CSx1 CSx0 Pre-scaler
0 0 1 1
0 1 0 8
0 1 1 64
1 0 0 256
1 0 1 1024
is given,
f = 16MHz, 𝑇 = = = 62.5𝑛𝑠
𝐴𝑣𝑎𝑖𝑙𝑎𝑏𝑙𝑒 𝑝𝑠 = 1, 8, 64, 256, 1024
Required interrupt interval = 100 ns
We Know,
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐝𝐞𝐥𝐚𝐲
𝐍𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 𝐧𝐞𝐞𝐝𝐞𝐝 =
𝑪𝒍𝒐𝒄𝒌 𝑷𝒆𝒓𝒊𝒐𝒅 (𝑻)
200 ns
Number of periods needed = = 3.2 = 3 𝑜𝑟 4 [Since we can't have
62.5 𝑛𝑠 a frac on of a clock]
Step 2: Determine Actual Achievable Delay: cycle,
If , 𝐍𝐨 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 = 𝟑
We Know,
𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 = 𝐍𝐨 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 × 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝 (𝐓)
𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 = 𝟑 × 𝟔𝟐. 𝟓𝐧𝐬 = 𝟏𝟖𝟕. 𝟓𝐧𝐬 (6.25% error) [This is the minimum possible
interrupt interval achievable
without prescaling]
If , 𝐍𝐨 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 = 𝟒
We Know,
𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 = 𝐍𝐨 𝐨𝐟 𝐩𝐞𝐫𝐢𝐨𝐝𝐬 × 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝 (𝐓)
𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 = 𝟒 × 𝟔𝟐. 𝟓𝐧𝐬 = 𝟐𝟓𝟎𝐧𝐬 (20% error)
𝐑𝐞𝐪𝐮𝐢𝐫𝐞𝐝 𝐃𝐞𝐥𝐚𝐲
5. 𝐓𝐢𝐦𝐞𝐫 𝐂𝐨𝐮𝐧𝐭 (𝐓𝐂/𝐎𝐂𝐑𝟐𝐁) = −𝟏
𝐏𝐫𝐞𝐬𝐜𝐚𝐥𝐞𝐫(𝒑𝒔)×𝐒𝐲𝐬𝐭𝐞𝐦 𝐂𝐥𝐨𝐜𝐤 𝐏𝐞𝐫𝐢𝐨𝐝(𝑻)
o For 𝑃𝑆 = 1:
𝟐𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = 𝒓𝒐𝒖𝒏𝒅(𝟐. 𝟐) = 𝟐
𝟏 × 𝟔𝟐. 𝟓𝒏𝒔
o For 𝑃𝑆 = 8:
𝟐𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = −𝟎. 𝟔 = 𝟎 [Since Nega ve value? → Not possible!]
𝟖 × 𝟔𝟐. 𝟓𝒏𝒔 Assume it 0
𝐀𝐃 = (𝟎 + 𝟏) × (𝟖 × 𝟔𝟐. 𝟓 𝒏𝒔) = 𝟓𝟎𝟎 𝒏𝒔
o For 𝑃𝑆 = 64:
𝟐𝟎𝟎𝒏𝒔
𝐓𝐂 = − 𝟏 = −𝟎. 𝟗𝟓 = 𝟎
𝟔𝟒 × 𝟔𝟐. 𝟓𝒏𝒔
So, Higher prescalers only make the interval longer. Therefore, prescaler = 1 gives
us the closest possible interval (125 ns).
Step 4: Observa on:
No prescaler gives 𝐀𝐜𝐭𝐮𝐚𝐥 𝐃𝐞𝐥𝐚𝐲 exactly 200 ns.
Solu on: Use the smallest possible prescaler (1) and accept a slight error.
C Program:
#include <avr/io.h>
#include <avr/interrupt.h>
void timer2_init() {
ISR(TIMER2_COMPB_vect) {
Oscillator Selection:
o Based on performance, cost, and complexity.
o Check the microcontroller datasheet for supported options.
Types of Oscillators:
o Internal:
R-C phase shift oscillator
Phase-Locked Loop (PLL)
o External:
Crystal oscillator
Ceramic resonator
CMOS clock
R-C oscillator
Capacitor-only oscillator
Selection Considerations:
o Balance between accuracy, cost, and simplicity.
Timer/Counter Oscillator
Crystal Sharing:
o Same crystal used for low-frequency and Timer/Counter oscillator.
Pin Sharing:
o TOSC1 ↔ XTAL1, TOSC2 ↔ XTAL2.
o Timer/Counter oscillator shares pins with system oscillator.
Usage Condition:
o Timer/Counter oscillator only usable with internal RC oscillator as system
clock.
Clock Frequency Rule:
o System clock must be 4× the oscillator frequency for Timer/Counter usage.
External Clock Input:
o Can apply external clock to TOSC1.
o Set EXTCLK = 1 in ASSR register to use external clock instead of 32.768 kHz
crystal.
System clock Pre-scalar
Prescaler Available:
o System clock can be divided using CLKPR (Clock Prescale Register).
Purpose:
o Reduce clock frequency and power consumption when low processing power is
needed.
Compatibility:
o Works with all clock sources.
Effect:
o Affects CPU and all synchronous peripherals.
Flexibility:
o Multiple sleep modes available.
Purpose:
o Controls power management and sleep mode behavior.
Key Bits:
o SM2:SM0 → Select the sleep mode (functions defined elsewhere).
o Other bits manage sleep enable/disable features.
PRR – Power Reduction Register
The PRR (Power Reduction Register) allows selective disabling of specific MCU modules to
reduce power consumption. Disabling a module stops its clock, significantly lowering
current draw.
Bit 4 – Reserved
o Do not modify this bit.
o Write it as 0 to avoid undefined behavior.
We need to:
Set the Sleep Mode Control Register (SMCR) to Power-save mode.
Enable Power Reduction Mode (PRR) for Timer0, Timer1, and SPI.
Total: 32 registers
Size: Each register is 8-bit
Purpose: Used for fast data access and manipulation without needing to access RAM.
Can be used individually (8-bit) or in pairs (16-bit) for certain operations.
4. Instruc on Decoder
Translates the binary instruction codes fetched from memory into control signals.
Directs other components (ALU, registers, etc.) on what operations to perform.
5. Status Register (SREG)
Contains flag bits that reflect the outcome of operations in the ALU.
Common flags:
o C: Carry
o Z: Zero
o N: Negative
o V: Overflow
o S: Sign
o H: Half Carry
o T: Transfer Bit
o I: Global Interrupt Enable
Flags Clock
Mnemonics Operands Description Operation
Affected Cycles
Clear bit b of
CBI A, b Clear Bit in I/O Register None 1
register A
Problem2: For the processor ATmega328P, show the State contents of RAM loca ons of
$212, $213, $214, $215, and $216 a er execu ng the following program.
LDI R16, 0x99
STS 0x212, R16
LDI R16, 0x85
STS 0x213, R16
LDI R16, 0x3F
STS 0x214, R16
LDI R16, 0x63
STS 0x215, R16
LDI R16, 0x12
STS 0x216, R16
Let’s execute the program line by line:
Problem3: For the processor ATmega328P, show the contents of registers and the 8-bit data
memory loca ons from OxFA41 to OxFA44 a er the following program is executed Assume,
C = 1.
LDI R18, 0x98;
LDI R29, Ox65:
SUB R29, R18;
STS OxFA41, R29;
ADDC R18, R29;
STS OxFA42, R18;
LDI R16, 0b11101101;
LDI R20, 0b11011011;
AND R20, R16;
XOR R29, R20;
STS OxFA43, R20;
STS OxFA44, R29;
Let’s execute the program line by line:
ldi
Load Immediate: Load a value directly into a register. Example: ldi r16,
0x55 loads 0x55 into register r16.
out
Output to I/O Register: Send data from a register to a hardware register
(like EECR, EEAR, etc.).
in
Input from I/O Register: Read data from a hardware register into a
general register.
sbi
Set Bit in I/O Register: Sets a single bit (makes it 1) in an I/O register.
Example: sbi EECR, EEPE sets the EEPE bit in the EECR register.
sbic
Skip if Bit in I/O Register is Cleared: If the bit is 0, the next instruction is
skipped.
cli
Clear Interrupts: Disables global interrupts (like turning off interrupt
handling temporarily).
sei Set Interrupts: Enables global interrupts.
ret
Return from Subroutine: Jumps back to the place where the subroutine
was called.
rjmp
Relative Jump: Jumps to a different instruction location (used for loops,
etc.).
Function Assembly C
Assembly C
Assembly C
Assembly C
Assembly C
Assembly C
Problem1: Prepare an Assembly program to set the Sleep Mode Control Register (SMCR) so
that the system remains in the power-save mode and power reduc on mode (PRR) for its
Timer0, Timer1, and SPI. Use the following table for the SMx bits of the SMCR register. The
PRR register bits are shown, too.
We need to:
Set the Sleep Mode Control Register (SMCR) to Power-save mode.
Enable Power Reduction Mode (PRR) for Timer0, Timer1, and SPI.
Debouncing is the process of fixing this issue, so one press is counted as only one signal, not
many.
In Arduino or digital circuits, debouncing removes these false signals to ensure accurate button
or switch input.
This bouncing causes multiple signals instead of one. It can mess up digital circuits like
counters.
It happens in switches like SPST and SPDT, and can also be affected by electrical noise
like:
Crosstalk
EMI (Electromagnetic Interference)
RFI (Radio Frequency Interference)
ESD (Electrostatic Discharge)
o A Schmitt trigger has two thresholds (high and low), so it ignores small
bounces.
o This behavior is called hysteresis, meaning it remembers the last state and
changes only when the input clearly crosses set levels.
o Add a Schmitt trigger buffer after the RC network to clean the signal before it
reaches the microcontroller.
3. Inverting Schmitt triggers are often used because they are faster than non-inverting
ones.
Example:
If a TV remote button isn't debounced, pressing once could change the channel multiple times.
Types of Debouncing:
1. Hardware Debouncing:
o Use physical components (resistors, capacitors, Schmitt triggers) to remove bounces.
2. Software Debouncing:
o Use code to ignore rapid changes in the button state for a short time.
3. Digital Debouncing:
o Like software, done using digital logic or microcontrollers.
4. VHDL Debouncing:
o Debouncing done in hardware description language (used for FPGAs or digital
design).
What is Hardware Debouncing?
A method using electronic components (like resistors, capacitors, and Schmitt triggers)
to clean up the noisy signal caused by bouncing in switches.
It ensures only one clean pulse is sent when a switch is pressed or released.
Goal: Eliminate switch bounce using hardware (resistor, capacitor, and Schmitt trigger).
How it Works:
o When the switch is open, the capacitor charges through R, pulling the
output HIGH.
o When the switch is closed, the capacitor discharges, pulling the output LOW.
o The RC time constant (𝝉 = 𝑹 × 𝑪) must be longer than the bounce
time (typically 10–20 ms).
Problem:
The capacitor charges/discharges slowly, causing intermediate voltages that can confuse
digital logic.
Adding a Schmitt Trigger (e.g: 74AHCT14)
Why?
o The Schmitt trigger has hysteresis, ensuring a clean transition between HIGH and
LOW.
o It inverts the signal (use two in series if non-inverted output is needed).
Improved Circuit:
How it Works:
Switch Open:
Capacitor charges through R1 + diode (D1) → Output = LOW (inverted).
Switch Closed:
Capacitor discharges through R2 → Output = HIGH (inverted).
t
R =−
V
C × ln( )
V
20 × 10
R =− ≈ 180 𝒌𝜴
1.7
0.1µF × ln( )
5
o Problem: Leakage current may cause false triggering.
≫The input leakage current (1μA) of the 74AHCT14 causes a small voltage drop across
R2:
V=I ×R
t
R +R =−
V
C × ln(1 − )
V
ΔV = (Vth− − Vth+ )
ΔV = 1.7 − 0.9 = 0.8 V
Step 3.1: Calculate R1 (Charging Capacitor)(Assume that there is a diode)
Some mes, the math gives R1 + R2 < R2, which doesn't make sense. In that case: we use diode
o When the switch is open, if we use diode then the capacitor charges through R1 + diode.
o Formula (with diode drop 0.7V → Vfinal = 4.3V):
t
R =−
V
C × ln(1 − )
V
Is Given:
≫Supply Voltage (Vcc) = 5 V
≫Schmi Trigger Thresholds:
Posi ve-going (Vth₊) = 0.8 V
Nega ve-going (Vth₋) = 2.2 V
≫Input Leakage Current (Ileak) = 15 µA
≫Debounce Time (t) = 5 ms & duty cycle is 50% = 10 ms to ensure stability.
≫Diode Forward Voltage (VD) = 0.7 V (Si diode)
≫ best-case switching threshold = 0.4V
t
R =−
V
C × ln( )
V
10 × 10
R =− ≈ 120 𝒌𝜴
2.2
0.1µF × ln
5
o Problem: Leakage current may cause false triggering.
≫The input leakage current (15μA) of the 74AHCT14 causes a small voltage drop across
R2:
V𝒍𝒆𝒂𝒌 = I ×R
o Solution:
10 × 10
R =− ≈ 12 𝒌𝜴
2.2
1µF × ln
5
t
R =−
V
C × ln(1 − )
V
o For Vfinal = 4.3V, Vth+ = 0.8V, C = 1 µF:
10 × 10
R =− ≈ 48 𝒌𝜴
0.8
1µF × ln(1 − )
4.3
Step 4: Hysteresis Voltage:
o Hysteresis (ΔV) is the difference between the two threshold voltages:
ΔV = (Vth− − Vth+ )
ΔV = 2.2 − 0.8 = 1.4 V
Final Component Values:
Capacitor (C): 1 µF
Resistor R₂ (discharge): 12 kΩ
Resistor R₁ (charge): 48 kΩ
Diode (D1): Si diode
Hysteresis Voltage (VH): 2.2 V - 0.8 V = 1.4 V
Is Given:
≫Vcc = 5 V
≫Schmi Trigger Thresholds:
Vth₊ = 3.2 V
Vth₋ = 1.8 V
≫ Ileak = 1.5 µA
≫ t = 10 ms
≫ VD = 0.65 V (Si diode)
t
R =−
V
C × ln( )
V
10 × 10
R =− = 97.88 ≈ 100 𝒌𝜴
1.8
0.1µF × ln
5
o Problem: Leakage current may cause false triggering.
≫The input leakage current (1.5μA) of the 74AHCT14 causes a small voltage drop across
R2:
V𝒍𝒆𝒂𝒌 = I ×R
t
R =−
V
C × ln(1 − )
V
ΔV = (Vth+ − Vth− )
ΔV = 3.2 − 1.8 = 1.4 V
Step 5: Best LOW Voltage Condi on:
o The best LOW voltage condition ensures that the capacitor fully discharges
below Vth_low = 1.8 V during bouncing.
o For R2 = 100 𝐤𝛀, & C = 0.1µF the discharge time constant is:
𝜏 = R2 × 𝐶
V , =I ×R
Is Given:
≫Vcc = 5 V
≫Schmi Trigger Thresholds:
Vth₊ = 3.1 V
Vth₋ = 1.9 V
≫ Ileak = 2 µA
≫ t = 20 ms
≫ VD = 0.55 V (Si diode)
t
R =−
V
C × ln( )
V
20 × 10
R =− = 206.70 ≈ 205 𝒌𝜴
1.9
0.1µF × ln
5
Step 3: Recheck: Input Leakage Voltage Bias
V𝒍𝒆𝒂𝒌 = I ×R
V𝒍𝒆𝒂𝒌 = 2𝛍𝐀 × 205 𝒌𝜴 = 0.41𝑽
≫ VLeak(0.41V) <Vth- (1.9V)→ (𝑎𝑐𝑐𝑒𝑝𝑡𝑎𝑏𝑙𝑒)
t
R =−
V
C × ln(1 − )
V
ΔV = (Vth+ − Vth− )
ΔV = 3.1 − 1.9 = 1.2 V
Step 5: Best LOW Voltage Condi on:
V , =I ×R
V , = 2 𝛍𝐀 × 205 𝒌𝜴 = 0.41 𝐕