《PIC学习笔记之中断》由会员分享,可在线阅读,更多相关《PIC学习笔记之中断(5页珍藏版)》请在金锄头文库上搜索。
1、实验环境:Proteus编程语言:汇编编程环境:MPLAB单片机:PIC16F877晶振:4MHzProteus仿真电路如图。(介绍见下文)丄宀, P Q-5?3TX三F:S-图1 (中断发生前) -WRAJirLkia cec-Li JT“axry.-wijii 斗UdSjliiAE!zE-tffaTTTU? A厂和-PfSt- YTlD&-A:t rL-oiLr 1HE止m ntdmhWf图2 (发生中断)程序源代码:LIST P=16F877INCLUDE P16F877.INCW_TEMP EQU 40H;定义数据暂存寄存器地址,S_TEMP EQU 41HPCH_TEMP EQU 4
2、2HFSR_TEMP EQU 43HDELAYDATA0 EQU 20H;定义延时函数相关变量存储器地址DELAYDATA1 EQU 21HDELAYDATA2 EQU 22HORG0000HGOTO MAINORG0004HINT: MOVWFW_TEMPSWAPF STATUS,0CLRF STATUSMOVWFS_TEMPMOVF FSR,0MOVWFFSR_TEMPMOVF PCLATH,0MOVWFPCH_TEMPCLRF PCLATH以上为现场保护,在进入中断之前首先将 w, status, PCLAThFSR暂存起来*H-r |來匚 AkTRg壬阜 佥BCFSTATUS,RP1B
3、CFSTATUS,RP0BSFPORTB,1BCFPORTB,2CALLDELAY20MS调用延时函数BCFINTCON,1中断处理结束,要将相应的中断标志位清零*.以下为恢复现场MOVFPCH_TEMP,0MOVWFPCLATHMOVFFSR_TEMP,0MOVWFFSRSWAPF S_TEMP,0MOVWFSTATUSSWAPF W_TEMP,1SWAPF W_TEMP,0RETFIEDELAY20MS: MOVLW D250MOVWF DELAYDATA0DELAY1NOPNOPNOPNOPNOPNOPNOPNOPMOVLW D250MOVWFDELAYDATA1DELAY2DECFSZ
4、DELAYDATA1GOTO DELAY2DECFSZDELAYDATA0GOTO DELAY1RETLW 0MAIN: MOVLW HD0MOVWF INTCON开总中断(GIE=1和INT中断(INTE=1并清零INT 中断标志位(INTF)MOVLW H80MOVWFOPTION_REG设定PORTB拉使能,INT下降沿触发中断BCF PORTB,1BANKSEL TRISB 进入 TRISB所在 bank1BCF TRISB,1BCF TRISB,2BCF STATUS,RP1BCF STATUS,RP0BSF PORTB;2 点亮 LED1BCF P0RTB;1 关闭 LEDOEND