📄 i2c.inc
字号:
; shl al, 03h
mov al,00011000b
call PreCsI2CWrite
mov cx, 007ffh
call DelayBase01
call CsI2CDummy
; pop dx
pop cx
pop ax
ret
RvDvAcktoTm ENDP
;-----------------------------------------------------------------------;
; SeqRdDvAcktoTm ;
;-----------------------------------------------------------------------;
; Receiver send ACK to Transmitter when sequential read. ;
;-----------------------------------------------------------------------;
SeqRdDvAcktoTm PROC PUBLIC
push ax
push cx
; push dx
; mov dh, 00000000b ; I2C data , this is for Receiver ACK cycle
; mov dl, 00000001b ; I2C clock
; mov al, 00h
; or al, dh
; shl al, 01h
; or al, dl
; shl al, 03h
mov al,00001000b
call PreCsI2CWrite
mov cx, 07ffh
call DelayBase01
call CsI2CDummy
; pop dx
pop cx
pop ax
ret
SeqRdDvAcktoTm ENDP
;-----------------------------------------------------------------------;
; ChkAck ;
;-----------------------------------------------------------------------;
; Wait the slave to assert data to low for acknowledge. ;
; Input : None ;
; Output: ;
; Register destroyed: None ;
;-----------------------------------------------------------------------;
ChkAck PROC PUBLIC
push ax
push cx
mov ah, 06dh
call read_sio_byte
or al, 00010000b ;I2C bus data active level high
call write_sio_byte
mov cx, 05ffh
call DelayBase01
mov cl, 010h
NoAckW:
dec cx
jz WaitAckTimOut
mov al, 00h ;This is for Transmitter ACK cycle
call CsI2CRead
test al, 00010000b
jnz NoAckW
clc
jmp short EndWaitAck
WaitAckTimOut:
stc
EndWaitAck:
pop cx
pop ax
ret
ChkAck ENDP
;-----------------------------------------------------------------------;
; PreEnableI2CBus ;
;-----------------------------------------------------------------------;
; Set the GPIO to default high. ;
; Input : None ;
; Output: None ;
; Register destroyed: None ;
;-----------------------------------------------------------------------;
PreEnableI2CBus PROC PUBLIC
push ax
push cx
mov ah, 06dh
call read_sio_byte
or al, 00011000b ;I2C bus data/clock active level high
call write_sio_byte
mov cx, 07ffh
call DelayBase01
pop cx
pop ax
ret
PreEnableI2CBus ENDP
;-----------------------------------------------------------------------;
; EnableI2CBus ;
;-----------------------------------------------------------------------;
; Input : None ;
; Output: None ;
; Register destroyed: None ;
;-----------------------------------------------------------------------;
EnableI2Cbus PROC PUBLIC
push ax
mov ah, 06dh
call read_sio_byte
or al, 00000100b ;Enable I2C bus control
call write_sio_byte
pop ax
ret
EnableI2CBus ENDP
;-----------------------------------------------------------------------;
; DelayBase ;
;-----------------------------------------------------------------------;
; Input : (CX) Delay count ;
; Output: (CX) = 0 ;
; Register destroyed: CX ;
;-----------------------------------------------------------------------;
DelayBase:
jmp short $+2
DelayBase01:
jmp short $+2
jmp short $+2
jmp short $+2
jmp short $+2
jmp short $+2
loop DelayBase
ret
;;==========================================================================;;
;; Input: AH = Device Slave Address
;; AL = Device Register Index
;; Output:AL = Register Value
;; Destroy:None
i2c_read_byte proc public
push cx
push dx
mov cx, ax
call PreEnableI2CBus ;I2C bus Data/Clock active level High
call EnableI2CBus ;Enable I2C bus
call StrI2CCycle
call CsI2CDummy
mov ah, 08h ; Device Slave Address
mov dh, ch ; I2C data (/W)
mov dl, 11111111b ; I2C clock
call csI2CCmd
call ChkAck
call CsI2CDummy
mov dh, cl ; I2C data (Device Register Index)
call csI2CCmd
call ChkAck
call CsI2CDummy
call StrI2CCycle
call CsI2CDummy
mov dh, ch ; I2C data
or dh, 01h ; (Read)
call csI2CCmd
call ChkAck
call CsI2CDummy
mov ah, ch ; Restore AH
mov cx,8
mov dl,0
read_loop:
shl dl,1
call CSI2CRead
test al,10h
jz read_data_low
or dl,1
read_data_low:
loop read_loop
mov al, dl ; AL = Read Data
call RvDvAcktoTm
call CsI2CDummy
call PreStpI2CCycle
mov cx, 0ffffh
call DelayBase01
pop cx
pop dx
ret
i2c_read_byte endp
;;==========================================================================;;
;; Input: AH = Device Slave Address
;; AL = Device Register Index
;; CL = Write Data
;; Output:None
;; Destroy:None
i2c_write_byte proc public
push cx
push dx
mov ch, ah
call PreEnableI2CBus ;I2C bus Data/Clock active level High
call EnableI2CBus ;Enable I2C bus
call StrI2CCycle
call CsI2CDummy
mov ah, 08h ; Device Slave Address
mov dh, ch ; I2C data (/W)
mov dl, 11111111b ; I2C clock
call csI2CCmd
call ChkAck
call CsI2CDummy
mov dh, al ; I2C data (Device Register Index)
call csI2CCmd
call ChkAck
call CsI2CDummy
mov dh, cl ; I2C data
call csI2CCmd ; Write Data
call ChkAck
call CsI2CDummy
mov ah, ch ; Restore AH
call PreStpI2CCycle
mov cx, 0ffffh
call DelayBase01
pop cx
pop dx
ret
i2c_write_byte endp
;;==========================================================================;;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -