?? mplxkey.lst
字號:
MPASM 01.40 Released MPLXKEY.ASM 1-16-1997 16:24:40 PAGE 1
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00001 ;*********************************************************************
00002 ;This program is to demonstrate how to multiplex four 7 segment LED
00003 ;digits and a 4X4 keypad using a PIC16C71.
00004 ;The four digits will start as '0000' and when a key is hit
00005 ;it is displayed on the 7 segment leds as a hex value 0 to F. The last
00006 ;digit hit is always displayed on the right most led with the rest of
00007 ;the digits shifted to the left. The left most digit is deleted.
00008 ;The LEDs are updated every 20mS, the keypad is scanned at a rate of 20 mS.
00009 ;The RTCC timer is used in internal interrupt mode to generate the
00010 ;5 mS.
00011 ;
00012 ; Stan D'Souza 5/8/93
00013 ;
00014 ; Program: MPLXKEY.ASM
00015 ; Revision Date:
00016 ; 1-15-97 Compatibility with MPASMWIN 1.40
00017 ;
00018 ;**********************************************************************
00019 LIST P=16C71
00020 ERRORLEVEL -302
00021 ;
00022 include <p16c71.inc>
00001 LIST
00002 ; P16C71.INC Standard Header File, Version 1.00 Microchip Technology, Inc.
00142 LIST
00023 ;
0000000C 00024 TempC equ 0x0c ;temp general purpose regs
0000000D 00025 TempD equ 0x0d
0000000E 00026 TempE equ 0x0e
00000020 00027 PABuf equ 0x20
00000021 00028 PBBuf equ 0x21
0000000F 00029 Count equ 0x0f ;count
00000010 00030 MsdTime equ 0x10 ;most significant Timer
00000011 00031 LsdTime equ 0x11 ;Least significant Timer
00000012 00032 KeyFlag equ 0x12 ;flags related to key pad
00000000 00033 keyhit equ 0 ;bit 0 --> key-press on
00000001 00034 DebnceOn equ 1 ;bit 1 --> debounce on
00000002 00035 noentry equ 2 ;no key entry = 0
00000003 00036 ServKey equ 3 ;bit 3 --> service key
00000013 00037 Debnce equ 0x13 ;debounce counter
00000014 00038 NewKey equ 0x14
0000002F 00039 WBuffer equ 0x2f
0000002E 00040 StatBuffer equ 0x2e
00000001 00041 OptionReg equ 1
00000002 00042 PCL equ 2
00043 ;
00044 ;
00045 push macro
00046 movwf WBuffer ;save w reg in Buffer
00047 swapf WBuffer, F ;swap it
00048 swapf STATUS,W ;get status
00049 movwf StatBuffer ;save it
00050 endm
MPASM 01.40 Released MPLXKEY.ASM 1-16-1997 16:24:40 PAGE 2
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00051 ;
00052 pop macro
00053 swapf StatBuffer,W ;restore status
00054 movwf STATUS ; /
00055 swapf WBuffer,W ;restore W reg
00056 endm
00057 ;
0000 00058 org 0
0000 280D 00059 goto Start ;skip over interrupt vector
00060 ;
0004 00061 org 4
00062 ;It is always a good practice to save and restore the w reg,
00063 ;and the status reg during a interrupt.
00064 push
0004 00AF M movwf WBuffer ;save w reg in Buffer
0005 0EAF M swapf WBuffer, F ;swap it
0006 0E03 M swapf STATUS,W ;get status
0007 00AE M movwf StatBuffer ;save it
0008 2036 00065 call ServiceInterrupts
00066 pop
0009 0E2E M swapf StatBuffer,W ;restore status
000A 0083 M movwf STATUS ; /
000B 0E2F M swapf WBuffer,W ;restore W reg
000C 0009 00067 retfie
00068 ;
000D 00069 Start
000D 2020 00070 call InitPorts
000E 202A 00071 call InitTimers
000F 00072 loop
000F 1992 00073 btfsc KeyFlag,ServKey ;key service pending
0010 2012 00074 call ServiceKey ;yes then service
0011 280F 00075 goto loop
00076 ;
00077 ;ServiceKey, does the software service for a keyhit. After a key service,
00078 ;the ServKey flag is reset, to denote a completed operation.
0012 00079 ServiceKey
0012 0814 00080 movf NewKey,W ;get key value
0013 008E 00081 movwf TempE ;save in TempE
0014 0E10 00082 swapf MsdTime,W ;move MSD out
0015 39F0 00083 andlw B'11110000' ;clr lo nibble
0016 0090 00084 movwf MsdTime ;save back
0017 0E11 00085 swapf LsdTime,W ;get Lsd
0018 390F 00086 andlw B'00001111' ;mask off lsd
0019 0490 00087 iorwf MsdTime, F ;and left shift 3rd
001A 0E11 00088 swapf LsdTime,W ;get Lsd again
001B 39F0 00089 andlw B'11110000' ;mask off 2nd
001C 040E 00090 iorwf TempE,W ;or with new lsd
001D 0091 00091 movwf LsdTime ;make Lsd
001E 1192 00092 bcf KeyFlag,ServKey ;reset service flag
001F 0008 00093 return
00094
00095 ;
0020 00096 InitPorts
MPASM 01.40 Released MPLXKEY.ASM 1-16-1997 16:24:40 PAGE 3
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
0020 1683 00097 bsf STATUS,RP0 ;select pg 1
0021 3003 00098 movlw 3 ;make RA0-3 digital I/O
0022 0088 00099 movwf ADCON1 ; /
0023 0185 00100 clrf TRISA ;make RA0-4 outputs
0024 0186 00101 clrf TRISB ;make RB0-7 outputs
0025 1283 00102 bcf STATUS,RP0 ;select page 0
0026 0185 00103 clrf PORTA ;make all outputs low
0027 0186 00104 clrf PORTB ; /
0028 1585 00105 bsf PORTA,3 ;enable MSB digit sink
0029 0008 00106 return
00107 ;
00108 ;
00109 ;The clock speed is 4.096Mhz. Dividing internal clk. by a 32 prescaler,
00110 ;the rtcc will be incremented every 31.25uS. If rtcc is preloaded
00111 ;with 96, it will take (256-96)*31.25uS to overflow i.e. 5mS. So the
00112 ;end result is that we get a rtcc interrupt every 5mS.
002A 00113 InitTimers
002A 0190 00114 clrf MsdTime ;clr timers
002B 0191 00115 clrf LsdTime ; /
002C 0192 00116 clrf KeyFlag ;clr all flags
002D 1683 00117 bsf STATUS,RP0 ;select pg 1
002E 3084 00118 movlw B'10000100' ;assign ps to rtcc
002F 0081 00119 movwf OptionReg ;ps = 32
0030 1283 00120 bcf STATUS,RP0 ;select pg 0
0031 3020 00121 movlw B'00100000' ;enable rtcc interrupt
0032 008B 00122 movwf INTCON ;
0033 3060 00123 movlw .96 ;preload rtcc
0034 0081 00124 movwf TMR0 ;start counter
0035 0009 00125 retfie
00126 ;
0036 00127 ServiceInterrupts
0036 190B 00128 btfsc INTCON,T0IF ;rtcc interrupt?
0037 283B 00129 goto ServiceTMR0 ;yes then service
0038 018B 00130 clrf INTCON ;else clr all int
0039 168B 00131 bsf INTCON,T0IE
003A 0008 00132 return
00133 ;
003B 00134 ServiceTMR0
003B 3060 00135 movlw .96 ;initialize rtcc
003C 0081 00136 movwf TMR0
003D 110B 00137 bcf INTCON,T0IF ;clr int flag
003E 1805 00138 btfsc PORTA,0 ;if msb on then do
003F 2042 00139 call ScanKeys ;do a quick key scan
0040 20A1 00140 call UpdateDisplay ;update display
0041 0008 00141 return
00142 ;
00143 ;
00144 ;ScanKeys, scans the 4X4 keypad matrix and returns a key value in
00145 ;NewKey (0 - F) if a key is pressed, if not it clears the keyhit flag.
00146 ;Debounce for a given keyhit is also taken care of.
00147 ;The rate of key scan is 20mS with a 4.096Mhz clock.
0042 00148 ScanKeys
0042 1C92 00149 btfss KeyFlag,DebnceOn ;debounce on?
MPASM 01.40 Released MPLXKEY.ASM 1-16-1997 16:24:40 PAGE 4
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
0043 2848 00150 goto Scan1 ;no then scan keypad
0044 0B93 00151 decfsz Debnce, F ;else dec debounce time
0045 0008 00152 return ;not over then return
0046 1092 00153 bcf KeyFlag,DebnceOn ;over, clr debounce flag
0047 0008 00154 return ;and return
0048 00155 Scan1
0048 208A 00156 call SavePorts ;save port values
0049 30EF 00157 movlw B'11101111' ;init TempD
004A 008D 00158 movwf TempD
004B 00159 ScanNext
004B 0806 00160 movf PORTB,W ;read to init port
004C 100B 00161 bcf INTCON,RBIF ;clr flag
004D 0C8D 00162 rrf TempD, F ;get correct column
004E 1C03 00163 btfss STATUS,C ;if carry set?
004F 2862 00164 goto NoKey ;no then end
0050 080D 00165 movf TempD,W ;else output
0051 0086 00166 movwf PORTB ;low column scan line
0052 0000 00167 nop
0053 1C0B 00168 btfss INTCON,RBIF ;flag set?
0054 284B 00169 goto ScanNext ;no then next
0055 1812 00170 btfsc KeyFlag,keyhit ;last key released?
0056 2860 00171 goto SKreturn ;no then exit
0057 1412 00172 bsf KeyFlag,keyhit ;set new key hit
0058 0E06 00173 swapf PORTB,W ;read port
0059 008E 00174 movwf TempE ;save in TempE
005A 2064 00175 call GetKeyValue ;get key value 0 - F
005B 0094 00176 movwf NewKey ;save as New key
005C 1592 00177 bsf KeyFlag,ServKey ;set service flag
005D 1492 00178 bsf KeyFlag,DebnceOn ;set flag
005E 3004 00179 movlw 4
005F 0093 00180 movwf Debnce ;load debounce time
0060 00181 SKreturn
0060 2097 00182 call RestorePorts ;restore ports
0061 0008 00183 return
00184 ;
0062 00185 NoKey
0062 1012 00186 bcf KeyFlag,keyhit ;clr flag
0063 2860 00187 goto SKreturn
00188 ;
00189 ;GetKeyValue gets the key as per the following layout
00190 ;
00191 ; Col1 Col2 Col3 Col3
00192 ; (RB3) (RB2) (RB1) (RB0)
00193 ;
00194 ;Row1(RB4) 0 1 2 3
00195 ;
00196 ;Row2(RB5) 4 5 6 7
00197 ;
00198 ;Row3(RB6) 8 9 A B
00199 ;
00200 ;Row4(RB7) C D E F
00201 ;
0064 00202 GetKeyValue
MPASM 01.40 Released MPLXKEY.ASM 1-16-1997 16:24:40 PAGE 5
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
0064 018C 00203 clrf TempC
0065 1D8D 00204 btfss TempD,3 ;first column
0066 286E 00205 goto RowValEnd
0067 0A8C 00206 incf TempC, F
0068 1D0D 00207 btfss TempD,2 ;second col.
0069 286E 00208 goto RowValEnd
006A 0A8C 00209 incf TempC, F
006B 1C8D 00210 btfss TempD,1 ;3rd col.
006C 286E 00211 goto RowValEnd
006D 0A8C 00212 incf TempC, F ;last col.
006E 00213 RowValEnd
006E 1C0E 00214 btfss TempE,0 ;top row?
006F 2878 00215 goto GetValCom ;yes then get 0,1,2&3
0070 1C8E 00216 btfss TempE,1 ;2nd row?
0071 2877 00217 goto Get4567 ;yes the get 4,5,6&7
0072 1D0E 00218 btfss TempE,2 ;3rd row?
0073 2875 00219 goto Get89ab ;yes then get 8,9,a&b
0074 00220 Getcdef
0074 150C 00221 bsf TempC,2 ;set msb bits
0075 00222 Get89ab
0075 158C 00223 bsf TempC,3 ; /
0076 2878 00224 goto GetValCom ;do common part
0077 00225 Get4567
0077 150C 00226 bsf TempC,2
0078 00227 GetValCom
0078 080C 00228 movf TempC,W
0079 0782 00229 addwf PCL, F
007A 3400 00230 retlw 0
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -