16C84单片机的RB口中断程序
Interrupts - Part II, Interrupt on RB Change
Introduction
This discussion deals with the interrupt on RB change feature. It assumes the reader is familiar with the general discussion dealing with interrupts.
The PIC16C84 and most other PICs provide a feature where the program may be configured such that an interrupt occurs when there is a change on any of a number of inputs. With the 16C84 (and 16C554/558), the interrupt is caused when there is a change on the upper nibble of Port B, i.e., RB.4::RB.7.
This might be used as a power saving technique. The processor is placed in the sleep mode and is activated only when the user changes the state of any on the four inputs. The processor then performs the defined task and goes back to sleep.
We are planning to use this feature in a 2000 event, 4 Channel event data logger. An event causes a change on any of the four inputs, waking the processor. The processor determines which of the four channels on which the event occurred. It will then fetch a 32-bit elapsed time in seconds from a Dallas DS1602. 32-bits accommodates an elapsed time of some 125 years, which is a bit much. Thus, only the lower 30 elapsed time bits will be used and the two bits associated with the channel will be inserted in the bit 31 and 30 positions. These four bytes will be saved to a Microchip 24LC65 (8K by 8) EEPROM.
Details
The general format of configuring for the Wake Up on Change interrupt is the same as with the external or timer / counter overflow interrupts. When ready for this interrupt, bit RBIE in the INTCON register is set which specifically indentifies the type of interrrupt that will be entertained. The GIE bit is then set.
On interrupt, the program vectors to location 004H. Note that interrupts are now disabled. The processor then performs the defined task, the RBIF is then cleared and the processor then returns from the interrupt service routine using the RETFIE command. Note that interrupts are now enabled.
There is a subtlety associated with the wake-up on change. PORTB must be read. The processor then stores a copy of the high nibble. The RBIF is then set and interrupt occurs when the high nibble of PORTB differs from this copy. Thus, it is important that in the interrupt service routine that PORTB be read so as to update the copy.
The following incorrect code in the interrupt service routine caused us to do a good deal of head scratching.
BCF INTCON, RBIF
MOVF PORTB, W
Indeed, we were clearing the interrupt flag. However, as the state of the high nibble of PORTB still differed from the old copy, the processor immediately set the RBIF flag and on RETFIE, we were immediately interrupted. In fact, the MOVF PORTB, F did us no good whatever.
The correct code was;
MOVF PORTB, W
BCF INTCON, RBIF
What a difference! In performing the MOVF PORTB, W, the old copy of the high nibble of PORTB is updated with the current state. The flag is then cleared. Thus, interrupt will occur when the high nibble of PORTB again changes.
In the following routine, the intent was to identify the "channel" on which the change occurred. Thus, in TOP, PORTB is fetched and saved in ORIGINAL.
In the interrupt service routine, PORTB is fetched and saved in NEW. Thus, the processor now has the new value as the basis for establishing if a future change occurs. The program then ascertains the specific bit which changed, and flashes an LED at a rate depending on which bit caused the interrupt. The NEW then becomes the ORIGINAL, the RBIF is cleared and when RETFIE is executed, contr
单片机 51单片机 MCU 单片机视频教程 单片机开发 ARM单片机 AVR单片机 PIC单片机 Atmel stm32 单片机学习 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)