📄 lab5.lst
字号:
MPASM 5.06 LAB5.ASM 5-28-2007 15:27:37 PAGE 1
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00001 #INCLUDE p16F877A.inc ; Include the standard definitions
00001 LIST
00002 ; P16F877A.INC Standard Header File, Version 1.00 Microchip Technology, Inc.
00400 LIST
00002 #INCLUDE 701PIC.inc ; Include MACROs from MASTER抯 03, PIC 102 class
00001 ; please note, this file is not finshed (comments, etc),
00002 ; but the code does work.
00003
00004
00005 LEDEnable MACRO BitPattern
00006 BANKSEL TRISB
00007 movlw BitPattern
00008 movwf TRISB
00009 BANKSEL PORTB
00010 ENDM
00011
00012 LEDOn MACRO LEDNumber
00013 movlw 1 << LEDNumber
00014 iorwf PORTB,F
00015 ENDM
00016
00017 LEDOff MACRO LEDNumber
00018 movlw H'FF' - ((1 << LEDNumber) & H'00FF')
00019 andwf PORTB,F
00020 ENDM
00021
00022 Wait4Ever MACRO
00023 goto $
00024 ENDM
00025
00026 ; Sets Piezo to beep at desired frequency, must declare
00027 ; Frequency and #DEFINE "DEVICE_FREQ_HZ". To control beep,
00028 ; use "PiezoOn" and "PiezoOff" macros
00029 PiezoEnable MACRO Freq
00030 BANKSEL TRISC
00031 bcf TRISC,2
00032 movlw ((DEVICE_FREQ_HZ/D'64')/Freq)-1
00033 movwf PR2
00034 movlw ((DEVICE_FREQ_HZ/D'128')/Freq)
00035 BANKSEL CCPR1L
00036 movwf CCPR1L
00037 movlw 0x06 ; Turn on TMR2 (for PWM)
00038 movwf T2CON ; and /16 prescale
00039 ENDM
00040
00041 PiezoOn MACRO
00042 movlw H'0C'
00043 movwf CCP1CON
00044 ENDM
00045
00046 PiezoOff MACRO
00047 clrf CCP1CON
00048 ENDM
MPASM 5.06 LAB5.ASM 5-28-2007 15:27:37 PAGE 2
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00049
00050
00051 ; *************************************************************************
00052 ; * MACRO This Macro loads literal values into the variables Dly3
00053 ; * Dly32 through Dly0 and then calls DoDly32 to run the delay.
00054 ; * This is the macro DEFINITION. The code is pasted in
00055 ; * place of the macro call later in the code.
00056 ; * Be sure to ensure all variables are in the same bank.
00057 ; *
00058 ; * USAGE (for 4 MHz clock):
00059 ; * Dly32 D'2' ; * 2 loops x 40 us = 40 us
00060 ; * Dly32 D'5000' ; * 5000 loops x 20 us = 100 ms
00061 ; * Dly32 D'50000' ; * 50000 loops x 20 us = 1 s
00062 ; * Dly32 D'50000'*D'60' ; * 60 x 1 second loops = 1 min
00063 ; * Dly32 D'45000000' ; * 4.5 million loops x 20 us = 90 s
00064 ; *************************************************************************
00065 Dly32 MACRO DLY
00066
00067 BANKSEL Dly3
00068 movlw (DLY-1) & H'FF' ; * Take the delay value argument
00069 movwf Dly0 ; * from the macro, precalculate
00070 movlw (DLY-1) >>D'08' & H'FF' ; * the required 4 RAM values and
00071 movwf Dly1 ; * load the The RAM values Dly3
00072 movlw (DLY-1) >>D'16' & H'FF' ; * though Dly0.
00073 movwf Dly2 ; * Bytes are shifted and anded
00074 movlw (DLY-1) >>D'24' & H'FF' ; * by the assembler to make
00075 movwf Dly3 ; * user calculations easier
00076
00077 nop ; 7 cycle delay for precision delay value
00078 nop ;
00079 nop ;
00080 nop ;
00081 nop ;
00082 nop ;
00083 nop ;
00084
00085 call DoDly32 ; Call DoDly32 to run the delay loop.
00086 ENDM ; End of the Macro Definition
00087 ; *************************************************************************
00088
00089 ISRSave MACRO ; PUSH W and STATUS
00090 movwf W_TEMP ;
00091 movf STATUS,W ; (swapf will also work here)
00092 movwf STATUS_TEMP ;
00093 ENDM
00094
00095 ISRRestore MACRO ; POP W and STATUS
00096 movf STATUS_TEMP,W ; (swapf will also work here)
00097 movwf STATUS ;
00098 swapf W_TEMP,F ; movf changes STATUS bit Z, but
00099 swapf W_TEMP,W ; swapf does not change STATUS
00100 ENDM
00101
MPASM 5.06 LAB5.ASM 5-28-2007 15:27:37 PAGE 3
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00102
0400 00103 ORG 0x400 ; Arbitrary location to put subroutines
00104 ; location must be past the end of your main code!
00105
0400 2800 00106 goto Start
00107 ; *************************************************************************
00108 ; * SUBROUTINE This subroutine provides a 32 bit delay loop.
00109 ; * DoDly32 It uses the macro "Dly32" with 4 arguments to load
00110 ; * the desired delay value. See the application note for
00111 ; * details.
00112 ; *
00113 ; * USAGE: Run the "Dly32" macro which then calls this routine
00114 ; * automatically.
00115 ; * You must ensure that all variables are in the same bank.
00116 ; *************************************************************************
0401 00117 DoDly32
0401 30FF 00118 movlw H'FF' ; Start with -1 in W
00119
0402 07A0 00120 addwf Dly0,F ; LSB decrement
0403 1803 00121 btfsc STATUS,C ; was the carry flag set?
0404 0103 00122 clrw ; if so, 0 is put in W
00123
0405 07A1 00124 addwf Dly1,F ; else, we continue.
0406 1803 00125 btfsc STATUS,C ; etc.
0407 0103 00126 clrw ; if so, 0 is put in W
00127
0408 07A2 00128 addwf Dly2,F ;
0409 1803 00129 btfsc STATUS,C ; etc.
040A 0103 00130 clrw ; if so, 0 is put in W
00131
040B 07A3 00132 addwf Dly3,F ;
040C 1803 00133 btfsc STATUS,C ; etc.
040D 0103 00134 clrw ; if so, 0 is put in W
00135
040E 0420 00136 iorwf Dly0,W ; * Inclusive-OR all variables
040F 0421 00137 iorwf Dly1,W ; * together to see if we have reached 0
0410 0422 00138 iorwf Dly2,W ; * on all of them.
0411 0423 00139 iorwf Dly3,W ; *
00140
0412 1D03 00141 btfss STATUS,Z ; Test if result of Inclusive-OR's is 0
0413 2C01 00142 goto DoDly32 ; It was NOT zero, so continue counting
0414 3400 00143 retlw 0 ; It WAS zero, so exit this subroutine.
00144 ; *************************************************************************
00145
00146
00147 ; ****************************************************************
00148 ; * !! YOU NEED THESE VARIABLES (if using Dly32) !! *
00149 ; * You may locate them where you wish *
00150 ; ****************************************************************
00000020 00151 Dly0 EQU 0x20 ; * Stores 4 bytes of data for the delay count
00000021 00152 Dly1 EQU 0x21 ; * Dly0 is the least significant byte
00000022 00153 Dly2 EQU 0x22 ; * while Dly3 is the most significant byte
00000023 00154 Dly3 EQU 0x23 ; *
MPASM 5.06 LAB5.ASM 5-28-2007 15:27:37 PAGE 4
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00155 ; ****************************************************************
00156
00157 ; ****************************************************************
00158 ; * !! YOU NEED THESE VARIABLES (if using ISRSave or ISRRestore) *
00159 ; * You may locate them where you wish *
00160 ; ****************************************************************
00000028 00161 STATUS_TEMP EQU 0x28 ; * Stores copy of STATUS when in ISR
00000029 00162 W_TEMP EQU 0x29 ; * Stores copy of WREG when in ISR
00163 ; ****************************************************************
2007 3F39 00003 __CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _LVP_OFF & _BODEN_OFF
00004
00000030 00005 ISRCtr EQU 0x30 ; location for ISRCtr variable (countdown for ISR loops)
0000007F 00006 Cycle4 EQU 0x7F
0000003D 00007 TMR0Val EQU D'61'
00008
0000 00009 ORG 0 ; Reset Vector Location (0)
0000 00010 Start
0000 281A 00011 goto Initialize ; On start-up go to Initialization code
00012
0004 00013 ORG 4 ; Interrupt Vector Location (4)
0004 00014 ISR
0004 1D0B 00015 BTFSS INTCON,TMR0IF
0005 0009 00016 RETFIE
00017 ISRSave
0006 00A9 M movwf W_TEMP ;
0007 0803 M movf STATUS,W ; (swapf will also work here)
0008 00A8 M movwf STATUS_TEMP ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -