8051外部中断 [英] 8051 external interrupt
问题描述
如何启用8051的外部中断?
how to enable external interrupt of 8051?
推荐答案
每个8051s中断在中断允许(IE
)特殊功能寄存器(SFR)中都有其自己的位,并且可以通过将相应的位置1来使能.下面的代码示例以8051汇编语言以及C语言编写,以提供正在发生的情况的大致概念.
Each of the 8051s interrupts has its own bit in the interrupt enable (IE
) special function register (SFR) and is enabled by setting the corresponding bit. The code examples below are in 8051 assembly as well as C to provide a general idea of what is taking place.
要启用外部中断0(EX0
),需要设置IE
的位0.
To enable external interrupt 0 (EX0
) you need to set bit 0 of IE
.
SETB EX0
或ORL IE,#01
或MOV IE,#01
要启用外部中断1(EX1
),需要设置IE
的第3位.
To enable external interrupt 1 (EX1
) you need to set bit 3 of IE
.
SETB EX1
或ORL IE,#08
或MOV IE,#08
然后需要通过将IE
的位7设置为全局中断,该位是全局中断允许/禁止位(EA
).如有必要,可以通过中断优先级(IP
)SFR将外部中断的优先级设置为高.
Interrupts then need to be globally enabled by setting bit 7 of IE
, which is the global interupt enable/disable bit (EA
). If necessary, you can set the priority of the external interrupts to high via the interrupt priority (IP
) SFR.
SETB EA
或ORL 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编译器供应商通常会定义自己的设置中断功能的方法.您可能需要查阅特定编译器的文档.
The various 8051 C compiler vendors often define their own methods of setting up interrupt functions. You may need to consult the documentation for your specific compiler.
使用Keil C51编译器定义中断功能(指向应用说明的pdf链接),指定了中断号和寄存器组,其中中断号对应于特定的中断向量地址.
To define an interrupt function using the Keil C51 Compiler (pdf link to application note), an interrupt number and register bank is specified where the interrupt number corresponds to a specific interrupt vector address.
void my_external_interrupt_0_routine(void) interrupt 0 using 2
{
/* do something */
}
使用8051 IAR C/C ++编译器(icc8051)定义中断功能( pdf参考指南链接),可以使用__interrupt
关键字和#pragma vector
指令.
To define an interrupt function using the 8051 IAR C/C++ Compiler (icc8051) (pdf link to reference guide), the __interrupt
keyword and the #pragma vector
directive can be used.
#pragma vector=0x03
__interrupt void my_external_interrupt_0_routine(void)
{
/* do something */
}
如果您是8051的新手,可以在 www.8052.com 上找到大量信息.我还建议> 8051/8052微控制器:体系结构,汇编语言和硬件接口 由网站管理员和8052.com的作者克雷格·斯坦纳(Craig Steiner)撰写.
If you are new to the 8051, there is a wealth of information available at www.8052.com. I would also recommend The 8051/8052 Microcontroller: Architecture, Assembly Language, and Hardware Interfacing written by Craig Steiner, the webmaster and author of 8052.com.
这篇关于8051外部中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!