[?] Subscribe To This Site

Add to Google
Add to My Yahoo!
Add to My MSN
Add to Newsgator
Subscribe with Bloglines

Claim Your: Useful

"Arduino Software Guide"

   Right Now...

Custom Search

Sign up for MicroZine:
''The'' Microcontroller Newsletter

Enter your first Name and primary email address in the form below:

And receive absolutely FREE a full project for:

"Measuring Analogue Voltages
Without An ADC"

(Using only one pin).

Instant Download:
You Can
Get It Right Now

Warning: This project could be Removed
at any time.  

It will NOT be
available indefinitely SO

To avoid
disappointment get it:


Don't worry -- your e-mail address is totally secure. I promise to use it only to send you MicroZine
Remember this is a project with full description and fully debugged C Source code - and it's not available from the main website.

You can only get it through this newsletter.

To get exclusive access Enter your first name Name and primary email address Now in the form above.:

But wait !

There's more...

You'll receive more
free and exclusive reports as well as site information and site product updates.

Scroll up to the form above and sign up NOW.  Don't forget it's FREE and if you don't like it you can unsubscribe at any time.

Click Here Now to use the form above to get your Valuable information absolutely free.

Social Bookmarking
Click & Add:
add to BlinkBlink
add to Del.icio.usDel.icio.us
add to DiggDigg
add to FurlFurl
add to GoogleGoogle
add to SimpySimpy
add to SpurlSpurl
Bookmark at TechnoratiTechnorati
add to YahooY! MyWeb
Readers comments

"I wanted to thank
you so so so much
for all the information
you have provided in
your site it's


- Ranish Pottath

"This site really is
the best and my favorite.
I find here many useful
projects and tips."

- Milan


Learn PIC C Now
Wondering how to program your next project using C and need a great start?

"Awesome site,
very, very easy and nice
to navigate!"

- Matt

"I am a newbie to PIC
and I wanted to say
 how great your
site has been for me."

- Dave


Learn Microcontrollers
"Interested in

Sign up for The
Free 7 day guide:


"Your site is a great
and perfect work.

- Suresh


"I couldn't find the correct
words to define
yourweb site.

Very useful, uncovered,
honest and clear.

Thanks so much for
your time and works.

- Anon

A Hardware Interrupt Tutorial.

This tutorial describes what a hardware interrupt is and how and when to use it.


An interrupt is a signal that stops the current program forcing it to execute another program immediately. The interrupt does this without waiting for the current program to finish. It is unconditional and immediate which is why it is called an interrupt.

The whole point of a interrupt is that the main program can perform a task without worrying about an external event.


For example if you want to read a push button connected to one pin of an input port you could read it in one of two ways either by polling it or by using interrupts.


Polling is simply reading the button input regularly. Usually you need to do some other tasks as well e.g. read an RS232 input so there will be a delay between reads of the button. As long as the delays are small compared to the speed of the input change then no button presses will be missed.

If however you had to do some long calculation then a keyboard press could be missed while the processor is busy. With polling you have to be more aware of the processor activity so that you allow enough time for each essential activity.

Hardware interrupt

By using a hardware interrupt driven button reader the calculation could proceed with all button presses captured. With the interrupt running in the background you would not have to alter the calculation function to give up processing time for button reading.

The interrupt routine obviously takes processor time – but you do not have to worry about it while constructing the calculation function.

You do have to keep the interrupt routine small compared to the processing time of the other functions - its no good putting tons of operations into the ISR. Interrupt routines should be kept short and sweet so that the main part of the program executes correctly e.g. for lots of interrupts you need the routine to finish quickly ready for the next one.

Hardware Interrupt or polling ?

The benefit of the hardware interrupt is that processor time is used efficiently and not wasted polling input ports. The benefit of polling is that it is easy to do.

Another benefit of using interrupts is that in some processors you can use a wake-from-sleep interrupt. This lets the processor go into a low power mode, where only the interrupt hardware is active, which is useful if the system is running on batteries.

Hardware interrupt Common terms

Terms you might hear associated with hardware interrupts are ISR, interrupt mask, non maskable interrupt, an asynchronous event, interrupt vector and context switching.

ISR Interrupt Service Routine.
Interrupt vector The address that holds the location of the ISR.
Interrupt mask Controls which interrupts are active.
NMI Non Maskable Interrupt - an interrupt that is always active.
Asynchronous event An event that could happen at any time.
Context switching Saving/restoring data before & after the ISR.


An ISR is simply another program function. It is no different to any other function except that it does context switching (saving/restoring processor registers) and at the end of the function it re-enables interrupts.

After the ISR finishes program execution returns to the original program and it continues exactly from where it was interrupted. The orriginal program will have no idea that this has happened.

Hardware Interrupt vector

This is a fixed address that contains the location of your ISR – for a PIC micro it is usually address 4. In other micros there may be an interrupt vector for each vector – you have to make an interrupt routine for each one that you want to use. For PIC micros you have just one interrupt and you have to detect which interrupt triggered by examining the interrupt flag register(s).

You program the interrupt address with the address of your interrupt routine. Whenever the interrupt is triggered (and if the interrupt is unmasked) program operation jumps to the location of your interrupt routine.

Note: high level language compilers take care of all of this for you - in 'C' you just declare the function using the keyword interrupt (as the type returned from the function). It then puts the address of this routine in the interrupt vector.


The NMI is exactly the same as a normal interrupt except that you can not control whether or not it is active - it's always active. It is more commonly found on larger (non-microcontroller) processors as a dedicated input pin. In this case it is more than likely fed with a brown-out (PSU voltage dip) detector circuit.

You don't need it in a microcontroller as you can achieve exactly the same functionality using a programmable interrupt and many microcontrollers have built in BODs.

Asynchronous event

This is simply an event that is not synchronised to the processor clock. It is an event that the processor can not predict e.g. A button press.

Context switching

This means that the register state is preserved at the start of an ISR and restored at the end of an ISR. It ensures that the function that was interrupted is not affected by the ISR.

A Basic Interrupt System.

This is a general description of an interrupt system (biased slightly to PIC micros) which is true for any interrupt module and it is useful for understanding how to control and use interrupts.

Hardware Interrupt Block Diagram

Interrupt tutorial block diagram.

Input sources.

These are signals that start an interrupt. They can be external pins (where a rising or falling edge of a input triggers the interrupt) or internal peripheral interrupts e.g. ADC completed, Serial Rx data received, timer overflow etc. (The 16F877 has 15 interrupt sources).

Note: you can ususlly control which edge (rising or falling) is used by setting bits in yet another register!

Interrupt flags.

Each hardware interrupt source has an associated interrupt flag - whenever the interrupt is triggered the corresponding interrupt flag is set. These flags are usually stored as bits within an interrupt register.

The processor can read from and write to the interrupt register, reading from it to find out which interrupts occurred and writing to it to clear the interrupt flags.

Interrupt mask

The interrupt mask has a set of bits identical to those in the interrupt register. Setting any bit (or unmasking) lets the corresponding signal source generate an interrupt - causing the processor to execute the ISR.

Note: When a bit in the mask is clear (masked) the ISR is not activated for that signal source but the interrupt register is still set by the signal source. So you can still detect what the hardware is doing by polling the interrupt register.

Hardware interrupt vector

The interrupt vector is a location in memory that you program with the address of your interrupt service routine (ISR). Whenever an unmasked interrupt occurs program execution starts from the address contained in the interrupt vector.

For PlC micros the hardware interrupt vector address is usually 0004.

Jump from Hardware Interrupt to:
Best-microcontroller-projects home page.

Privacy Policy | Contact | About Me

Site Map | Terms of Use | Links


  Best Microcontroller projects.