Internal Timers and Interrupts in Micro Controllers

What is an internal timer of a microcontroller?

An internal timer of a microcontroller is a built-in hardware component that is used to measure time intervals and trigger events at specific times. It consists of a counter that increments at a certain frequency, typically based on the system clock, and can be configured to generate interrupts or trigger other actions when a specific time or count value is reached.

Timers can be used for,

i. Controlling the timing of program execution

ii. Measuring the duration of external events

iii. Generating periodic signals for use in communication

Internal timers are a common feature of most microcontrollers and are an important tool for controlling the timing and behavior of embedded systems.

Advantages of using internal timers over delay function

• Precise Timing - Internal timers are hardware components that can generate precise timing signals, whereas delay functions rely on software loops that may not provide accurate timing due to variations in the execution time of the loop.

• Power consumption - Using internal timers can be more power-efficient than using delay functions, as the microcontroller can enter low-power modes while the timer is running in the background. Delay functions, on the other hand, require the microcontroller to remain active and consume more power.

• Flexibility - Internal timers are programmable and configurable, which allows for greater flexibility in setting the timing parameters based on the specific application requirements. Delay functions are typically fixed and cannot be easily modified.

• Multitasking - Internal timers can run in the background while the microcontroller is performing other tasks, which allows for multitasking and more efficient use of system resources. Delay functions, on the other hand, can block the execution of other tasks and slow down the system.

What is an interrupt?

In microcontrollers, interrupts are a critical feature that enables the microcontroller to respond to external events in real-time. A microcontroller is a specialized computer system that is designed to perform a specific task, and interrupts are used to trigger the microcontroller to perform specific actions when an event occurs.

Microcontroller interrupts can be triggered by a variety of sources, including input/output devices, timers, and software events. When an interrupt is triggered, the microcontroller stops executing its current task and switches to the interrupt service routine (ISR) associated with the interrupt. The ISR is a small program that performs the necessary tasks in response to the interrupt, such as updating a display, reading sensor data or communicating with other devices.

What is the purpose of resetting in micro-controller?

The purpose of a reset in a microcontroller is to restore the microcontroller to its initial state and ensure that it starts executing its program from a known state. A reset initializes all the hardware and software components of the microcontroller, including the processor, memory, and peripherals, and sets them to their default values.

There are several reasons why a reset may be necessary in a microcontroller, including:

1. Power-up/reset: When the microcontroller is first powered up, a reset is necessary to initialize all the hardware and software components and ensure that the microcontroller starts executing its program from a known state.

2. Software errors: If the microcontroller encounters a software error or a program crash, a reset may be necessary to restore the microcontroller to a stable state and prevent further errors.

3. Hardware errors: If the microcontroller encounters a hardware error, such as a voltage spike or a malfunctioning peripheral, a reset may be necessary to restore the microcontroller to a stable state and prevent damage to the hardware.

4. Debugging: During the development and testing phase, a reset may be necessary to ensure that the microcontroller starts executing the program from a known state and to test specific features or functions.

In addition to these reasons, many microcontrollers have multiple reset sources, such as a hardware reset pin, a watchdog timer, or a software reset instruction, which provide different levels of reset functionality and can be used in different scenarios.


Atmel code for ATmega2560 – LED blink

#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/common.h>

int main(void)

{

SREG |= (0<<7)                           //disable all interrupts

TCCR1A = 0;

TCCR1B = 0;

TCNT1 = 0;

OCR1A = 31250;                         // compare match register 16MHz/256/2Hz

TCCR1B |= (1<<WGM12);        //CTC mode

TCCR1B |= (1<<CS12);              //256 prescaler

TIMSKI |= (1<<OCIE1A);          //enable timer compare interrupt

 

SREG |= (1<<7);                          //enable all interrupts

DDRB |= (1<<7);

while (1){}

}

ISR(TIMER1_COMPA_vect){

       PORTB^= (1<<7);                       //Toggle the Logic Value of pin7 of PortB

            }

Slight modification on code to blink the LED every two seconds

#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/common.h>

int main(void)

{

SREG |= (0<<7)                                  //disable all interrupts

TCCR1A = 0;

TCCR1B = 0;

TCNT1 = 0;

OCR1A = (31250 * 2);                                   // compare match register (16MHz/256/2Hz * 2)

TCCR1B |= (1<<WGM12);   //CTC mode

TCCR1B |= (1<<CS12);                     //256 prescaler

TIMSKI |= (1<<OCIE1A);     //enable timer compare interrupt

 

SREG |= (1<<7);                                //enable all interrupts

DDRB |= (1<<7);

while (1){}

}

ISR(TIMER1_COMPA_vect){

       PORTB^= (1<<7);                       //Toggle the Logic Value of pin7 of PortB

}


The only change made to the code is the main function, where we have updated the OCR1A register value to 31250*2, which corresponds to a two-second delay between LED toggles. The rest of the code remains the same.

Comments