关于AVR中止:
体系在正常运转主程序时,假如忽然有一个重要的使命要立刻处理,那么体系就要保存现在的作业,然后再去处理这个使命,碑文这个重要使命结束今后再回来原本的主程序持续运转,这便是中止。
主程序一旦进入中止服务程序,那么AVR芯片将主动的封闭大局中止,在这个期间不再碑文其它的中止恳求,直到中止程序结束今后芯片才主动的从头敞开大局中止。(留意,在这个期间某些中止恳求或许会被丢掉,某些恳求会留下中止恳求美丽,一旦当时的中止碑文结束,这个有中止美丽的恳求就有或许立刻得到呼应,如INT0的下降沿触发就会留下中止恳求美丽,而低电平触发就不会流下中止恳求美丽)。假如你想在碑文中止服务程序时呼应别的一个更重要的中止,那么就要在中止服务程序中参加一条翻开大局中止的查办。
咱们现在先来评论外部中止
外部中止要要记住5个寄存器,分别是:
1.status register -SREG状况寄存器。而且外部中止关怀的是它的Bit-7-I位,大局中止使能。置位时,使能大局中止。那些独自的中止,便是你要完结的那些中止,他们的使能由其他独立的操控寄存器操控,就比如说下面说的EIMSK等。假如咱们对I清0,则悉数的中止是不或许发生了,就象一个总的开关相同,即便独自中止标志置位与否。I能够经过SEI和CLI指令来置位和清0。
2.External Interrupt Mask Register -EIMSK外部中止屏蔽寄存器。当INT7–INT0为
3.External Interrupt Control Register A – EICRA外部中止操控寄存器A。Bits 7..0–ISC31, ISC30–ISC00, ISC00:外部中止3 – 0灵敏电平操控位。详见datesheet。假如SREG寄存器的I标志和EIMSK寄存器相应的中止屏蔽方位位,那么外部中止3 – 0由引脚INT3~INT0引脚激活。需求留意的是,改动ISCn时,有或许发生中止。因而,主张首要在EIMSK里铲除相应的中止使能位INTn,然后再改动ISCn。终究呢,千万不要忘记在从头使能中止之前,经过对EIFR的相应中止标志位INTFn写“
4.External Interrupt Control Register B – EICRB外部中止操控寄存器B。Bits 7..0–ISC71, ISC70 – ISC41, ISC40:外部中止7 – 4灵敏电平操控位。详见datesheet,它和EICRA但是不同的。检测信号跳变沿之前MCU首要对INT7:4引脚进行采样。假如挑选了跳变沿中止或是电平改换中止(上升沿和下降沿都将发生中止),只需信号持续时间大于一个时钟周期,中止就会发生;不然无法确保触发中止。要留意咱们XTAL分频器的存在,CPU时钟有或许比XTAL时钟慢。若挑选了低电平中止,低电平有必要坚持到当时指令完结,然后才会发生中止。而且只需将引脚拉低,就会引发中止恳求。而且,相同需求象EICRA相同留意:改动ISCn1/ISCn0时一定要先经过清零EIMSK寄存器的中止使能位来制止中止。不然在改动ISCn1/ISCn0的进程中或许发生中止。
5.External Interrupt Flag Register – EIFR。外部中止标志寄存器。INT7:0引脚电平发生跳变时触发中止恳求,并置位相应的中止标志INTF7:0。假如SREG的位I以及EIMSK寄存器相应的中止使能位为’
咱们应该清楚的是:外部中止经过引脚INT7:0触发。只需使能了中止,即便引脚INT7:0装备为输出,只需电平发生了适宜的改变,中止也会触发。这个特色能够用来发生软件中止。经过设置外部中止操控寄存器–EICRA (INT3:0)和EICRB (INT7:4),中止能够由下降沿、上升沿,或者是低电平触发。当外部中止使能而且装备为电平触发,只需引脚电平为低,中止就会发生。若要求INT7:4在信号下降沿或上升沿触发,I/O时钟有必要作业,如数据手册P 33“时钟体系及其散布”阐明的那样。INT3:0的中止条件检测则是异步的。也便是说,这些中止能够用来将器材从睡觉形式唤醒。在睡觉进程(除了闲暇形式)中I/O时钟是中止的。
上面的多为数据手册晒干的内容。惹祸咱们回有几个困惑。
1.为何AVR写“
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=749852
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=691118&bbs_page_no=1&bbs_id=1000
http://www.c51bbs.com/c51bbs/topic1/c51bbs17322.htm
但终究呢,真实的原因是在AVR-GCC的协助文档avr-libc原本就有的,FAQ24,Why are (many) interrupt flags cleared by writing a logical 1?当然,你的E文要好地。
拜见如下阐明:
Why are (many) interrupt flags cleared by writing a logical 1?
Usually, each interrupt has its own interrupt flag bit in some control register, indicating the specified interrupt condition has been met by representing a logical
From the hardwares point of view, an interrupt is asserted as long as the respective bit is set, while global interrupts are enabled. Thus, it is essential to have the bit cleared before interrupts get re-enabled again (which usually happens when returning from an interrupt handler).
Only few subsystems require an explicit action to clear the interrupt request when using interrupt handlers. (The notable exception is the TWI interface, where clearing the interrupt indicates to proceed with the TWI bus hardware handshake, so its never done automatically.)
However, if no normal interrupt handlers are to be used, or in order to make extra sure any pending interrupt gets cleared before re-activating global interrupts (e. g. an external edge-triggered one), it can be necessary to explicitly clear the respective hardware interrupt bit by software. This is usually done by writing a logical 1 into this bit position. This seems to be illogical at first, the bit position already carries a logical 1 when reading it,so why does writing a logical 1 to it clear the interrupt bit?
The solution is simple:writing a logical 1 to it requires only a single OUT instruction, and it is clear that only this single interrupt request bit will be cleared. There is no need to perform a read-modify-write cycle (like, an SBI instruction), since all bits in these control registers are interrupt bits, and writing a logical 0 to the remaining bits (as it is done by the simple OUT instruction) will not alter them, so there is no risk of any race condition that might accidentally clear another interrupt request bit. So instead of writing
TIFR |= _BV(TOV0); /* wrong! */
simply use
TIFR = _BV(TOV0);
2.只需使能了中止,即便引脚INT7:0装备为输出,只需电平发生了适宜的改变,中止也会触发.这句怎样了解?答:也便是说,你翻开了中止int0和int1,一起这两个管脚界说为输出,然后,你运用软件设置这两个管脚的输出电平,当分量中止条件时,中止就发生了。这不便是软件中止吗?
3.在装备了外部中止操控寄存器、屏蔽寄存器、标志寄存器后,是否还需求设置IO口为输入端口呢?答:是的,不过IO口上电时就默许是输入了,能够不写这条指令。弥补,设置成输出也照样发生中止。它用PINx读。