给你个汇遍程序片段
其中写EEPROM一段供你参考;其中的数据是我自己程序用的.你可以按照EEPROM的操作写读数据子程序.
关于IIC总线的那些子程序应当是完整的,注释我自己能看懂,不知你是否看得懂.有问题互相交流.
2402的从地址你自己定义
;IIC bus data section define:
bit_cnt data 08h ;Bit counter for iic routines
slv_addr data 0ah ;Slave address for iic routines
flags data 28h ;Location for bit flags
no_ack bit flags.0 ;Iic no acknowledge flag
bus_fault bit flags.1 ;Iic bus fault flag
iic_busy bit flags.2 ;Iic bus is busy flag
eeprom_addr equ 0a6h ;The address of 24c02
;a write data to eeprom subprograming
wri_rom:
mov slv_addr,#eeprom_addr
call master
mov a,#18h
call send_byte
mov slv_addr,#eeprom_addr
call master
mov a,#01h
call send_byte
mov a,#01h
call send_byte
mov a,32h
call send_byte
mov a,33h
call send_byte
mov a,#02h
call send_byte
mov a,#02h
call send_byte
mov a,36h
call send_byte
mov a,37h
call send_byte
call send_stop
call delay10
ret
;iic bus operating subprograming
send_stop: clr sda_pin ;get sda ready for stop.
setb scl_pin ;set clock for stop.
jnb scl_pin,$
nop
nop
nop ;delay 3 cycles
setb sda_pin ;send iic stop.
;delay satisfied via software.
clr iic_busy ;clear iic busy status.
ret ;bus should now be released.
master: setb iic_busy ;indicate that 12c frame is in progress.
clr no_ack ;clear error status flags.
clr bus_fault
jnb scl_pin,fault ;check for bus clear.
jnb sda_pin,fault
clr sda_pin ;begin 12c start.
nop
nop
nop ;delay_3_cycles.
clr scl_pin ;completa 12c start.
nop
nop
nop ;delay_3_cycles.
mov a,slv_addr ;get slave address.
call send_byte ;send slave address.
ret
fault: setb bus_fault ;set fault status.
ret ;and return.
send_byte: mov bit_cnt,#8 ;set bit count value.
sb_loop: rlc a ;send one data bit.
mov sda_pin,c ;put data bit on pin.
setb scl_pin
jnb scl_pin,$ ;drive scl high.
nop
nop
nop ;delay_3_cycles.
clr scl_pin ;clear scl.
nop
nop
nop ;delay_3_cycles.
djnz bit_cnt,sb_loop;repeat until all bits sent.
setb sda_pin ;release data line for acknowledge.
setb scl_pin
jnb scl_pin,$ ;sent clock for acknowledge.
nop
nop
nop
nop ;delay_4_cycles.
jnb sda_pin,sb_ex ;cheek for valid acknowledge bit.
setb no_ack ;set status for no acknowledge.
sb_ex: clr scl_pin ;finish acknowledge bit.
nop
nop
nop ;delay_3_cycles.
ret ;return.
recv_byte: mov bit_cnt,#8 ;set bit count.
rb_loop: setb scl_pin
jnb scl_pin,$ ;read one data bit.
nop
nop
nop ;delay_3_cycles.
mov c,sda_pin ;get data bit from pin.
rlc a ;rotate bit into result byte.
clr scl_pin ;clear scl pin.
nop
nop
nop ;delay_3_cycles.
djnz bit_cnt,rb_loop;repeat accumulator.
push acc ;save accumulator.
mov a,byte_cnt ;copies byte count into a.
cjne a,#1,rb_ack ;check for last byte of frame.
setb sda_pin ;send no acknowledge on last byte.
sjmp rb_aclk ;no ack on last byte;jump to rb_aclk.
rb_ack: clr sda_pin ;send acknowledge bit.
rb_aclk: setb scl_pin
jnb scl_pin,$ ;send acknowledge clock.
pop acc ;restore accumulator.
nop
nop
nop ;delay_3_cycles.
clr scl_pin ;clear scl pin.
setb sda_pin ;clear ackonwledge bit.
nop
nop
nop
nop ;delay_4_cycles.
ret ;return from recv_byte.
;Delay subprograming
delay10: mov r7,#28
delay10_lp: mov r6,#8fh
djnz r6,$
djnz r7,delay10_lp
ret
发表时间:2003年11月14日16:58:54