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

📄 701pic.inc

📁 pic单片机资料
💻 INC
字号:
; please note, this file is not finshed (comments, etc), 
; but the code does work.


LEDEnable	MACRO	BitPattern
	BANKSEL	TRISB
	movlw	BitPattern
	movwf	TRISB
	BANKSEL	PORTB
	ENDM
	
LEDOn		MACRO	LEDNumber
	movlw	1 << LEDNumber
	iorwf	PORTB,F
	ENDM

LEDOff		MACRO	LEDNumber
	movlw	H'FF' - ((1 << LEDNumber) & H'00FF')
	andwf	PORTB,F
	ENDM

Wait4Ever	MACRO
	goto	$
	ENDM
 
; Sets Piezo to beep at desired frequency, must declare
; Frequency and #DEFINE "DEVICE_FREQ_HZ".  To control beep,
; use "PiezoOn" and "PiezoOff" macros
PiezoEnable	MACRO	Freq
	BANKSEL	TRISC
	bcf		TRISC,2
	movlw	((DEVICE_FREQ_HZ/D'64')/Freq)-1
	movwf	PR2
	movlw	((DEVICE_FREQ_HZ/D'128')/Freq)
	BANKSEL	CCPR1L
	movwf	CCPR1L
	movlw	0x06		; Turn on TMR2 (for PWM) 
	movwf	T2CON		; and /16 prescale
	ENDM

PiezoOn		MACRO
	movlw	H'0C'
	movwf	CCP1CON
	ENDM

PiezoOff	MACRO
	clrf	CCP1CON
	ENDM


; *************************************************************************
; * MACRO       This Macro loads literal values into the variables Dly3 
; * Dly32       through Dly0 and then calls DoDly32 to run the delay.
; *             This is the macro DEFINITION.  The code is pasted in 
; *             place of the macro call later in the code.
; *             Be sure to ensure all variables are in the same bank.
; *
; * USAGE (for 4 MHz clock):
; *	Dly32	D'2'		; * 2 loops x 40 us = 40 us           
; *	Dly32	D'5000'		; * 5000 loops x 20 us = 100 ms       
; *	Dly32	D'50000'	; * 50000 loops x 20 us = 1 s         
; *	Dly32	D'50000'*D'60'	; * 60 x 1 second loops = 1 min   
; *	Dly32	D'45000000'	; * 4.5 million loops x 20 us = 90 s  
; *************************************************************************
Dly32	MACRO DLY
		
        BANKSEL Dly3
        movlw   (DLY-1) & H'FF'		; * Take the delay value argument 
        movwf   Dly0    		; * from the macro, precalculate 
        movlw   (DLY-1) >>D'08' & H'FF'	; * the required 4 RAM values and
        movwf   Dly1    		; * load the The RAM values Dly3 
        movlw   (DLY-1) >>D'16' & H'FF'	; * though Dly0.
        movwf   Dly2    		; * Bytes are shifted and anded
        movlw   (DLY-1) >>D'24' & H'FF'	; * by the assembler to make 
        movwf   Dly3    		; * user calculations easier

        nop	; 7 cycle delay for precision delay value
        nop ;
        nop ;
        nop ;
        nop ;
        nop ;
        nop ;

        call    DoDly32 ; Call DoDly32 to run the delay loop.
        ENDM            ; End of the Macro Definition
; *************************************************************************

ISRSave MACRO				; PUSH W and STATUS
	movwf	W_TEMP			; 
	movf	STATUS,W		; (swapf will also work here)
	movwf	STATUS_TEMP		;
	ENDM

ISRRestore MACRO				; POP W and STATUS
	movf	STATUS_TEMP,W	; (swapf will also work here)
	movwf	STATUS			;
	swapf	W_TEMP,F		; movf changes STATUS bit Z, but 
	swapf	W_TEMP,W		; swapf does not change STATUS
	ENDM


	ORG		0x400		; Arbitrary location to put subroutines
						; location must be past the end of your main code!

	goto	Start
; *************************************************************************
; * SUBROUTINE  This subroutine provides a 32 bit delay loop.  
; * DoDly32     It uses the macro "Dly32" with 4 arguments to load
; *             the desired delay value.  See the application note for
; *             details.
; *
; * USAGE:      Run the "Dly32" macro which then calls this routine 
; *             automatically.
; *             You must ensure that all variables are in the same bank.
; *************************************************************************
DoDly32
        movlw   H'FF'           ; Start with -1 in W  

        addwf   Dly0,F          ; LSB decrement
        btfsc   STATUS,C        ; was the carry flag set?
        clrw		            ; if so, 0 is put in W

        addwf	Dly1,F          ; else, we continue.
        btfsc	STATUS,C        ; etc.
        clrw		            ; if so, 0 is put in W

        addwf	Dly2,F          ; 
        btfsc	STATUS,C        ; etc.
        clrw		            ; if so, 0 is put in W

        addwf	Dly3,F          ;
        btfsc	STATUS,C        ; etc.
        clrw		            ; if so, 0 is put in W

        iorwf	Dly0,W          ; * Inclusive-OR all variables
        iorwf	Dly1,W          ; * together to see if we have reached 0
        iorwf	Dly2,W          ; * on all of them.
        iorwf	Dly3,W          ; *
	
        btfss	STATUS,Z        ; Test if result of Inclusive-OR's is 0
        goto	DoDly32         ; It was NOT zero, so continue counting
        retlw	0               ; It WAS zero, so exit this subroutine.
; *************************************************************************


; ****************************************************************
; *           !! YOU NEED THESE VARIABLES (if using Dly32) !!    *
; * You may locate them where you wish                           *
; ****************************************************************
Dly0	EQU	0x20	; * Stores 4 bytes of data for the delay count
Dly1	EQU	0x21	; * Dly0 is the least significant byte
Dly2	EQU	0x22	; * while Dly3 is the most significant byte
Dly3	EQU	0x23	; *
; ****************************************************************

; ****************************************************************
; * !! YOU NEED THESE VARIABLES (if using ISRSave or ISRRestore) *
; * You may locate them where you wish                           *
; ****************************************************************
STATUS_TEMP	EQU	0x28 	; * Stores copy of STATUS when in ISR
W_TEMP		EQU	0x29 	; * Stores copy of WREG when in ISR
; ****************************************************************

⌨️ 快捷键说明

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