8051外部中断

如何启用8051的外部中断?

每个8051中断在中断使能( IE )特殊function寄存器(SFR)中都有自己的位,并通过设置相应的位使能。 下面的代码示例在8051汇编和C中,以提供正在发生的事情的一般概念。

要启用外部中断0( EX0 ),需要设置IE 0位。

SETB EX0ORL IE,#01MOV IE,#01

要启用外部中断1( EX1 ),需要设置IE 3位。

SETB EX1ORL IE,#08MOV IE,#08

然后,需要通过设置IE 7位(全局中断启用/禁用位( EA ))来全局启用中断。 如有必要,可以通过中断优先级( IP )SFR将外部中断的优先级设置为高。

SETB EAORL IE,#80

C中的示例:

 #define IE (*(volatile unsigned char *)0xA8) #define BIT(x) (1 << (x)) ... IE &= ~BIT(7); /* clear bit 7 of IE (EA) to disable interrupts */ ... IE |= BIT(0); /* set bit 0 of IE (EX0) to enable external interrupt 0 */ ... IE |= BIT(1); /* set bit 3 of IE (EX1) to enable external interrupt 1 */ ... IE ^= BIT(7) /* toggle bit 7 of IE (EA) to re-enable interrupts */ 

要么

 IE = 0x89; /* enable both external interrupts and globally enable interrupts */ 

各种8051 C编译器供应商经常定义自己设置中断函数的方法。 您可能需要查阅特定编译器的文档。

要使用Keil C51编译器定义中断函数( pdf链接到应用笔记 ),需要指定中断号和寄存器库,其中中断号对应于特定的中断向量地址。

 void my_external_interrupt_0_routine(void) interrupt 0 using 2 { /* do something */ } 

要使用8051 IAR C / C ++编译器(icc8051)( 参考指南的pdf链接 )定义中断函数,可以使用__interrupt关键字和#pragma vector指令。

 #pragma vector=0x03 __interrupt void my_external_interrupt_0_routine(void) { /* do something */ } 

如果您是8051的新手 ,请访问www.8052.com获取丰富的信息。 我还推荐8051/8052微控制器:架构,汇编语言和硬件接口 ,由网站管理员和8052.com的作者Craig Steiner编写。

非常好的教程,它帮助了我很多。 http://www.8052.com/tutint.phtml