Blinking LED Using External Interrupt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Program to Blink LED using Ext Interrupt
#include<reg51.h>
// LED Pin connections
sbit LED = P1^0; // LED connected to Pin P1.0
//Function declarations
void init_ports(void);
void init_ExtINT0(void);
/////////////////Main function///////////////////////
void main(void)
{
init_ports(); // Make all ports to LOW
init_ExtINT0(); // Intialize INT0 interrupt
while(1);
}
// Init Port function
void init_ports(void)
{
P0 = 0x00; // Make all pins zero
P1 = 0x00; // Make all pins zero
P2 = 0x00; // Make all pins zero
P3 = 0x04; // Make P3.2 (INT0) pin high only
}
// External INT0 pin interrupt init function
void init_ExtINT0(void)
{
IT0 = 1; //Negative Edge triggered interrupt mode
EX0 = 1; //Enable external interrupt INT0
EA = 1; //Enable global interrupts
}
// INT0 ISR
void external0_isr(void) interrupt 0
{
LED = ~LED; // Toggle LED pin
}