📄 i2cserial.asm
字号:
;==============================================================================
; I2C Serial Communication
;------------------------------------------------------------------------------
; Notes:
;
; This module contains a set of simple I2C communication
; routines. As the routines are quite nested they can
; only be used on a device with a reasonably deep stack.
;
; The code assumes that both the SCL and SDA lines are
; pulled high externally through a couple of suitable
; registers. A HI state is created on a line by setting
; the port bit to be a high impedance input, while a LO
; is created by make the port bit a LO output.
;==============================================================================
; Revision History:
;
; 2005-02-03 AJ Initial version
;------------------------------------------------------------------------------
include ../pic.inc
include config.inc
errorlevel -302
global I2C_REGISTER
global I2C_REGCOUNT
global I2cStart
global I2cStop
global I2cRxByte
global I2cTxByte
global I2cAck
global I2cNack
extern SCRATCH
;==============================================================================
; The data area holds the target I2C register
udata
I2C_REGISTER res 1
I2C_REGCOUNT res 1
;==============================================================================
code
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cDelay50us:
call I2cDelay10us
call I2cDelay10us
call I2cDelay10us
call I2cDelay10us
;
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cDelay10us:
clrwdt
nop
nop
nop
nop
nop
nop
return
; Set the SCL line high by making it an input pin and
; then waiting 50us.
;
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cSclHi:
banksel I2C_TRIS
bsf I2C_TRIS,I2C_SCL
goto I2cDelay50us
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cSclLo:
banksel I2C_PORT
bcf I2C_PORT,I2C_SCL
banksel I2C_TRIS
bcf I2C_TRIS,I2C_SCL
goto I2cDelay50us
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cSdaHi:
banksel I2C_TRIS
bsf I2C_TRIS,I2C_SDA
goto I2cDelay50us
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cSdaLo:
banksel I2C_PORT
bcf I2C_PORT,I2C_SDA
banksel I2C_TRIS
bcf I2C_TRIS,I2C_SDA
goto I2cDelay50us
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cStart: call I2cSclLo
call I2cSdaHi
call I2cSclHi
call I2cSdaLo
goto I2cSclLo
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cStop: call I2cSclLo
call I2cSdaLo
call I2cSclHi
goto I2cSdaHi
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cAck: call I2cSdaLo
call I2cSclHi
call I2cSclLo
goto I2cSdaHi
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cNack: call I2cSdaHi
call I2cSclHi
goto I2cSclLo
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cRxByte: call I2cSdaHi
call I2cRxBit
call I2cRxBit
call I2cRxBit
call I2cRxBit
call I2cRxBit
call I2cRxBit
call I2cRxBit
call I2cRxBit
banksel SCRATCH
movf SCRATCH,W
return
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cRxBit:
call I2cSclHi
banksel I2C_PORT
clrc
btfsc I2C_PORT,I2C_SDA
setc
banksel SCRATCH
rlf SCRATCH,F
goto I2cSclLo
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cTxByte:
banksel SCRATCH
movwf SCRATCH
call I2cTxBit
call I2cTxBit
call I2cTxBit
call I2cTxBit
call I2cTxBit
call I2cTxBit
call I2cTxBit
call I2cTxBit
goto I2cSdaHi
; Words: 24/26 Cycles: 11/13 Stack Depth: 0
I2cTxBit:
banksel SCRATCH
rlf SCRATCH,F
skpnc
goto I2cTxHi
call I2cSdaLo
goto I2cTxClk
I2cTxHi: call I2cSdaHi
I2cTxClk: call I2cSclHi
goto I2cSclLo
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -