⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab5.asm

📁 定时器和中断
💻 ASM
字号:
		#INCLUDE p16F877A.inc	; Include the standard definitions
		#INCLUDE 701PIC.inc		; Include MACROs from MASTER抯 03, PIC 102 class
 __CONFIG   _CP_OFF &  _WDT_OFF &  _XT_OSC & _LVP_OFF & _BODEN_OFF
 
ISRCtr	EQU	0x30				; location for ISRCtr variable (countdown for ISR loops)
Cycle4	EQU	0x7F
TMR0Val	EQU	D'61'

		ORG	0					; Reset Vector Location (0)
Start 
		goto	Initialize		; On start-up go to Initialization code

		ORG	4					; Interrupt Vector Location (4)
ISR
		BTFSS	INTCON,TMR0IF
		RETFIE	
		ISRSave	
		BANKSEL TMR0						; Macro to context save in ISR
		BCF	INTCON,TMR0IF						; Clear the TMR0 interrupt condition
		MOVLW	TMR0Val
		MOVWF	TMR0						; Reload the Timer for accurate timing
		DECFSZ  Cycle4						; Decrement the ISRCounter (from 4 to 0) and test for 0
		goto	SkipToggle		; if the counter is not yet 0, then toggling is not needed
		goto	ToggleLED		; if the counter is 0, run toggle code

ToggleLED
		MOVLW	D'4'					; put 4 into W to reload into the
		MOVWF	Cycle4					; Interrupt counter (after 4 counts we need to reset it)

		MOVLW   D'1'
		XORWF   PORTB						; write 1 to W in order to, 
								;  toggle LED 0 of PORTB - XOR with 1 will toggle the bit

SkipToggle						; Done interrupt, so restore and exit
		ISRRestore				; Macro to context restore when leaving ISR
		retfie						; Return from interrupt

Initialize
		LEDOff	0				; Ensure LED 0 is off
		LEDEnable	H'FE'		; MACRO to setup I/O port to drive LED RB0
		banksel PORTA
		MOVLW	D'4'
		MOVWF	Cycle4					; Write 4 to W to 
		CALL	DoReloadTimer				;  preload the ISR counter with
								; Preload the timer for appropriate time delay
		BSF	INTCON,GIE						; Turn on GIE and 
		BSF	INTCON,TMR0IE						;  TMR0 interrupt (T0IE)
		BANKSEL TRISA						; set banking to access OPTION_REG in bank 1
		MOVLW	B'11000111'
		MOVWF	OPTION_REG						; Set up TMR0 for 256 prescale and 
								;  internal clock mode
		BANKSEL PORTA						; return to accessing BANK 0

		goto	$				; Wait forever for Interrupt


; Subroutine to reload TMR0 
DoReloadTimer
		MOVLW	D'61'						; Preload Timer to 
		MOVWF	TMR0						; count 195 times before roll-over
		RETURN						; return from subroutine
		END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -