You are on page 1of 4

http://microcontrollerlearn.blogspot.

com/

Lecture 8: Using external interrupt


In this lecture we will discuss interrupt in 8051 microcontroller. Interrupt mean notification that specific event occurred and controller should know in this moment the event. In other word interrupt stop the overflow of program and after interrupt routine finished return to the program flow.

Types of interrupts: 1) External interrupt Comes from external interrupt from outside of microcontroller. 2) Internal interrupt comes from internal event from microcontroller like timer overflow or serial interrupt. In this lecture we will use external interrupt to invert status of led on and off. Connect hardware as below.

http://microcontrollerlearn.blogspot.com/

http://microcontrollerlearn.blogspot.com/

We will use external interrupt zero (pin 12). First initialize interrupt by following only these three steps. 1) Enable this bit should be set by one before using any type of interrupt EA= 1; 2) External interrupt of microcontroller have two configurations either rising edge or falling edge.

we will use falling edge in our project. By set IT0 by one. IT0 = 1;
http://microcontrollerlearn.blogspot.com/

http://microcontrollerlearn.blogspot.com/

3) Set this bit to enable external interrupt zero. EX0 = 1; Now we initialized external interrupt zero and ready to used. void interrupt_init(void){//interruptEA= 1; //enable all interrups it must be enabled before using any interrupt//external interrupt 0IT0 = 1; //Configure interrupt 0 for falling edge on /INT0EX0 = 1; //enable external interrupt 0 used for switch}

How to set function of interrupt in keil ? Refer to the help of keil to know the number of interrupt.

The external interrupt 0 with number 0. So the architecture of function as below. void function_name (void) interrupt interrupt_number{ //your code} function_name: Write your function name. We will call it external_interrupt_0 interrupt_number: Write here interrupt number. Refer to the upper table the number will be zero (0). //your code: Write here your code required when interrupt occurred. We will invert the status of led. When it zero becomes one and vice versa. led= ~led; So the final function shape will be as below. void external_interrupt_0 (void) interrupt 0{ ~led;} led=

The circuit will working when button pressed then we enter to the interrupt function then invert the status of led. Full code

#include <at89c51xd2.h> sbit led= P2^0; void external_interrupt_0 (void) interrupt 0{


http://microcontrollerlearn.blogspot.com/

led=

http://microcontrollerlearn.blogspot.com/

~led;} void interrupt_init(void){//interruptEA= 1; //enable all interrups it must be enabled before using any interrupt//external interrupt 0IT0 = 1; //Configure interrupt 0 for falling edge on /INT0EX0 = 1; //enable external interrupt 0 used for switch} void main(void){ interrupt_init(); while(1); } The circuit will working as below in video

https://www.youtube.com/watch?v=dixrNVdRPjc
Thanks Eng.Mohamed Abbas

http://microcontrollerlearn.blogspot.com/

You might also like