如何使能8051的外部中断?
最佳答案
每个 8051s 中断在中断使能 (IE
) 特殊功能寄存器 (SFR) 中都有自己的位,并通过设置相应的位来启用。下面的代码示例采用 8051 汇编语言和 C 语言,以提供对正在发生的事情的一般概念。
要启用外部中断 0 (EX0
),您需要设置 IE
的位 0。
SETB EX0
或 ORL IE,#01
或 MOV IE,#01
要启用外部中断 1 (EX1
),您需要设置 IE
的位 3。
SETB EX1
或 ORL IE,#08
或 MOV IE,#08
然后需要通过设置 IE
的第 7 位来全局启用中断,这是全局中断启用/禁用位 (EA
)。如有必要,您可以通过中断优先级 (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 编译器供应商通常定义他们自己的设置中断函数的方法。您可能需要查阅特定编译器的文档。
要使用 Keil C51 编译器 (pdf link to application note) 定义中断函数,需要指定中断号和寄存器组,其中中断号对应于特定的中断 vector 地址。
void my_external_interrupt_0_routine(void) interrupt 0 using 2
{
/* do something */
}
要使用 8051 IAR C/C++ 编译器 (icc8051) ( pdf link to reference guide ) 定义中断函数,可以使用 __interrupt
关键字和 #pragma vector
指令用过。
#pragma vector=0x03
__interrupt void my_external_interrupt_0_routine(void)
{
/* do something */
}
如果您是 8051 的新手,可以在 www.8052.com 上找到大量信息。 .我也会推荐 The 8051/8052 Microcontroller: Architecture, Assembly Language, and Hardware Interfacing由 8052.com 的网站管理员兼作者 Craig Steiner 撰写。
关于c - 8051外部中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2744953/