A Little about the blog.....

This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......

please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....

Wednesday, May 25, 2011

Interrupts Handling in Brief: Tutorial by Trinadh

Interrupts in embedded systems play a crucial role in efficient programming saving a lot of time and memory in most of the applications.

An interrupt is nothing but “A Pause or Hold” in the current running function or program and execution of a Task which an Interrupt handler is intended to do. Immediately returns back to the original function after the completion of the interrupt handler.

These following are a few Technical words that are to be remembered while dealing with Interrupts:

Interrupt Handler: A Routine function to be executed when an interrupt is called.

Interrupt Handler address: The address where the interrupt handler is to be placed for execution

Generally in 89s52 micro controller, there are 6 six different causes that can call an Interrupt.

1. Timer 0 overflow
2. Timer 1 overflow
3. External 0 Interrupt
4. External 1 Interrupt
5. Receiving a bit in serial Terminal
6. Transmission of a bit in a serial Terminal

In any of the above cases, an interrupt is generated by the corresponding flag.

To differentiation between different interrupts can be made by observing the corresponding flags.

General procedure for using an Interrupt in an Assembly Language Program

1. Write your program logic as per your needs.
2. At the starting of the Program don’t forget to initialize the Interrupt Enable pin without which no interrupt call can be notice by the micro controller. This can be done by using the command “SETB EA”.
3. After initializing, now choose the interrupt that you want to use in your program and enable it. For example if you want to use an external interrupt 1(EX1), use the command “SETB EX1”.
4. Now the microcontroller realizes that you are going to use the EX1 interrupt and responds whenever the interrupt is called and directs to the corresponding Interrupt Handler.
5. Place the Interrupt handler at any location in your program mentioning the interrupt handler address. Care should be taken such that the logic of the program should not be spoiled by misplacing the interrupt handler.
6. Don’t forget to end the interrupt handler with the command RETI instead of usual return command RET.



The sample assembly code for using the interrupts is here below:

org 000h //starting address of the program
sjmp main //jumping to the main program
mov p2,#000h //initializing the PORT2 as an output port
mov P1,#000h //initializing the PORT1 as an output port

org 030h //starting address of the main program
main: setb EA //setting Interrupt Enable =1
setb EX1 // initializing the External Interrupt 0 to use
mov a,#0ffh
mov r4,#08h
here: mov P1,a //continuous loop
delay1: lcall delay // toggling all the bits in P1
djnz r4,delay1//using a small delay routine
cpl a
sjmp here

Interrupt Routine:

org 0013h //Interrupt Handler addrees for EX1
setb P3.3 //Reset the external interrupt 1 bit to 1
PUSH Acc // protecting the value present in the accumulator
mov P2,#0fh //changing the value in PORT2 to observe the routine
POP Acc // sending back the value in accumulator safely
RETI // returning back from the interrupt routine


delay: //Delay Routine
mov R1,#01ch
mov R2,#0ffh
mov R3,#15h
here1: djnz R1,here1
djnz R2,here1
RET
End //end of the program

No comments:

Post a Comment