how to use interupts

Sponsored Link :

. .


interrupt 300x150 how to use interupts

interrupt

When receiving data and change in status from I/O Ports, we have two methods available to us. We can Poll the port, which involves reading the status of the port at fixed intervals to determine whether any data has been received or a change of status has occurred. If so, then we can branch to a routine to service the ports requests. As you could imagine, polling the port would consume quite some time. Time which could be used doing other things such refreshing the screen, displaying the time etc. A better alternative would be to use Interrupts. Here, the processor does your tasks such as refreshing the screen, displaying the time etc, and when a I/O Port/Device needs attention as a byte has been received or status has changed, then it sends a Interrupt Request (IRQ) to the processor. Once the processor receives an Interrupt Request, it finishes its current instruction, places a
few things on the stack, and executes the appropriate Interrupt Service Routine (ISR) which can remove the byte from the port and place it in a buffer. Once the ISR has finished, the processor returns to where it left off.
Using this method, the processor doesn’t have to waste time, looking to see if your I/O Device is in need of attention, but rather the device will interrupt the processor when it needs attention. When receiving data and change in status from I/O Ports, we have two methods available tous. We can Poll the port, which involves reading the status of the port at fixed intervals to determinewhether any data has been received or a change of status has occurred. If so, then we can branch to aroutine to service the ports requests.As you could imagine, polling the port would consume quite some time. Time which could beused doing other things such refreshing the screen, displaying the time etc. A better alternative wouldbe to use Interrupts. Here, the processor does your tasks such as refreshing the screen, displaying thetime etc, and when a I/O Port/Device needs attention as a byte has been received or status haschanged, then it sends a Interrupt Request (IRQ) to the processor.Once the processor receives an Interrupt Request, it finishes its current instruction, places afew things on the stack, and executes the appropriate Interrupt Service Routine (ISR) which canremove the byte from the port and place it in a buffer. Once the ISR has finished, the processor returnsto where it left off.Using this method, the processor doesn’t have to waste time, looking to see if your I/O Deviceis in need of attention, but rather the device will interrupt the processor when it needs attention.

Interrupts and Intel Architecture

Interrupts do not have to be entirely associated with I/O devices. The 8086 family of microprocessors provides 256 interrupts, many of these are only for use as software interrupts, which we do not attempt to explain in this document. The 8086 series of microprocessors has an Interrupt Vector Table situated at 0000:0000 which extends for 1024 bytes. The Interrupt Vector table holds the address of the Interrupt Service Routines (ISR), all four bytes in length. This gives us room for the 256 Interrupt Vectors.

The average PC, only has 15 Hardware IRQ’s plus one Non-Maskable IRQ. The rest of the interrupt vectors are used for software interrupts and exception handlers. Exception handlers are routines like ISR’s which get called or interrupted when an error results. Such an example is the first Interrupt Vector which holds the address of the Divide By Zero, Exception handler. When a divide by zero occurs the Microprocessor fetches the address at 0000:0000 and starts executing the code at this Address.

Hardware Interrupts

The Programmable Interrupt Controller (PIC) handles hardware interrupts. Most PC’s will have two of them located at different addresses. One handles IRQ’s 0 to 7 and the other, IRQ’s 8 to 15, giving a total of 15 individual IRQ lines, as the second PIC is cascaded into the first, using IRQ2. Most of the PIC’s initialization is done by BIOS, thus we only have to worry about two instructions. The PIC has a facility available where we can mask individual IRQ’s so that these requests will not reach the Processor. Thus the first instruction is to the Operation Control Word (OCW1) to set which IRQ’s to mask and which IRQ’s not too. As there are two PIC’s located at different addresses, we must first determine which PIC we need to use. The first PIC, located at Base Address 0x20h controls IRQ 0 to IRQ 7.

Implementing the Interrupt Service Routine (ISR)

In C you can implement your ISR using void interrupt yourisr() where yourisr is a far

placed in the Interrupt Vector Table so that, it will be called when interrupted.
The following code is a basic implementation of an ISR.
void interrupt yourisr() /* Interrupt Service Routine (ISR) */

{

disable();

/* Body of ISR goes here */

oldhandler();

outportb(0×20,0×20); /* Send EOI to PIC1 */

enable();

}


void interrupt yourisr() defines this function as an Interrupt Service Routine. disable(); clears the interrupt flag, so that no other hardware interrupts ,except a NMI (Non-Maskable Interrupt) can occur. Otherwise, and interrupt with a higher priority that this one can interrupt the execution of this ISR. However this is not really a problem in many cases, thus is optional.

The body of your ISR will include code which you want to execute upon this interrupt request being activated. Most Ports/UARTs may interrupt the processor for a range of reasons, eg byte received, time-outs, FIFO buffer empty, overruns etc, thus the nature of the interrupt has to be determined. This is normally achieved by reading the status registers of the port you are using. Once it has been established, you can service it’s requests. If you read any data from a port, it is normally common practice to place it in a buffer, rather that immediately writing it to the screen, inhibiting further interrupts to be processed. Most Ports these days will have FIFO buffers which can contain more than one byte, thus repeat your read routine, until the FIFO is empty, then exit your ISR.

Keyword :
interrupt handler ,routine ,processor ,software ,signal ,vector ,timer interrupt ,the user ,registers ,priority ,microcontroller ,memory ,keyboard ,interrupt vector ,interrupt service routine ,vectors ,tutorial ,trigger ,status register ,spurious interrupt ,programmer ,priority interrupt ,peripheral ,offset ,maskable interrupt ,irq number ,interrupt vector table ,interrupt request ,interrupt mask ,interrupt handling ,interrupt controller ,intcon ,instruction ,how to ,handlers ,external interrupt ,exceptions ,exception ,embedded systems ,controller ,character ,application note


admin tagged this post with: , , , , , , , Read 322 articles by

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Select Category