?? sl4442.inc
字號(hào):
;IC卡位定義
ICin BIT P1.4 ;IC卡插入連鎖端口
VCC1 BIT P2.0 ;IC卡電源控制端口
RST BIT P1.1 ;IC卡復(fù)位端
SD BIT P1.2 ;IC卡數(shù)據(jù)線
CLK BIT P1.0 ;IC卡時(shí)鐘線
ICN BIT P0.7 ;IC卡短路檢測(cè)端口
;IC卡常數(shù)
Idetify1 EQU 0A2H ;此處為IC卡保護(hù)區(qū)00~03單元的值,用于識(shí)別卡
Idetify2 EQU 13H
Idetify3 EQU 10H
Idetify4 EQU 91H
;*****************************
checkic:
;寫IC卡之前先校驗(yàn)密碼
clr vcc1
acall delay10us
acall CheckPassword ;密碼校驗(yàn)
ret
;*****************************************************************************
; SLE4442 通用讀寫模塊使用說(shuō)明 *
;ReadCard 讀出從SubAdr開始的ByteCnt字節(jié),結(jié)果存放于(RcvDat)中,讀出正確,ACC中返*
;回#0,返回#01表示無(wú)效卡或者卡損壞,(注讀函數(shù)已經(jīng)對(duì)數(shù)據(jù)進(jìn)行了校驗(yàn)) *
;WriteCard從SubAdr開始寫入ByteCNT字節(jié),待寫入數(shù)據(jù)存放于XmtDat中,寫入正確ACC中 *
;返回#0,返回#01表示無(wú)效卡或者卡損壞.(注.寫函數(shù)已經(jīng)把校驗(yàn)數(shù)自動(dòng)寫入了相應(yīng)的位置
;寫卡之前一定要調(diào)用CheckPassword核對(duì)密碼,*
;密碼存放于PSW開始的三個(gè)單元,ACC中返回#0表示核對(duì)正確,#01表示無(wú)效卡或者卡損壞,*
;#02表示密碼錯(cuò).核對(duì)密碼完成后,EC;開始的4個(gè)單元返回錯(cuò)誤計(jì)數(shù)值及密碼. *
;*****************************************************************************
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
;*******************************************
;延時(shí)10微秒子程序
Delay10uS:
PUSH 07H
MOV R7,#03H ;delay 10us
DJNZ R7,$
POP 07H
RET
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -