(八)外部中断的设计与应用 02 中断优先级嵌套设计

仿真电路图:

 

参考代码:

 1 #include<reg52.h>
 2 #define uchar8 unsigned char 
 3 #define uint16 unsigned int
 4 
 5 /*****************************/
 6 // 函数名称: DelayMS( )
 7 // 函数功能: 延时
 8 // 入口函数: 延时毫秒
 9 // 出口函数: 无
10 /*************************                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              *******************/
11 void delay(uint16 val)
12 {
13        uint16  i,j;
14        for( i=0;i<val;i++)
15            for(j=0;j<113;j++);
16 
17 }
18 
19 
20 void tx_init()    // 中断系统初始化函数
21 {
22     EA=1;
23     PX0=1;         // 外中断0 为 高优先级
24     PX1=0;         // 外中断1 为 低优先级
25 
26 
27     EX0=1;         // 允许外部中断
28     IT0=1;         // 外中断0跳沿触发 
29     EX1=1;         // 同上
30     IT1=1;
31 
32 }
33 
34 void main()
35 {
36     tx_init();
37     P1=0x00;
38     while(1)
39     {
40       delay(1000);
41       P1=~P1;
42     }
43 }
44 
45 void tx0_func() interrupt 0     // 外中断0 中断处理函数
46 {
47     uchar8 i;
48     uchar8 code table0[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; // 流水灯 ,一个灯灭,从右往左 
49     
50     for(i=0;i<7;i++)
51     {
52        P0=table0[i]    ;
53        delay(1000);
54     
55     }
56 
57     P0=0xff;    // 处理完毕灯全部熄灭
58 
59 }
60 
61 void tx1_func() interrupt 2     // 外中断1 中断处理函数
62 {
63     uchar8 i;
64     uchar8 code table2[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; // 流水灯一个灯亮,从右往左
65     
66     for(i=0;i<7;i++)
67     {
68        P0=table2[i]    ;
69        delay(1000);
70     
71     }
72 
73     P0=0x00;    // 处理完毕灯全部亮
74 
75 }

 

 

程序说明, S0 按键 代表 外部中断0,是高优先级

               S1 按键 代表 外部中断1, 是低优先级

               只能高优先级打断低优先级,所以外中断1 的处理程序可以被 外中断0的处理函数打断,形成中断嵌套。执行完 中断0,还是会返回执行中断1 的。

 

posted on 2015-12-26 18:31  无悔这一生。  阅读(780)  评论(0编辑  收藏  举报

导航