INCLUDE<P16F877.INC>
#INCLUDE"MAIN.INC"
#INCLUDE"LEDSN.INC"
#INCLUDE"DEL.INC"
#INCLUDE"KEYS.INC"
#INCLUDE"EEPROM.INC"
EADR EQU 31H
COUNT EQU 32H
ORG 00H
NOP
MAIN:
CALL MAINIT
MLP:
CALL DEL
BTFSS 2FH,0
GOTO MLP
CALL KEYS
CALL DISP
BCF 2FH,0
GOTO MLP
DELA
LEDNS
KEYFS
KEYSA
EPFLASH
KEYS:
MOVF KID,0
ADDWF PCL,1
KTAB:GOTO KEYEN
GOTO KEYDEL
GOTO KEYSCAN
GOTO KEYUP
GOTO KEYPROC
CLRF KID
RETLW 0
KEYPROC:
CLRF KID
MOVLW 0AH
SUBWF VAL,0
BTFSS STATUS,C
GOTO KEYNUM
ADDWF PCL,F
GOTO KEYA ;读数据并比较
GOTO KEYB ;清0
GOTO KEYC ;修改地址
GOTO KEYD ;写入新数据
GOTO KEYE ;--
GOTO KEYF ;--
CLRF VAL
RETLW 0
KEYA: ;AFUN
; MOVLW 0AH
; MOVWF DAT1
BTFSC 2FH,3
GOTO KAEXT
MOVLW DAT1
MOVWF FSR
MOVLW 6
MOVWF R0
KAL:CALL EPREAD
MOVF DAT,W
XORWF INDF,W
BNZ KAEXT
INCF ADR,F
DECFSZ R0,F
GOTO KAL
BSF 2EH,0
KAEXT:
RETLW 0
KEYB: ;BFUN
; MOVLW 0BH
; MOVWF DAT1
MOVLW 6
MOVWF R0
MOVLW DAT1
MOVWF FSR
CLRW
KBL:CLRF INDF
;MOVWF INDF,1
INCF FSR,F
DECFSZ R0,1
GOTO KBL
RETLW 0
KEYC: ;CFUN
; MOVLW 0CH
; MOVWF DAT1
BSF 2EH,1
MOVF EADR,0
MOVWF ADR
CALL EPREAD
XORLW .200
BNZ KCEXT
; CLRF EADR
MOVLW 7
ADDWF EADR,F
ADDWF ADR,F
KCEXT:
RETLW 0
KEYD:
; MOVLW 0DH
; MOVWF DAT1
BTFSS 2EH,1
GOTO KCEXT
MOVLW DAT1
MOVWF FSR
MOVLW 6
MOVWF R0
KDL:MOVF INDF,0
MOVWF DAT
CALL EPWRITE
INCF FSR,F
INCF ADR,F
DECFSZ R0,F
GOTO KDL
MOVF EADR,0
MOVWF ADR
MOVF COUNT,W
MOVWF DAT
CALL EPWRITE
INCF COUNT,F
BCF 2EH,1
RETLW 0
KEYE:
RETLW 0
KEYF:
RETLW 0
KEYNUM:
MOVLW DAT1
MOVWF FSR
MOVLW 2
MOVWF R1
MOVF VAL,0
MOVWF R0
KPL:
MOVF INDF,0
MOVWF KTMP
MOVF R0,0
MOVWF INDF
INCF FSR,1
MOVF INDF,0
MOVWF R0
MOVF KTMP,0
MOVWF INDF
INCF FSR,1
DECFSZ R1,1
GOTO KPL
KNEXT:
RETLW 0
MAINIT:
BANK1
MOVLW 0FH
MOVWF TD
CLRF TC
CLRF TB
BANK0
CLRF ADR
CLRF DAT
CLRF COUNT
MOVLW 7
MOVWF EADR
MOVLW 04H
MOVWF DAT1
MOVLW 6H
MOVWF DAT2
CLRF DAT3
CLRF DAT4
MOVLW 0FFH
MOVWF PB
MOVLW 0FFH
MOVWF PC
RETLW 0
END
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
;功能:数码管显示N位数
;初始化:POUT=PDIG=0XFF
;输入:LEDS=1 DAT1为要显示的数
; LEDS=2 DAT1,DAT2为要显示的数
; LEDS=3 DAT1,DAT2,DAT3为要显示的数
; LEDS=4或5 4<=N<=8 DAT1[N]为要显示的数
;应用:改变LEDS 的赋值可选择编译调用不同的程序
;DEFINE LEDS 1
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
;**********************************
POUT EQU PB
PDIG EQU PC
;DAT1 EQU VAL
;NBYT 为4,5子程序用
;NBYT为要显示的位数
#DEFINE NBYT 6
;LEDS 为要调用的显示程序
#DEFINE LEDS 4
;#DEFINE LEDAC 2
IF LEDS==3||4
DID EQU 29H
ENDIF
IF LEDS==5
DID EQU 29H
DIG EQU 28H
ENDIF
;************************
IF LEDS==1
LEDNS MACRO
;1BYTE LED DISPLAY
;功能:数码管显示1位数
;初始化:POUT=PDIG=0XFF
;输入:DAT1为要显示的数
;应用:在程序中声明
;DEFINE LEDS 1
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
DISP:
MOVF DAT1,0
CALL TAB
MOVWF POUT
; MOVLW 02H
; MOVWF PDIG
COMF POUT,1 ;共阴极,共阳可略
BCF PDIG,0
RETLW 0
LEDTAB
ENDM
ENDIF
;**********************
IF LEDS==2
LEDNS MACRO
;2BYTE LED DISPLAY
;功能:数码管显示2位数
;初始化:POUT=PDIG=0XFF
;输入:DAT1,DAT2为要显示的数
;应用:在程序中声明
;DEFINE LEDS 2
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
DISP:
MOVLW 0FFH
MOVWF POUT
BTFSC PDIG,1
GOTO DPS1
MOVF DAT1,0
CALL TAB
MOVWF POUT
COMF POUT,1
BCF PDIG,0
BSF PDIG,1
RETLW 0
DPS1:MOVF DAT2,0
CALL TAB
MOVWF POUT
COMF POUT,1
BSF PDIG,0
BCF PDIG,1
RETLW 0
LEDTAB
ENDM
ENDIF
;**********************
IF LEDS==3
LEDNS MACRO
;功能:数码管显示3位数
;初始化:POUT=PDIG=0XFF
; DID=0
;输入:DAT1,DAT2为要显示的数
;应用:在程序中声明
;DEFINE LEDS 3
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
DISP: ; 3-4BYTE
MOVLW 0H
MOVWF POUT
MOVF DID,0
ADDWF PCL,1
DSP:GOTO DSP1
GOTO DSP2
GOTO DSP3
CLRF DID
RETLW 0
DSP1:
MOVF DAT1,0
CALL TAB
MOVWF POUT
COMF POUT,1
MOVLW 0FEH
MOVWF PDIG
INCF DID,1
RETLW 0
DSP2:MOVF DAT2,0
CALL TAB
MOVWF POUT
COMF POUT,1
SETC
RLF PDIG,1
INCF DID,1
RETLW 0
DSP3:MOVF DAT3,0
CALL TAB
MOVWF POUT
COMF POUT,1
SETC
RLF PDIG,1
INCF DID,1
RETLW 0
LEDTAB
ENDM
ENDIF
;************************
IF LEDS==4
LEDNS MACRO
;功能:数码管显示N位数
;初始化:POUT=PDIG=0XFF
; DID=0
;输入:DAT1[N]为要显示的数
;应用:在程序中声明
;DEFINE NBYT N 4<=NBYT<=8
;DEFINE LEDS 4
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
DISP:MOVF DID,0
XORLW NBYT
BTFSS STATUS,Z
GOTO DNEX
CLRF DID
MOVLW 0FEH
MOVWF PDIG
DNEX:
MOVF DID,0
BZ DNEX1
SETC
RLF PDIG,1
DNEX1:MOVLW DAT1
MOVWF FSR
MOVF DID,0
ADDWF FSR,1
MOVF INDF,0
CALL TAB
MOVWF POUT
COMF POUT,1
INCF DID,1
RETLW 0
LEDTAB
ENDM
ENDIF
;************************
IF LEDS==5
LEDNS MACRO
;功能:数码管显示N位数,同上
;初始化:POUT=PDIG=0XFF
; DID=0 ,DIG=0XFE
;输入:DAT1[N]为要显示的数
;应用:在程序中声明
;DEFINE NBYT N N位要显示的数
;DEFINE LEDS N 5
;在主程序文件中加入LEDNS宏
;在主程序中直接调用DISP
DISP:
MOVLW 0H
MOVWF POUT
MOVLW DAT1
MOVWF FSR
MOVF DID,0
ADDWF FSR,1
DSLP:MOVF INDF,0
CALL TAB
MOVWF POUT
COMF POUT,1
MOVF DIG,0
MOVWF PDIG
SETC
RLF DIG,1
DSEXT:
INCF DID,1
MOVF DID,0
XORLW NBYT
BTFSS STATUS,Z
RETLW 0
CLRF DID
MOVLW 0FEH
MOVWF DIG
RETLW 0
LEDTAB
ENDM
ENDIF
LEDTAB MACRO
TAB:;ANDLW 0FH
ADDWF PCL
TAB1:RETLW 0XC0 ;0
RETLW 0XF9 ;
RETLW 0XA4
RETLW 0XB0
RETLW 0X99
RETLW 0X92
RETLW 0X82
RETLW 0XF8
RETLW 0X80
RETLW 0X90
RETLW 0X88
RETLW 0X83
RETLW 0XC6
RETLW 0XA1
RETLW 0X86
RETLW 0X8E ;F
RETLW 0XBF ;- 10H
RETLW 0X89 ;H 11H
RETLW 0X8F ;K
RETLW 0XC7 ;L
RETLW 0XAB ;N 14H
RETLW 0X8C ;P
RETLW 0X0
ENDM
;EPROM=2 FLASH 读写操作
;EPROM=3 EEPROM AND FLASH 读写操作
;EECON1:18CH | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0
; EEPDG - - - WRERR WREN WR RD
;EECON2:18DH BANK3
;EEADR: 10DH BANK2
;EEDATA:10CH
#DEFINE EPROM 1
IF EPROM==1
ADR EQU 70H
DAT EQU 71H
ENDIF
IF EPROM==2
ADR EQU 70H
ADRH EQU 71H
DAT EQU 72H
DATH EQU 73H
ENDIF
IF EPROM==1
EPFLASH MACRO
;查询标志位,写入数据
;向EEPROM中写入一个字节
EPWRITE:
BANK3
BTFSC EECON1,WR ;
GOTO $-1
BANK2
MOVF ADR,W
MOVWF EEADR
MOVF DAT,W
MOVWF EEDATA
BANK3
BCF EECON1,EEPGD
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 0X55
MOVWF EECON2
MOVLW 0XAA
MOVWF EECON2
BSF EECON1,WR
BCF EECON1,WREN
BSF INTCON,GIE
RETLW 0
;READ DATA EEPROM
;从EEPROM中读取一个字节
;读取数据前检测是否写入
EPREAD:
BANK3
BTFSC EECON1,WR
GOTO $-1
BANK2
CLRF DAT
MOVF ADR,W
MOVWF EEADR
BANK3
BCF EECON1,EEPGD
BSF EECON1,RD
BANK2
MOVF EEDATA,W
MOVWF DAT
RETLW 0
ENDM
ENDIF
;FLASH WRITE READ
IF EPROM==2
EPFLASH MACRO
;向FLASH中写入两个字节
FLWRITE:
BANK3
BTFSC EECON1,WR
GOTO $-1
BANK2
MOVF ADR,W
MOVWF EEADR
MOVF ADRH,W
MOVWF EEADRH
MOVF DAT,W
MOVWF EEDATA
MOVF DATH,W
MOVWF EEDATH
BANK3
BSF EECON1,EEPGD
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 0X55
MOVWF EECON2
MOVLW 0XAA
MOVWF EECON2
BSF EECON1,WR
NOP
NOP
BCF EECON1,WREN
BSF INTCON,GIE
RETLW 0
;READ DATA EEPROM
;从FLASH中读取两个字节
FLREAD:
BANK3
BTFSC EECON1,WR
GOTO $-1
BANK2
CLRF DAT
CLRF DATH
MOVF ADR,W
MOVWF EEADR
MOVF ADRH,W
MOVWF EEADRH
BANK3
BSF EECON1,EEPGD
BSF EECON1,RD
NOP
NOP
BANK2
MOVF EEDATA,W
MOVWF DAT
MOVF EEDATH,W
MOVWF DATH
RETLW 0
ENDM
ENDIF
IF EPROM==3
EPFLASH MACRO
;EEPROM 写入操作
EPWRITE:
BANK3
BTFSC EECON1,WR
GOTO $-1
BANK2
MOVF ADR,W
MOVWF EEADR
MOVF DAT1,W
MOVWF EEDATA
BANK3
BCF EECON1,EEPGD
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 0X55
MOVWF EECON2
MOVLW 0XAA
MOVWF EECON2
BSF EECON1,WR
BCF EECON1,WREN
BSF INTCON,GIE
RETLW 0
;READ DATA EEPROM
;从EEPROM中读取一个字节
EPREAD:
BANK2
MOVF ADR,W
MOVWF EEADR
BANK3
BCF EECON1,EEPGD
BSF EECON1,RD
BANK2
MOVF EEDATA,W
MOVWF DAT1
RETLW 0
;向FLASH中写入两个字节
EPWRITE:
BANK3
BTFSC EECON1,WR
GOTO $-1
BANK2
MOVF ADR,W
MOVWF EEADR
MOVF ADRH,W
MOVWF EEADRH
MOVF DAT1,W
MOVWF EEDATA
MOVF DAT2,W
MOVWF EEDATH
BANK3
BSF EECON1,EEPGD
BSF EECON1,WREN
BCF INTCON,GIE
MOVLW 0X55
MOVWF EECON2
MOVLW 0XAA
MOVWF EECON2
BSF EECON1,WR
NOP
NOP
BCF EECON1,WREN
BSF INTCON,GIE
RETLW 0
;从FLASH中读取两个字节
EPREAD:
BANK2
MOVF ADR,W
MOVWF EEADR
MOVF ADRH,W
MOVWF EEADRH
BANK3
BCF EECON1,EEPGD
BSF EECON1,RD
NOP
NOP
BANK2
MOVF EEDATA,W
MOVWF DAT1
MOVF EEDATH,W
MOVWF DAT2
RETLW 0
ENDM
ENDIF
TITLE "MAIN MACRO FILE"
;经常用到的自定义宏命令和变量
;简略指令集
;程序内部暂存器,只中当前子程序中使用
;程序退出后其值效 常作循环 暂存变量
DAT1 EQU 20H
DAT2 EQU 21H
DAT3 EQU 22H
DAT4 EQU 23H
DAT5 EQU 24H
DAT6 EQU 25H
DAT7 EQU 26H
DAT8 EQU 27H
R0 EQU 40H
R1 EQU 41H
R2 EQU 42H
R3 EQU 43H
R4 EQU 44H
R5 EQU 45H
R6 EQU 46H
R7 EQU 47H
AW EQU 48H
BW EQU 49H
CW EQU 4AH
EW EQU 4BH
;2FH SYSTEM BYTE
SYS EQU 0 ;系统标志位
TMENA EQU 1
EMEND EQU 2
;KEYEN EQU 4
;SETRP0=RP0=1 SETRP1=RP1=1
#DEFINE SETRP0 BSF STATUS,5
#DEFINE SETRP1 BSF STATUS,6
;CLRP0=RP0=0 CLRP1=RP1=0
#DEFINE CLRP0 BCF STATUS,5
#DEFINE CLRP1 BCF STATUS,6
;CLRGIE=GIE=0 SETGIE=GIE=1
#DEFINE CLRGIE BCF INTCON,7
#DEFINE SETGIE BSF INTCON,7
#DEFINE CLRPBF BCF INTCON,0
#DEFINE SETPBF BSF INTCON,0
#DEFINE CLRINTF BCF INTCON,1
#DEFINE SETINTF BSF INTCON,1
#DEFINE CLRPEIE BCF INTCON,6
#DEFINE SETPEIE BSF INTCON,6
;CLRT0F=T0IF=0 SET0F=T0IF=1
#DEFINE CLRT0F BCF INTCON,2
#DEFINE SET0F BSF INTCON,2
;
#DEFINE CLRT1F BCF PIR1,0
#DEFINE SET1F BSF PIR1,0
;
#DEFINE CLRT2F BCF PIR1,1
#DEFINE SET2F BSF PIR1,1
#DEFINE CLRCP1F BCF PIR1,2
#DEFINE SETCP1F BSF PIR1,2
#DEFINE CLRSPF BCF PIR1,3
#DEFINE SETSPF BSF PIR1,3
#DEFINE CLRTXF BCF PIR1,4
#DEFINE SETTXF BSF PIR1,4
#DEFINE CLRRXF BCF PIR1,5
#DEFINE SETRXT BSF PIR1,5
#DEFINE CLRADF BCF PIR1,6
#DEFINE SETADF BSF PIR1,6
#DEFINE CLRPSPF BCF PIR1,7
#DEFINE SETPSPF BSF PIR1,7
#DEFINE CLRCP2F BCF PIR2,0
#DEFINE SETCP2F BSF PIR2,0
#DEFINE CLRBCF BCF PIR2,3
#DEFINE SETBCF BSF PIR2,3
;
#DEFINE T1ON BSF T1CON,0
#DEFINE T1OFF BCF T1CON,0
#DEFINE T2ON BSF T2CON,2
#DEFINE T2OFF BCF T2CON,2
#DEFINE EPRD BSF EECON1,0
#DEFINE EPNRD BCF EECON1,0
#DEFINE EPWR BSF EECON1,1
#DEFINE EPNWR BCF EECON1,1
#DEFINE EPWEN BSF EECON1,2
#DEFINE EPWNE BCF EECON1,2
#DEFINE EEPOM BCF EECON1,7
#DEFINE FLASH BSF EECON1,7
#DEFINE PA PORTA
#DEFINE PB PORTB
#DEFINE PC PORTC
#DEFINE PD PORTD
#DEFINE PE PORTE
#DEFINE CLRPA CLRF PA
#DEFINE CLRPB CLRF PB
#DEFINE CLRPC CLRF PC
#DEFINE CLRPD CLRF PD
#DEFINE CLRPE CLRF PE
#DEFINE TA TRISA
#DEFINE TB TRISB
#DEFINE TC TRISC
#DEFINE TD TRISD
#DEFINE TE TRISE
#DEFINE CLRTA CLRF TA
#DEFINE CLRTB CLRF TB
#DEFINE CLRTC CLRF TC
#DEFINE CLRTD CLRF TD
#DEFINE CLRTE CLRF TE
BANK0 MACRO
BCF STATUS,RP0
BCF STATUS,RP1
ENDM
BANK1 MACRO
BSF STATUS,RP0
BCF STATUS,RP1
ENDM
BANK2 MACRO
BCF STATUS,RP0
BSF STATUS,RP1
ENDM
BANK3 MACRO
BSF STATUS,RP0
BSF STATUS,RP1
ENDM
一周热门 更多>