📄 密码锁.asm
字号:
LOOP7: MOV R7,#0FAH
LOOP8: DJNZ R7,LOOP8
DJNZ R6,LOOP7
RET
;*********************************************************
; Delay 100ms
;********************************************************
DEL100MS:MOV R6,#64H
LOOP3: MOV R7,#0FAH
LOOP4: DJNZ R7,LOOP4
DJNZ R6,LOOP3
RET
;*********************************************************
; Delay 260ms
;*********************************************************
DEL260MS:MOV R6,#0FFH
LOOP5: MOV R7,#0FFH
LOOP6: DJNZ R7,LOOP6
DJNZ R6,LOOP5
RET
;*********************************************************
; the following programme is used to write or read at24c02
;*********************************************************
;The CPU is 6M ,the programm is designed for eeprom 24c01 or 24c02.
;I have set the error flag ,when the eeprom is wrong ,set the flag!
;when you read and write the eeprom,please chech the flag and you could
;do next thing!!!
error_flag BIT 00h
BEEP BIT P3.5
R_LED BIT P3.7
SDA BIT P3.2
SCL BIT P3.1
check_counter equ 1fh ;check ack loop counter,you could use other register!
DEV_add_R equ 10100001b ;The device's address + read(1)
DEV_add_W EQU 10100000B ;The device's address +write(0)
;***********************************************************************
;******************I2C START**************************
S_START: SETB SDA
SETB SCL
nop
nop
nop
nop
nop
nop
CLR SDA
nop
nop
nop
nop
nop
nop
CLR SCL
NOP
RET
;****************I2C STOP********************************
S_STOP: CLR SDA
SETB SCL
nop
nop
nop
nop
nop
nop
SETB SDA
nop
nop
nop
nop
nop
nop
CLR SCL
NOP
RET
;******************Send the ACK****************************
T_ACK: CLR SDA
SETB SCL
nop
nop
nop
nop
nop
nop
CLR SCL
SETB SDA
RET
;******************Check the ACK****************************
CHACK: SETB SDA
SETB SCL
nop
nop
nop
nop
nop
nop
MOV C,SDA
MOV F0,C
CLR SCL
NOP
NOP
RET
;*****************send a single byte*************************
WRBYT: MOV R7,#8
WRBYT1: RLC A
JC WRBYT2
CLR SDA
SETB SCL
nop
nop
nop
nop
nop
nop
CLR SCL
DJNZ R7,WRBYT1
RET
WRBYT2: SETB SDA
SETB SCL
nop
nop
nop
nop
nop
nop
CLR SCL
CLR SDA
DJNZ R7, WRBYT1
RET
;***********************Receive a single byte************************
RDBYT: MOV R7,#8
RDBYT1:SETB SDA
SETB SCL
nop
nop
nop
nop
nop
nop
MOV C,SDA
MOV A,R6
RLC A
MOV R6,A
CLR SCL
DJNZ R7,RDBYT1
RET
;******************Write a byte to eeprom(24c02)********************
;(R1)=24C02'S address (R0)=RAM address
WR_ONEBYT: LCALL S_START
MOV check_counter,#200 ;ACK COUNTER
LOOP100: MOV A ,#DEV_add_W
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR100
DJNZ check_counter,LOOP100
LCALL ERROR ;IF the ACK is wrong,return
RET
ADDR100: MOV check_counter,#200
LOOP200: MOV A,R1 ;SEND the eeprom's address
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR200
DJNZ check_counter,LOOP200
LCALL ERROR
RET
ADDR200: MOV check_counter,#200
LOOP300: MOV A,@R0 ;GET the data
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR300
DJNZ check_counter,LOOP300
LCALL ERROR ;if the ACK is wrong ,return
RET
ADDR300: LCALL S_STOP
RET
;************************write n bytes to eeprom***************
;(R1)=eeprom's address (R0)=RAM 's address
;(R2)=n
;**************************************************************
WR_BYTES:LCALL S_START
MOV check_counter,#200 ;counter
A100: MOV A,#DEV_add_W ;Wait to write
LCALL WRBYT
LCALL CHACK
JNB F0,B100
DJNZ check_counter,A100
LCALL ERROR
RET
B100: MOV check_counter,#200
A200: MOV A,R1 ;EEPROM'S ADDRESS
LCALL WRBYT
LCALL CHACK
JNB F0,B200
DJNZ check_counter,A200
LCALL ERROR
RET
B200: MOV check_counter,#200
A300: MOV A,@R0 ;GET the data
LCALL WRBYT
LCALL CHACK
JNB F0,B300
DJNZ check_counter,A300
LCALL ERROR
RET
B300: DJNZ R2,WGO_ON ;IF the data sending is over,return
LCALL S_STOP
RET
WGO_ON: INC R0 ;get the next data
LJMP B200
;**********************Error process**************************
ERROR: setb error_flag
ret
;************************************************************
ERROR_PRO:
clr R_LED ;at24c02 error! and open the red led !!!
ajmp $ ;wait to process
;********************delay 20ms*****************************
DEL20MS:
MOV R6,#50
D_LOOP3: MOV R7,#100
D_LOOP4: DJNZ R7,D_LOOP4
DJNZ R6,D_LOOP3
RET
;***********************Read a byte from eeprom************
;(R1)=eeprom's address
;(R0)=RAM 's address
RD_ONEBYT: LCALL S_START
MOV check_counter,#200
LOOP400: MOV A,#DEV_add_W
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR400
DJNZ check_counter,LOOP400
LCALL ERROR
RET
ADDR400: MOV check_counter,#200
LOOP500: MOV A,R1 ;EEPROM'S address
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR500
DJNZ check_counter,LOOP500
LCALL ERROR
RET
ADDR500: LCALL S_START
MOV check_counter,#200
LOOP600: MOV A,#DEV_add_R
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR600
DJNZ check_counter,LOOP600
LCALL ERROR
RET
ADDR600: LCALL RDBYT
LCALL T_NOACK
LCALL S_STOP
MOV @R0,A ;STORE THE DATA
RET
;********************Read n bytes from eeprom**********************
;(R1)=eeprom's address
;(R0)=RAM's address
;(R2)=n
;******************************************************************
RD_BYTES: LCALL S_START
MOV check_counter,#200
LOOP700: MOV A,#DEV_add_W
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR700
DJNZ check_counter,LOOP700
LCALL ERROR
RET
ADDR700: MOV check_counter,#200
LOOP800: MOV A,R1 ;eeprom's address
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR800
DJNZ check_counter,LOOP800
LCALL ERROR
RET
ADDR800: MOV check_counter,#200
LCALL S_START
LOOP900: MOV A,#DEV_add_R
LCALL WRBYT
LCALL CHACK
JNB F0,ADDR900
DJNZ check_counter,LOOP900
LCALL ERROR
RET
ADDR900: LCALL RDBYT
MOV @R0,A ;STORE THE DATA
INC R0
DJNZ R2,GO_ON
LJMP R_OVER ;complete receive the data
GO_ON: LCALL T_ACK
SJMP ADDR900
R_OVER: LCALL T_NOACK ;send the noack signal
LCALL S_STOP
RET
;*********************send the NOACK signal************************
T_NOACK: SETB SDA
NOP
SETB SCL
nop
nop
nop
nop
nop
nop
CLR SCL
CLR SDA
NOP
RET
;******************************************************************
; the following programme is used to check if 60h~6fh and 70h~7fh
; is 0ffh ,if is ,and set the flag,if not clr the flag!
;******************************************************************
data_0ffh_check:
CLR data_full_flag
MOV R0,#60H
MOV R6,#8
GO_ON_CHECK:
CJNE @R0,#0FFH,T10
INC R0
DJNZ R6,GO_ON_CHECK
;**************************************************
; 70H~7FH check
;*************************************************
MOV R0,#70H
MOV R6,#8
GO_ON_CHECK_1:
CJNE @R0,#0FFH,T10
INC R0
DJNZ R6,GO_ON_CHECK_1
SETB data_full_FLAG
T10: RET
;****************************************************
; save the password to eeprom
; write eeprom from RAM data with address 70h~7fh
; the password is 8 bits!!!
;***************************************************
save_pwd:
push psw
setb psw.4
clr psw.3
mov r0,#70h
mov r1,#08h
mov r2,#8
lcall wr_bytes
jb error_flag,k6
ljmp k7
k6: ljmp error_pro
k7: lcall del260ms ;wait the at24c02 completed writing!!!***************add on 2004 7.9 12:52
mov r0,#70h
mov r1,#0f0h
mov r2,#8
lcall wr_bytes
pop psw
jb error_flag,k20
ljmp k21
k20: ljmp error_pro
k21:
;write sucessfully!
LCALL DEL260MS ;password save successfully!!! and this is used to wait write completed!!!
CLR P3.5
LCALL DEL100MS
SETB P3.5
LCALL DEL100MS
CLR P3.5
LCALL DEL100MS
SETB P3.5
LCALL DEL100MS
CLR P3.5
LCALL DEL100MS
SETB P3.5
LCALL DEL100MS
ret
;*********************************************************
; this progamme compare 60h~6fh data and 70h~7fh data
;if equal ,set the flag,if not,clr the flag
;*********************************************************
data_compare:
CLR equal_flag ;clr the flag!
mov r6,#8
MOV R0,#60H
MOV R1,#70H
ADDR2000:
MOV A,@R0
MOV 50H,@R1
CJNE A,50H,ADDR2100
INC R0
INC R1
DJNZ R6,ADDR2000
setb equal_flag ;equal,and set the flag!!!
ADDR2100:NOP
RET
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -