以前做过的一个读卡器
程序是用51的汇编写的
SOCK BIT P1.1
DIN BIT P1.2
DOUT BIT P1.3
;----------------------------------
;INPUT : A
;OUTPUT: NONE
;SEND A COMMAND
;----------------------------------
SN_CMD:
MOV R2, #008H
CLR C
SN_LOOP:
RLC A
MOV DIN, C
NOP
NOP
SETB SOCK
NOP
NOP
CLR SOCK
NOP
NOP
DJNZ R2,SN_LOOP
RET
;-----------------------------------------
; INPUT : A
;OUTPUT : NONE
;SEND BYTE
;-----------------------------------------
S_BYTE:
MOV R2,#008H ;数据是8位
SBYTE1:
JB DOUT ,$
RRC A
MOV DIN, C ;发送一位
NOP
NOP
SETB SOCK
NOP
NOP
CLR SOCK
DJNZ R2, SBYTE1
RET
;-----------------------------------------
; INPUT : @R0---30H
;OUTPUT : A
;RECEIVE A BYTE
;-----------------------------------------
R_BYTE:
JB TIMER_FLAG,R_BYTE_ERR ;TIMER_FLAG超时标志
JNB SOCK, R_BYTE
R1_1:
JB DOUT,R1_22
JNB TIMER_FLAG,R1_1
SJMP R_BYTE_ERR
R1_22:
JNB SOCK ,R1_2
JNB TIMER_FLAG,R1_22
SJMP R_BYTE_ERR
R1_2:
JNB SOCK,$
R1_3:
MOV C,DOUT
RRC A
JB SOCK,$
DJNZ R2, R1_2
MOV @R0,A
INC R0
MOV R2,#008H
CJNE R0,#03CH,R1_2
R_BYTE_END:
SETB C
RET
R_BYTE_ERR:
CLR C
RET