Timer interrupts help run tasks regularly without stopping the main program. They let your device do things on time, like a clock.
Timer interrupt for periodic tasks in Embedded C
void Timer_ISR(void) {
// Code to run when timer interrupt happens
}
// Setup timer with period and enable interrupt
void Timer_Init(void) {
// Configure timer registers
// Enable timer interrupt
// Start timer
}The function Timer_ISR is called automatically when the timer reaches the set time.
You must enable the timer interrupt and start the timer in your setup function.
void Timer_ISR(void) {
LED = !LED; // Toggle LED
}
void Timer_Init(void) {
Timer_Period = 1000; // 1 second
Enable_Timer_Interrupt();
Start_Timer();
}void Timer_ISR(void) {
sensor_value = Read_Sensor();
}
void Timer_Init(void) {
Timer_Period = 100; // 100 ms
Enable_Timer_Interrupt();
Start_Timer();
}This program simulates a timer interrupt that toggles an LED every 1 second. The main loop calls Timer_Tick to simulate time passing. Each time the timer reaches the period, the interrupt toggles the LED state.
#include <stdint.h> #include <stdbool.h> volatile bool led_state = false; // Simulated hardware registers volatile uint32_t TIMER_COUNT = 0; volatile uint32_t TIMER_PERIOD = 1000; // 1 second period volatile bool TIMER_INTERRUPT_FLAG = false; // Simulated LED output void Set_LED(bool state) { led_state = state; } // Timer Interrupt Service Routine void Timer_ISR(void) { // Clear interrupt flag TIMER_INTERRUPT_FLAG = false; // Toggle LED Set_LED(!led_state); } // Timer initialization void Timer_Init(void) { TIMER_COUNT = 0; TIMER_INTERRUPT_FLAG = false; // In real hardware, configure timer registers and enable interrupt here } // Simulate timer counting and interrupt triggering void Timer_Tick(void) { TIMER_COUNT++; if (TIMER_COUNT >= TIMER_PERIOD) { TIMER_COUNT = 0; TIMER_INTERRUPT_FLAG = true; Timer_ISR(); } } int main(void) { Timer_Init(); for (int i = 0; i < 5; i++) { Timer_Tick(); // Print LED state if (led_state) { printf("LED ON\n"); } else { printf("LED OFF\n"); } } return 0; }
Timer interrupts run code automatically without waiting.
Always clear the interrupt flag inside the ISR to avoid repeated calls.
Keep ISR code short to avoid slowing the main program.
Timer interrupts let you run code regularly without stopping other tasks.
Set up the timer period and enable interrupts to use them.
Write your periodic code inside the interrupt service routine.
Practice
What is the main purpose of a timer interrupt in embedded C programming?
Solution
Step 1: Understand timer interrupt functionality
Timer interrupts trigger code execution automatically at set time intervals without manual intervention.Step 2: Compare options with timer interrupt purpose
Only To run code automatically at regular time intervals describes automatic, regular code execution, which matches timer interrupt use.Final Answer:
To run code automatically at regular time intervals -> Option BQuick Check:
Timer interrupt = automatic periodic code run [OK]
- Thinking timer interrupts stop the main program
- Confusing manual function calls with interrupts
- Assuming timer interrupts speed up the processor
Which of the following is the correct way to enable a timer interrupt in embedded C?
void setup() {
// Enable timer interrupt here
}Solution
Step 1: Identify register for timer interrupt enable
TIMSK register controls timer interrupt enable bits; setting TOIE0 bit enables Timer0 overflow interrupt.Step 2: Analyze each option
TIMSK |= (1 << TOIE0); sets TOIE0 bit correctly. TIMSK = 0; clears TIMSK disabling interrupts. TCCR0 = 0; sets timer control register, not interrupt enable. PORTB |= (1 << PB0); sets port pin, unrelated to interrupts.Final Answer:
TIMSK |= (1 << TOIE0); -> Option AQuick Check:
Enable timer interrupt = set TIMSK TOIE0 bit [OK]
- Clearing TIMSK disables interrupts
- Confusing timer control register with interrupt enable
- Setting port pins instead of interrupt bits
What will be the output of the following code snippet if the timer interrupt triggers every 1 second and increments count?
volatile int count = 0;
ISR(TIMER0_OVF_vect) {
count++;
}
int main() {
setup_timer_interrupt();
while(count < 3) {}
printf("Count: %d", count);
return 0;
}Solution
Step 1: Understand interrupt increments
Each timer interrupt increments count by 1 every 1 second.Step 2: Analyze loop condition and output
Main loop waits until count reaches 3, then prints count. So output is "Count: 3".Final Answer:
Count: 3 -> Option DQuick Check:
count increments 3 times before print [OK]
- Assuming count stays 0 without interrupt
- Printing before count reaches 3
- Confusing count increments with time
Identify the error in the following code that prevents the timer interrupt from working properly:
volatile int flag = 0;
ISR(TIMER1_COMPA_vect) {
flag = 1;
}
int main() {
setup_timer1();
while(flag == 0) {
// wait
}
// do something
return 0;
}Solution
Step 1: Check ISR vector correctness
ISR vector TIMER1_COMPA_vect is correct for Timer1 Compare A interrupt.Step 2: Verify interrupt enabling in setup
If setup_timer1() does not enable the interrupt, ISR never triggers, so flag stays 0.Final Answer:
setup_timer1() function is missing enabling the interrupt -> Option CQuick Check:
Interrupt must be enabled in setup for ISR to run [OK]
- Assuming ISR vector name is wrong
- Removing volatile from flag
- Changing loop condition incorrectly
You want to toggle an LED every 500ms using a timer interrupt on an 8-bit microcontroller with a 1MHz clock. Which approach correctly sets up the timer interrupt for this periodic task?
Options:- Set timer prescaler to 64, preload timer to 131, enable overflow interrupt.
- Set timer prescaler to 256, preload timer to 0, enable compare match interrupt at 500.
- Set timer prescaler to 8, preload timer to 0, enable overflow interrupt.
- Set timer prescaler to 1024, preload timer to 0, enable overflow interrupt.
Solution
Step 1: Calculate timer counts for 500ms with 1MHz clock
Timer tick = prescaler / clock = 64 / 1,000,000 = 64us. Counts needed = 500ms / 64us = 7812.5 counts.Step 2: Use 8-bit timer overflow with preload
8-bit timer max count = 256. Number of overflows = 7812.5 / 256 ≈ 30.5. Preload = 256 - (7812.5 % 256) = 256 - 132.5 ≈ 123.5 (131 preload with prescaler 64 is closest practical setup).Step 3: Analyze options
Set timer prescaler to 64, preload timer to 131, enable overflow interrupt. uses prescaler 64 and preload 131, which is a common approach to adjust overflow timing. Other options either use wrong prescaler or compare match without correct value.Final Answer:
Set timer prescaler to 64, preload timer to 131, enable overflow interrupt. -> Option AQuick Check:
Prescaler 64 + preload adjusts overflow for 500ms toggle [OK]
- Ignoring timer resolution and prescaler effect
- Using wrong interrupt type for timing
- Not calculating preload for overflow timing
Ready to prove it?
Pass the test and this pattern moves unknown
