-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterruptExample.c
64 lines (60 loc) · 1.32 KB
/
interruptExample.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <reg52.h>
sbit led = P2^0;
sbit led1 = P3^0;
void TimerDelay (int x,int no);
int j;
void blink(void) interrupt 0{ //pushbutton connected to INT0 //ISR (interrupt service routine)
//toggle led faster 5 times
for(j=0;j<5;j++){
led1=1;
TimerDelay(5,0); //using timer0
led1=0;
TimerDelay(5,0);
}
return;
}
void TimerDelay (int i,int no)
{
int x;
switch (no)
{
case 1:
for (x=0; x<i;x++){
TMOD=0x10; //Timer 1, mode 1(16-bit)
TL1=0xFE; //load TL1
TH1 = 0xA5; //load TH1
TR1=1; //turn on T1
while (TF1==0); //wait for TF1 to roll over
TR1=0; //turn off t1
TF1 = 0; //clear TF1
}
break;
case 0:
for (x=0; x<i;x++){
TMOD=0x01; //Timer 0, mode 1(16-bit)
TL0=0xFE; //load TL0
TH0=0xA5; //load TH0
TR0=1; //turn on T0
while (TF0==0); //wait for TF0 to roll over
TR0=0; //turn off t0
TF0=0; //clear TF0
}
break;
}
//A5FE=42494, 65536-42494=23042, now xtal/12=11.0592mhz/12= 0.9216mhz, so T=1.085us, therefore 23042*1.085 = 25ms
}
void main(void)
{
IT0=1; //falling edge//inside tcon register
EA=1;// enable global interrupts //ea and ex0 in IE register
EX0=1; //enable INT0
led=1;
led1=0;
while (1){
//toggle the led
led=1;
TimerDelay(20,1); //using timer1
led=0;
TimerDelay(20,1);
}
}