?? i2c.txt
字號:
////=====================================================
read_data: ldi peout,1111b
ldi pe,1111b
call delay_2us
ldi rom_buf,1110b
sta pe,00h ;sda low,start I2C
call delay_2us
ldi rom_data,1010b
call write_byte
ldi rom_data,0000b ;device address
call write_byte
call ack_sub
lda data_addrh,00h
sta rom_data,00h
call write_byte
lda data_addrl,00h
sta rom_data,00h ;read address
call write_byte
call ack_sub
////---------------------
andim rom_buf,1101b
sta pe,00h
call delay_2us
ldi pe,0fh
call delay_2us
ldi rom_buf,1110b
sta pe,00h ;sda low,start I2C
call delay_2us
////---------------------
ldi rom_data,1010b
call write_byte
ldi rom_data,0001b ;read commande
call write_byte
call ack_sub
////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ldi peout,1110b ;pc2 input
////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ldi read_datal,00h
ldi read_datah,00h
ldi temp,08h
sta READ_CNT,01h
read_loop:
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
orim rom_buf,0010b
sta pe,00h
lda read_datah,00h
addm read_datah,00h
lda read_datal,00h
addm read_datal,00h
bnc d1_bit0_0
orim read_datah,01h
d1_bit0_0:
lda pe,00h
ba0 is_bit_1
jmp cnt_sub
is_bit_1:
orim read_datal,01h
cnt_sub: ldi temp,01h
subm READ_CNT,01h
bnz read_loop
////%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ldi peout,1111b
ldi rom_buf,1101b
sta pe,00h
call delay_2us
orim rom_buf,0010b
sta pe,00h
call delay_2us
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
ldi pe,1100b ;sda low
call delay_2us
ldi pe,1110b ;scl high
call delay_2us
ldi pe,1111b ;sda high when scl high,stop
call delay_2us
rtni
////=====================================================
write_data: ldi peout,1111b
ldi pe,0fh
call delay_2us
ldi rom_buf,1110b
sta pe,00h ;sda low,start I2C
call delay_2us
ldi rom_data,1010b
call write_byte
ldi rom_data,0000b ;device address
call write_byte
call ack_sub
lda data_addrh,00h
sta rom_data,00h
call write_byte
lda data_addrl,00h
sta rom_data,00h ;write address
call write_byte
call ack_sub
lda DATA_H,00h
sta rom_data,00h
call write_byte
lda DATA_L,00h
sta rom_data,00h ;write data
call write_byte
call ack_sub
i2c_stop:
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
ldi pe,1100b ;sda low
call delay_2us
ldi pe,1110b ;scl high
call delay_2us
ldi pe,1111b ;sda high when scl high,stop
call delay_2us
rtni
////=====================================================
write_byte:
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
andim rom_buf,1110b
ldi TEMP_0,1000b
and rom_data,00h ;bit3
shr
shr
shr
orm rom_buf,00h
sta pe,00h ;send sda
call delay_2us
orim rom_buf,0010b
sta pe,00h ;scl high
call delay_2us
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
andim rom_buf,1110b
ldi TEMP_0,0100b
and rom_data,00h ;bit2
shr
shr
orm rom_buf,00h
sta pe,00h ;send sda
call delay_2us
orim rom_buf,0010b
sta pe,00h ;scl high
call delay_2us
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
andim rom_buf,1110b
ldi TEMP_0,0010b
and rom_data,00h ;bit1
shr
orm rom_buf,00h
sta pe,00h ;send sda
call delay_2us
orim rom_buf,0010b
sta pe,00h ;scl high
call delay_2us
andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
andim rom_buf,1110b
ldi TEMP_0,0001b
and rom_data,00h ;bit0
orm rom_buf,00h
sta pe,00h ;send sda
call delay_2us
orim rom_buf,0010b
sta pe,00h ;scl high
call delay_2us
rtni
////=====================================================
ack_sub: andim rom_buf,1101b ;scl low
sta pe,00h
call delay_2us
orim rom_buf,0001b
sta pe,00h
ldi peout,1110b ;pc2 input
call delay_2us
orim rom_buf,0010b ;scl high
sta pe,00h
ack_wait: lda pe,00h
ba0 ack_wait
ldi peout,1111b
call delay_2us
rtni
////=====================================================
delay_2us: nop
nop
nop
nop
rtni
//======================================================
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -