16C84单片机的RB口中断程序
ol passes back to the main program with interrupts enabled. It is important to note that PORTB is read, thus updating the processor's copy prior to clearing RBIF.
Note that considerable time is spent flashing the LED in the interrupt service routine. If any of the four bits on the high nibble of PORTB should change prior to clearing RBIF, RBIF will again be set by the hardware as there is a change from the latest copy. Thus, on RETFIE, interupts are again enabled and another interrupt will occur. However, this is eactly what is desired; a bit on the high nibble of PORTB changed.
I am none too certain I have been any too clear. Always read PORTB so as to update the processor's reference prior to clearing RBIF. Failure to do so may cause a false interrupt as the processor compares the current state of the high nibble of PORTB with the most recent read. That is, the same change, which is in fact no change, will cause an interrupt.
Note that in this routine, the W and STATUS registers were not saved on entry into the interrupt service routine and restored prior to exit as there was nothing of value to save. Rather, the processor simply goes to sleep and awaits the interrupt.
; WAKE_UP.ASM
;
; This program is intended to illustrate the WAKE-UP on change feature
; associated with many PICs.
;
; RB interrupts are enabled and the processor goes into a sleep mode.
; On interrupt, the specific RB interrupt which caused the interrupt
; is determined and an LED is flashed 10 times at a speed determined
; the input which changed.
;
; Copyright, Locksley Haynes, Morgan State University, Nov 22, '97
LIST p=16c84
#include c:\mplab\p16c84.inc>
__CONFIG 11H
CONSTANT LED=0 ; PORTA pin for LED
CONSTANT CH3=7 ; CH3 corresponds to PORTB.7
CONSTANT CH2=6
CONSTANT CH1=5
CONSTANT CH0=4 ; CH0 corresponds to PORTB.4
CONSTANT VARS=0CH
LOOP1 EQU VARS+0 ; outter timing loop
LOOP2 EQU VARS+1 ; inner timing loop
LED_CNT EQU VARS+2 ; times LED is winked
ORIGINAL EQU VARS+3
NEW EQU VARS+4
CHANGE EQU VARS+5
N EQU VARS+6
ORG 000H
CLRWDT
GOTO TOP
ORG 004H
GOTO WAKE_UP
TOP:
BCF OPTION_REG, 7 ; enable internal pullups
BSF STATUS, RP0
MOVLW 0F0H ; RB.7 - RB.4 are inputs
MOVWF TRISB
BCF STATUS, RP1
BTFSS STATUS, NOT_TO ; not a watch dog timer reset
GOTO TOP_1
; sample PORTB before going to sleep
MOVF PORTB, W ; fetch the current state
MOVWF ORIGINAL ; current state in high nibble
TOP_1:
BSF INTCON, GIE ; enable general interupts
BSF INTCON, RBIE ; enable interrupt on change
L1:
SLEEP
NOP
GOTO L1
WAKE_UP: ; interrupt service routine
MOVF PORTB, W ; sample changed state of pins
MOVWF NEW ; this will become the new original
XORWF ORIGINAL, W
MOVWF CHANGE ; 1's now in high nibble now identifies
; the bit that has changed
CLRF N ; set index to 0
BTFSC CHANGE, CH0
GOTO BLINK
INCF N, F ; N=1
BTFSC CHANGE, CH1
GOTO BLINK
INCF N, F ; N=2
BTFSC CHANGE, CH2
GOTO BLINK
INCF N, F
GOTO BLINK
BLINK: ; N is either 0, 1, 2 or 3 corresponding to the channel
; this is now mapped into a delay
CALL DELAY_LOOKUP
MOVWF LOOP1 ; save the delay in LOOP1
GOTO BLINK_AT_SPEED
DELAY_LOOKUP: ; map N into 100, 150, 200 or 250 msecs
MOVF N, W
ADDWF PCL, F
DT .100, .150, .200, .250
BLINK_AT_SPEED:
MOVLW .10
MOVWF LED_CNT
L2:
BSF PORTA, LED
CALL DELAY
CALL DELAY
BCF PORTA, LED
CALL DELAY
CALL DELAY
DECFSZ LED_CNT, F
GOTO L2
MOVF NEW, W
MOVWF ORIGINAL ; new original states
BCF INTCON, RBIF ; clear interrupt flag
RETFIE
DELAY:
L3:
MOVLW .110
MOVLW LOOP2
L4:
CLRWDT
NOP
NOP
NOP
NOP
NOP
NOP
DECFSZ LOOP2, F
GOTO L4
DECFSZ LOOP1, F
GOTO L3
RETURN
END
单片机 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)