|
24c64程序 该程序已使用过,免调试。
;宏定义区; N_NOP MACRO nop nop nop ENDM
TRAP MACRO nop nop nop sjmp $ ENDM TRAP_RET MACRO nop nop nop ret nop nop nop sjmp $ ENDM
;=============================================== ; function: start_24c ;启动I2C总线 ; input: --------- ; output: --------- ; usage: --------- ;============================================= start_24c: ;启动I2C总线 setb CSDA ;数据线下降沿产生 N_NOP setb CSCL ;时钟线下降沿 N_NOP clr CSDA N_NOP clr CSCL N_NOP ret
;================================================ ; function: stop_24c ;停止I2C总线 ; input: ; output: --------- ; usage: ;================================================ stop_24c: ;停止I2C总线 clr CSDA ;数据线上升沿有效? N_NOP setb CSCL N_NOP setb CSDA ;数据线上升沿有效? N_NOP clr CSDA ;且时钟线须为高电平 N_NOP clr CSCL ;且时钟线须为高电平 N_NOP ret
;=============================================== ; function: ch_ack(应答位检查);应答位检查 ; input: ; output: --------- ; usage: ;============================================== ch_ack: ;应答位检查 setb CSDA N_NOP setb CSCL N_NOP out_chkack: mov c, SDA jc out_chkack clr SCL N_NOP ret
;================================================= ; function: send_ack(发送应答);;发送应答位 ; input: ; output: --------- ; usage: ;================================================ send_ack: ;发送应答位 clr CSDA N_NOP setb CSCL N_NOP clr CSCL N_NOP setb CSDA N_NOP ret
;=================================================== ; function: send_ack(发送非应答);发送非应答位 ; input: ; output: --------- ; usage: ;============================================= sendn_ack: ;发送非应答位 setb CSDA N_NOP setb CSCL N_NOP clr CSCL N_NOP clr CSDA N_NOP ret
;================================================= ; function: read_24c_N ;从I2C总线接收N个数据字节 ; input: r1---------欲接收数据存放缓冲区首址 ; b----------从器件地址/读写控制字 ; dptr-------欲从eprom读数据的首地址 ; r2---------欲接收数据的字节数 ; output: --------- ; usage: a,c,r1,r2,dptr ;================================================ read_24c_N:;从I2C总线接收N个数据字节 lcall start_24c ;start
mov a, b ;ic_addr clr acc.0 lcall out_24c
;mov a, dph ;addr_low ;lcall out_24c
mov a, dpl ;addr_hi lcall out_24c
lcall start_24c ;start
mov a, b ;ic_addr lcall out_24c
dec r2 cjne r2, #00h, read_loop sjmp read_once read_loop: lcall in_24c ;read lcall send_ack ;send nack mov @r1, a inc r1 djnz r2, read_loop
read_once: lcall in_24c ;read lcall sendn_ack ;send nack lcall stop_24c ;stop
mov @r1, a TRAP_RET
;================================================= ; function: write_24c_N ;向I2C总线发送N个数据字节 ; input: r0---------欲发送数据缓冲区首址 ; b----------从器件地址/写控制字 ; dptr-------欲写入eprom数据的首地址 ; r2---------欲发送数据的字节数 ; output: --------- ; usage: a,c,r0,r2,dptr ;======================================================== write_24c_N:;向I2C总线发送N个数据字节 lcall start_24c ;start
mov a, b ;从地址 lcall out_24c
;mov a, dph ;高位地址 ;lcall out_24c
mov a, dpl ;低位地址 lcall out_24c
write_loop: mov a, @r0 lcall out_24c inc r0 djnz r2, write_loop
lcall stop_24c ;stop N_NOP N_NOP TRAP_RET
;========================================================= ; function: write_24c_1 ;向I2C总线发送1个数据字节 ; input: b----------要写的数据 ; dpl-------欲写入eprom数据的地址 ; output: --------- ; usage: a,b,dptr ;========================================================= write_24c_1:;向I2C总线发送1个数据字节 lcall start_24c ;start
mov a, #0a0h ;从地址 lcall out_24c
mov a, dpl ;低位地址 lcall out_24c
mov a, b lcall out_24c
lcall stop_24c ;stop
N_NOP N_NOP TRAP_RET
;=============================================== ; function: read_24c_1 ;从I2C总线接收1个数据字节 ; input: dpl-------欲从eprom读数据的地址 ; output: a ; usage: a,dptr ;=================================================== read_24c_1:;从I2C总线接收1个数据字节 lcall start_24c ;start
mov a, #0a0h ;ic_addr lcall out_24c
mov a, dpl ;addr_hi lcall out_24c
lcall start_24c ;start
mov a, #0a1h ;ic_addr lcall out_24c
lcall in_24c ;read
lcall sendn_ack ;send nack lcall stop_24c ;stop TRAP_RET
;================================================= ; function: out_24c ;向I2C发送一个字节 ; input: a ; output: -------- ; usage: a,c ;======================================================== out_24c:;向I2C发送一个字节 ;lcall dog_int_feed
mov c, acc.7 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.6 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.5 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.4 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.3 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.2 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.1 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
mov c, acc.0 mov CSDA, c N_NOP setb CSCL N_NOP clr CSCL N_NOP
setb CSDA N_NOP setb CSCL N_NOP
out_wait: mov c, CSDA jc out_wait clr CSCL TRAP_RET
;========================================================= ; function: in_24c ;从I2C总线接收一个数据字节 ; input: --------- ; output: a ; usage: a,c ;================================================= in_24c: ;从I2C总线接收一个数据字节 ;lcall dog_int_feed
setb CSDA N_NOP setb CSCL N_NOP mov c, CSDA mov acc.7, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.6, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.5, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.4, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.3, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.2, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.1, c clr CSCL
N_NOP setb CSCL N_NOP mov c, CSDA mov acc.0, c clr CSCL
N_NOP TRAP_RET
|