📄 sl4442.inc
字号:
;IC卡位定义
ICin BIT P1.4 ;IC卡插入连锁端口
VCC1 BIT P2.0 ;IC卡电源控制端口
RST BIT P1.1 ;IC卡复位端
SD BIT P1.2 ;IC卡数据线
CLK BIT P1.0 ;IC卡时钟线
ICN BIT P0.7 ;IC卡短路检测端口
;IC卡常数
Idetify1 EQU 0A2H ;此处为IC卡保护区00~03单元的值,用于识别卡
Idetify2 EQU 13H
Idetify3 EQU 10H
Idetify4 EQU 91H
;*****************************
checkic:
;写IC卡之前先校验密码
clr vcc1
acall delay10us
acall CheckPassword ;密码校验
ret
;*****************************************************************************
; SLE4442 通用读写模块使用说明 *
;ReadCard 读出从SubAdr开始的ByteCnt字节,结果存放于(RcvDat)中,读出正确,ACC中返*
;回#0,返回#01表示无效卡或者卡损坏,(注读函数已经对数据进行了校验) *
;WriteCard从SubAdr开始写入ByteCNT字节,待写入数据存放于XmtDat中,写入正确ACC中 *
;返回#0,返回#01表示无效卡或者卡损坏.(注.写函数已经把校验数自动写入了相应的位置
;写卡之前一定要调用CheckPassword核对密码,*
;密码存放于PSW开始的三个单元,ACC中返回#0表示核对正确,#01表示无效卡或者卡损坏,*
;#02表示密码错.核对密码完成后,EC;开始的4个单元返回错误计数值及密码. *
;*****************************************************************************
ReadCard:
PUSH 05H
PUSH 02H
PUSH 00H
ACALL ANRST
JNZ ReadExit
MOV R5,SubAdr
MOV R0,#ReadBuf
MOV R2,#16
ACALL Remm
MOV R0,#ReadBuf
mov R2,#14
MOV A,@R0
checksum:
INC R0
XRL A,@R0
DJNZ R2,checksum
INC R0
CLR C
SUBB A,@R0
ReadExit:
POP 00H
POP 02H
POP 05H
RET
;*********************************************
WriteCard:
PUSH 06H
PUSH 05H
PUSH 02H
PUSH 00H
ACALL ANRST
JNZ WriteExit
MOV R5,SubAdr
MOV R0,#WriteBuf
MOV R2,#14
MOV A,@R0
WriteCheck:
INC R0
XRL A,@R0
DJNZ R2,WriteCheck
INC R0
MOV @R0,A
MOV R2,#16
MOV R0,#WriteBuf
WriteLoop:
MOV A,@R0
MOV R6,A
ACALL Wrmm
INC R0
INC R5
DJNZ R2,WriteLoop
MOV A,#00H
WriteExit:
POP 00H
POP 02H
POP 05H
POP 06H
RET
;*********************************
CheckPassword:
ACALL ANRST
JNZ CheckExit
Acall Rescm ;read psw
MOV A,ReadBuf
anl a,#07h
jz IS_FAIL
rrc a
jnc CheckPassword1
MOV A,ReadBuf
anl a,#06h ;write one "0" in EC
jz IS_FAIL ;The card has locked twice
sjmp CheckPassworda
CheckPassword1:
rrc a
jnc IS_FAIL
MOV A,ReadBuf
anl a,#05h
jz IS_FAIL
CheckPassworda:
MOV R5,#00H
mov R6,A ;write EC back to card;DATA IN ACC
Acall Wrscm
Lcall ReadPsw ;get the passedword in memorry
mov r2,#3 ;set compare verificatSDn data counter
MOV r5,#01H
mov r0,#ReadBuf ;get verificatSDn data start add pointer
CheckPasswordb:
mov a,@r0
MOV R6,A
Acall Verda ;compare verificatSDn data byte by byte
inc r0 ;increase add pointer
inc r5 ;increase scm add pointer
djnz r2,CheckPasswordb
mov r5,#00h ;erase EC
mov R6,#0ffh
Acall Wrscm
Acall Rescm ;read EC to insure comparing is successed
mov A,ReadBuf
ANL A,#07H
cjne a,#07H,IS_FAIL
MOV A,#00H ;Success
CheckExit:
RET
IS_FAIL:
MOV A,#02H
AJMP CheckExit
;*********************************
;R4,R5,R6
;*********************************
SendComm:
PUSH ACC
setb SD ;generate start conditSDn
Acall Delay10uS
setb CLK
Acall Delay10uS
clr SD
Acall Delay10uS
clr CLK
MOV A,R4
ACALL SendByte1
MOV A,R5
ACALL SendByte1
MOV A,R6
ACALL SendByte1
clr sd
Acall Delay10uS
setb CLK
acall Delay10uS
setb sd ;generate stop condition by IFD
acall Delay10uS ;delay 6 us (remain tH at least 9 us)
POP ACC
ret
;*****************************************
SendByte1:
PUSH 03H
mov r3,#8 ;set bits counter
SendLoop:
rrc a
mov sd,c ;send data BIT to I/O port
acall Delay10uS ;delay 12 us (tL,include td7=1 us)
setb CLK
acall Delay10uS ;delay 12 us (tH)
clr CLK
djnz r3,SendLoop
POP 03H
RET
;**************************************************
;R2--Byte number, R5--Start adress,@r0--return data
;**************************************************
Repm:
PUSH 04H ;set resd protection memory CardBuffer
MOV R4,#34h ;CardBuffer
AJMP rr1
Remm:
PUSH 04H ;set read main memory CardBuffer
mov R4,#30h
AJMP rr1
rr1:
PUSH 03H
PUSH 02H
PUSH 00H
Acall SendComm
remm2:
clr a
mov r3,#8 ;set the BIT counter
remm1:
clr CLK
nop ;delay 4 us for td2
nop
nop
nop
setb sd ;get the BIT
nop
mov c,sd
rrc a
nop
nop
setb CLK
acall Delay10uS ;delay 12 us for tL
djnz r3,remm1
mov @r0,a ;get the byte
inc r0
djnz r2,remm2
acall Break
POP 00H
POP 02H
POP 03H
POP 04H
ret
;********************************************************************
Break: clr CLK
acall Delay10uS ;delay 5 us
setb RST
acall Delay10uS ;delay 7 us for tRES
clr RST
ret
;***********************************************************************
Verda:
PUSH 04H
mov R4,#33h ;set compare verification data CardBuffer
AJMP w1
Wrscm:
PUSH 04H
mov R4,#39H ;set write security memory CardBuffer
AJMP w1
Wrpm:
PUSH 04H
mov R4,#3CH ;set write protection memory CardBuffer
AJMP w1
Wrmm:
PUSH 04H
mov R4,#38H ;set write main memory CardBuffer
AJMP w1
w1:
Acall SendComm
wrmm2:
clr CLK
nop ;delay 4 us for td2
nop
nop
nop
setb sd ;get date I/O state
nop
mov c,sd
jc wrmm1 ;if I/O state return to high then terminate
NOP ;writing operation
nop
nop
nop
setb CLK
acall Delay10uS ;delay 12 us for tH
Ajmp wrmm2
wrmm1:
POP 04H
ret
;********************************************************************
Rescm:
PUSH 04H
PUSH 00H
PUSH ByteCnt ;push 03h
PUSH BitCnt ;push 02h
PUSH ACC
mov R4,#31H ;set read main memory
Lcall SendComm
mov ByteCnt,#4 ;Set byte counter
MOV R0,#ReadBuf
rescm2:
mov BitCnt,#8 ;set the BIT counter
rescm1:
clr CLK
nop ;delay 4 us for td2
nop
nop
nop
setb sd ;get the BIT
nop
mov c,sd
rrc a
nop
nop
setb CLK
acall Delay10uS ;delay 12 us for tL
djnz BitCnt,rescm1
mov @R0,a ;get the byte
INC R0
djnz ByteCnt,rescm2
clr CLK ;one more clk make the card to set the
acall Delay10uS ;I/O to state H
POP ACC
POP BitCnt ;pop 02h
POP ByteCnt ;pop 03h
POP 00h
POP 04h
ret
;**************************************************************************
; Anrst: This prg excute answer-to-reset operation according to
; ISO 7816-3.The prg read out the first four bytes of the
; main memory of the sle4442 card and store them in (@R0,
; @(R0+1),@(R0+2),@(R0+3))
;**************************************************************************
Anrst:
PUSH 00H
PUSH ByteCnt
PUSH BitCnt
PUSH PSW
MOV R0,#ReadBuf
clr a ;initialization the buffer
setb RST
acall Delay10uS ;delay 6 us for td4
setb CLK
acall Delay10uS ;delay 12 us for tH
clr CLK
acall Delay10uS ;delay 6 us for td4
clr RST
mov ByteCnt,#4 ;set byte counter
anrst1:
mov BitCnt,#8 ;set BIT counter
anrst2:
acall Delay10uS ;delay 12 us for tL
setb CLK
setb sd ;get BIT
nop
nop
mov c,sd
rrc a
acall Delay10uS ;delay 12 us for tH
clr CLK
djnz BitCnt,anrst2
mov @r0,a
inc r0
djnz ByteCnt,anrst1
MOV R0,#ReadBuf
MOV A,@R0
CJNE A,#Idetify1,CardErr
INC R0
MOV A,@R0
CJNE A,#Idetify2,CardErr
INC R0
MOV A,@R0
CJNE A,#Idetify3,CardErr
INC R0
MOV A,@R0
CJNE A,#Idetify4,CardErr
MOV A,#00H
AJMP Anrstend
CardErr: MOV A,#01H
Anrstend:
POP PSW
POP BitCnt
POP ByteCnt
POP 00h
RET
;*******************************************
;延时10微秒子程序
Delay10uS:
PUSH 07H
MOV R7,#03H ;delay 10us
DJNZ R7,$
POP 07H
RET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -