0
0
Embedded Cprogramming~5 mins

Timer interrupt for periodic tasks in Embedded C

Choose your learning style9 modes available
Introduction

Timer interrupts help run tasks regularly without stopping the main program. They let your device do things on time, like a clock.

Blink an LED every second without stopping other code
Read a sensor value every 100 milliseconds automatically
Send data over communication every fixed time
Update a display regularly without delay
Measure time intervals in a program
Syntax
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.

Examples
This example toggles an LED every second using the timer interrupt.
Embedded C
void Timer_ISR(void) {
    LED = !LED; // Toggle LED
}

void Timer_Init(void) {
    Timer_Period = 1000; // 1 second
    Enable_Timer_Interrupt();
    Start_Timer();
}
This example reads a sensor every 100 milliseconds automatically.
Embedded C
void Timer_ISR(void) {
    sensor_value = Read_Sensor();
}

void Timer_Init(void) {
    Timer_Period = 100; // 100 ms
    Enable_Timer_Interrupt();
    Start_Timer();
}
Sample Program

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.

Embedded C
#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;
}
OutputSuccess
Important Notes

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.

Summary

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.