原创 EMC指令版本SLE4428卡读写程序

2007-8-29 18:05 3378 6 5 分类: MCU/ 嵌入式
SLE4428卡读写程序。这个EMC指令版本是根据C51程序改写的。已经通过了调试。
;============================
;发送数据子程序
;入口: DBUF_24 =要发送的数据(8_BIT)
;============================
ic_send_data:
MOV A,@8 ;循环次数=8
MOV COUNT_24,A
TXLP:
RRC DBUF_24 ;左移(带C)
CALL ic_BITOUT ;输出1_BIT
DJZ COUNT_24 ;循环结束?
JMP TXLP
RET
;=================
;位输入子程序
;说明: 当SCL=1时,从SDA上读电平
;出口: C="从SDA上读入的位值"
;=================
ic_BITIN:
set_io_in ;设置i/o为输入口
set_clk_out
bc 0x05,0
bs port6,icclk ;clk=1
call delay ;延时
BC R3,0
JBC port6,icio
BS R3,0 ;C=io
call delay ;延时0.6US
BC port6,icclk ;clk=0
call delay ;延时0.6US
RET
;=================
;位输出子程序
;说明: 每当SCL=0时,改写SDA上的电平
;入口: C="要写到SDA上的位值"
;=================
ic_BITOUT:
;SDA_OUT ;设置SDA为输出口
set_io_out
set_clk_out

JBS R3,0
JMP BIT0
BS port6,icio ;io=C=1
JMP io1
BIT0:
BC port6,icio ;io=C=0
io1:
call delay ;延时0.6US
bs port6,icclk ;clk=1
BIT2:
call delay
call delay
bc port6,icclk ;clk=0
RET
;============================
;接收数据子程序
;入口: TT_24.REND_24 =读完毕标志
;出口: DBUF_24 =接受到的数据(8_BIT)
;============================
ic_receive_data:
MOV A,@8 ;循环次数=8
MOV COUNT_24,A
CLR DBUF_24
RXLP:
CALL ic_BITIN ;输入1_BIT
RRC DBUF_24 ;左移(带C)
DJZ COUNT_24 ;循环结束?
JMP RXLP
RET


;===========================
;复位操作
;============================
ic_reset:
set_clk_out
set_io_out
set_rst_out

bc port6,icclk ;clk=0
call delay10
bc port6,icrst ;rst=0
call delay10
bs port6,icio ;i/o=1
call delay10
bs port6,icrst ;rst=1
call delay10
bs port6,icclk ;clk=1
call delay10
bc port6,icclk ;clk=0
call delay10
bc port6,icrst ;rst=0
call delay10

call ic_receive_data
mov ic_buffer1,dbuf_24
call ic_receive_data
;mov ic_buffer2,dbuf_24
call ic_receive_data
;mov ic_buffer3,dbuf_24
call ic_receive_data
;mov ic_buffer4,dbuf_24
ret

;============================延时子程序====================================
;功能:短延时 入口:NONE 出口:NONE
;影响资源:A,TEMP ,STATUS
;==========================================================================
DELAY:
MOV A,@0X05
MOV TEMP,A
DJZ TEMP
JMP $-1
RET
;============================延时子程序10MS================================
;功能:延时 入口:NONE 出口:NONE
;影响资源:A,TEMP ,STATUS
;==========================================================================
DELAY10:
MOV A,@0x10
MOV TEMP,A
MOV A,@0xff
MOV TEMP1,A
DJZ TEMP1
JMP $-1
DJZ TEMP
JMP $-5
RET

DELAY100:
mov a, @0x36
mov delay_time1, a
delay51:
mov a, @0x25
mov delay_time2,a
delay52:
mov a, @0x42
mov flag, a
delay53:
djz flag
jmp delay53
djz delay_time2
jmp delay52
djz delay_time1
jmp delay51
RET

;=========发送命令=========
;send command
;==========================
ic_sendcommand:
;start condition
set_clk_out
set_io_out
bc port6,icclk
bs port6,icio
call delay
bs port6,icclk
call delay
bc port6,icio
call delay
bc port6,icclk
call delay

mov dbuf_24,ic_command1
call ic_send_data
mov dbuf_24,ic_command2
call ic_send_data
mov dbuf_24,ic_command3
call ic_send_data

;stop condition
bc port6,icio
call delay
bs port6,icclk
call delay
bs port6,icio
call delay
ret

;process
ic_process:
bc port6,icclk
call delay
bc port6,icio
mov process_temp,@254
puls:
bs port6,icclk
call delay
bc port6,icclk
djz process_temp
jmp puls
ret

/*================读主存储器==================
入口:rmm_address 读取的地址
rmm_len 读取的长度
出口:ic_buffer1
;============================================*/
ic_readmainmemory:
bank 0
mov ic_command1,@0x30
mov ic_command2,rmm_address
mov ic_command3,@0
call ic_sendcommand

bc port6,icclk
call delay
mov len,@0 ;0x10为私有要保护
mov rmm_temp,@255
mov a,rmm_address
sub rmm_temp,a
inc rmm_temp ;rmm_temp=256-address
goto_readdata:
CJL len,rmm_temp,readdata
;COMPARE AND JUMP IF REG1 jmp exit_readdata
readdata:
CJL len,rmm_len,save_data
call ic_receive_data
jmp drop_data
save_data:
call ic_receive_data
mov a,dbuf_24
mov ic_buffer1,a
drop_data:
inc len
jmp goto_readdata
exit_readdata:
bs port6,icclk
call delay
bc port6,icclk
call delay
ret

/*================写主存储器==================
入口:wmm_address 写主存的地址
wmm_value 写主存的数据
出口:
;============================================*/
ic_writemainmemory:
set_io_out
set_clk_out
mov ic_command1,@0x38
mov ic_command2,wmm_address
mov ic_command3,wmm_data
call ic_sendcommand
bc port6,icclk
call delay
bc port6,icio
call delay
mov len,@0 ;0x10为私有要保护
goto_writedata:
CJL len,@254,writedata
jmp exit_writedata
writedata:
bs port6,icclk
call delay
bc port6,icclk
call delay
inc len
jmp goto_writedata
exit_writedata:
bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
ret
/*================读安全储区=================
入口:
出口:rsm_data1、rsm_data2、rsm_data3、rsm_data4读取的数据
;============================================*/
ic_readsecmemory:
set_io_out
set_clk_out
mov ic_command1,@0x31
;mov a,@0xff
mov ic_command2,@0xff
mov ic_command3,@0xff
call ic_sendcommand
bc port6,icclk
call delay

call ic_receive_data
mov rsm_data1,dbuf_24
call ic_receive_data
mov rsm_data2,dbuf_24
call ic_receive_data
mov rsm_data3,dbuf_24
call ic_receive_data
mov rsm_data4,dbuf_24
set_io_out
set_clk_out
bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
ret
/*================比较密码 =================
入口:
出口:
;============================================*/
ic_compareverifdata:
set_io_out
set_clk_out
call ic_readsecmemory
nop
nop
;mov a,rsm_data1
;and a,@0x07
;mov rsm_data1,a
mov rsm_data1,rsm_data1
jbc 0x03,2
jmp fail_exit

nop
CJE rsm_data1,@0x07,setbit1
CJE rsm_data1,@0x06,setbit2
CJE rsm_data1,@0x04,setbit3
jmp fail_exit
setbit1:
mov wsm_data,@0x06
nop
jmp endcomp
setbit2:
mov wsm_data,@0x04
nop
jmp endcomp
setbit3:
mov wsm_data,@0x00
nop
endcomp:
mov wsm_address,@0
call ic_writesecmemory

;mov len,@0
;CJL len,@3,writedata
;mov password_data,@0xff
mov ic_command1,@0x33
mov ic_command2,@1
mov ic_command3,@0xff
call ic_sendcommand
set_io_out
set_clk_out
bc port6,icclk
call delay
bc port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay

mov ic_command1,@0x33
mov ic_command2,@2
mov ic_command3,@0xff
call ic_sendcommand

bc port6,icclk
call delay
bc port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay

mov ic_command1,@0x33
mov ic_command2,@3
mov ic_command3,@0xff
call ic_sendcommand

bc port6,icclk
call delay
bc port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
nop
fail_exit:
mov wsm_address,@0
mov wsm_data,@0xff
call ic_writesecmemory
call delay
call ic_readsecmemory
CJE rsm_data1,@0x07,success
mov ic_compare_password_flag,@0
jmp failed
success:
mov ic_compare_password_flag,@1
failed:
nop
nop
ret
/*================读保护储器=================
出口:rpm_data 读取的数据
;============================================*/
ic_readprotectmemory:
set_io_out
set_clk_out
mov ic_command1,@0x34
mov a,@0
mov ic_command2,a
mov ic_command3,a
call ic_sendcommand

bc p6cr,icclk
call delay
call ic_receive_data
mov rpm_data1,dbuf_24
call ic_receive_data
mov rpm_data2,dbuf_24
call ic_receive_data
mov rpm_data3,dbuf_24
call ic_receive_data
mov rpm_data4,dbuf_24
set_io_out
set_clk_out
bs p6cr,icclk ;stop condition
call delay
bc p6cr,icclk
call delay
ret

/*================写保护储器=================
入口:wpm_address 写的地址
出口:wpm_data 写的数据
;============================================*/
ic_writeprotectmemory:
set_io_out
set_clk_out
mov ic_command1,@0x3c
mov ic_command2,wpm_address
mov ic_command3,wpm_data
call ic_sendcommand
bc p6cr,icclk
call delay
bc p6cr,icio
call delay

mov len,@0
goto_writeprotectdata:
CJL len,@254,writeprotectdata
jmp exit_writeprotectdata
writeprotectdata:
bs p6cr,icclk
call delay
bc p6cr,icclk
call delay
inc len
jmp goto_writeprotectdata
exit_writeprotectdata:

bs p6cr,icio
call delay
bs p6cr,icclk ;stop condition
call delay
bc p6cr,icclk
call delay
ret


/*================写安全储区=================
入口:wsm_address 写的地址
出口:wsm_data 写的数据
;============================================*/
ic_writesecmemory:
set_io_out
set_clk_out
mov ic_command1,@0x39
mov ic_command2,wsm_address
mov ic_command3,wsm_data
call ic_sendcommand
bc port6,icclk
call delay
bc port6,icio
call delay

mov len,@0
goto_writesecdata:
CJL len,@254,writesecdata
jmp exit_writesecdata
writesecdata:
bs port6,icclk
call delay
bc port6,icclk
call delay
inc len
jmp goto_writesecdata
exit_writesecdata:

bs port6,icio
call delay
bs port6,icclk ;stop condition
call delay
bc port6,icclk
call delay
ret

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /2 下一条