给你一个例子
- #include "reg51.h"
-
- typedef unsigned char u8; //0 to 255
- typedef unsigned int u16; //0 to 65535
-
- bit Flag_exint0 = 0;
- u8 Delay_exint0 = 0;
- //External interrupt0 service routine
- void exint0() interrupt 0 //(location at 0003H)
- {
- Flag_exint0 = 1;
- Delay_exint0 = 255;
- EX0 = 0;
- }
-
- void main()
- {
- IT0 = 1; //set INT0 int type (1:Falling 0:Low level)
- EX0 = 1; //enable INT0 interrupt
- EA = 1; //open global interrupt switch
-
- while (1)
- {
- if(Delay_exint0>0)
- {
- if(INT0)//sbit in Header file
- {
- Delay_exint0--;
- if(Delay_exint0==0)
- {
- IE0 = 0;
- EX0 = 1;
- }
- }else{
- Delay_exint0 = 255;
- }
- }
- if(Flag_exint0)
- {
- Flag_exint0 = 0;
- //your code E.g.:
- P2 ^= 1;
- }
- }
- }
-
复制代码
|