headerphoto

Interrupts in Microcontrollers

Sabtu, 18 Juli 2009 23:38:29 - oleh : juragan

Interrupts are of immense importance in any microcontroller-based system. A peripheral such as timer/counter, A to D converter, or even a memory or a keyboard can interrupt the microcontroller from its ongoing sequential execution to let it know that it requires attention. This may be due to several reasons such as timer overflowing, ADC returning data, or the signal on an input pin changing. Generally, many standard references compare the polling technique with the interrupt and concludes that the interrupt is a superior technique. However, in microcontroller paradigm, if the developer intends to use the polling technique, then he will have to implement it in software. Ideally the peripheral can be made to set/reset a flag, the status of which requires to be checked using a conditional loop. But the interrupt offers a flexible and time-efficient response to the external events with the least software overhead.

Interrupts in 8051 microcontroller are as follows:
Timer 0 Overflow
Timer 1 Overflow
Reception/Transmission of Serial Character
External Event 0
External Event 1

Writing ISRs in C
Program 1:
This simple program illustrates the initialization sequence for the timer, serial I/O and interrupt. A function called “serial int” is set as the handler for interrupt 4, which is the serial port interrupt. A character ‘V’ is sent to the serial buffer. Observe this in the simulation window of Keil IDE.

Program Source Code
*********************************************************
// Serial Port Interrupt
#include <reg52.h>
#include <stdio.h>
void main (void)
{ int i;
TMOD = (TMOD & 0xF0) | 0x20; // Set Timer 1
Mode 2
SCON = 0x50; // Serial Communication
Mode 1
EA = 1; // Enable Global Interrupt Bit
ES = 1; // Enable Serial Port Interrupts

TH1 = 0xFD; // Load Timer 1 Baud Rate
TR1 = 1; //Set Timer 1 Run control
bit
while (1)
{ SBUF = ‘V’; //Send V continuously on
//Serial Port
for(i=0;i<=10;i++); /* Delay to avoid transmissionoverlapping */
}
}
// Serial Port Interrupt Service Routine.
void timer0 ISR (void) interrupt 4
{ TI = 0; //Clear transmission
interrupt flag
} *********************************************************

Program 2:
This program is modification of the Program 4.14. Unlike the interrupt technique used in Program 2, here the interrupt flag is polled continuously. Observe the transmission of ‘v’ in simulator window.
Program Source Code
*********************************************************
// Serial port Interrupt Programming
#include<REG52.H>
void main()
{
TMOD=0x20; // Timer 1 Mode 2
TH1=0XFD; // Baud rate = 9600 for 11.0592

MHz XTAL TL1=0XFD;
SCON=0X50; //Serial Communication
mode 1
TR1 = 1; // Start Timer 1
TI = 0; // Clear Transmit Interrupt
flag
while(1)
{
SBUF = ‘v’; // Send ‘v’ continuously on
Serial port
while(!TI); // Check TI flag till it is zero
TI = 0; // Clear Transmit Interrupt
flag
}
}

/*********************************************************/

 

kirim ke teman | versi cetak

Berita "C Programming for 8051/ AVR" Lainnya