📄 i2c_eepr.asm
字号:
;|INTERNAL PARAMETERS: |
;| None |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_Start
bset I2CCR,#START ; Generate start condition
.Stwait btjf I2CSR1,#SB,Stwait ; Wait for the Start bit generation (EV5)
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C STOP ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine generates the I2C-Bus Stop Condition |
;| |
;|INPUT PARAMETERS: |
;| None |
;| |
;|INTERNAL PARAMETERS: |
;| None |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_Stop
bset I2CCR,#STOP ; Generate stop condition
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C SET ADDRESS ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine generates the Start-bit and transmits the address byte |
;| |
;|INPUT PARAMETERS: |
;| External I2C device address (A=i2c_addr) |
;| |
;|INTERNAL PARAMETERS: |
;| A (Accumulator) |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_SetAddr
call I2Cm_Start ; Generate a start condition
ld I2CDR,A ; Write address to be transmitted
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C TRANSMIT DATA BYTE ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine transmits a data byte |
;| |
;|INPUT PARAMETERS: |
;| Data byte to be transfered (A=i2c_data) |
;| |
;|INTERNAL PARAMETERS: |
;| Y |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_TxData
ld Y,I2CSR2 ; Check the communication error status
.Txerr jrne Txerr ; Communication error check:infinity loop
bset I2CCR,#PE ; Touch the CR to pass "EV6"
btjf I2CSR1,#BTF,I2Cm_TxData ; Wait BTF ("EV8")
ld I2CDR,A ; Write data byte to be transmitted
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C RECEIVE DATA BYTE ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine receives a data byte |
;| |
;|INPUT PARAMETERS: |
;| Last byte to receive flag (active high): A=last |
;| |
;|INTERNAL PARAMETERS: |
;| Y, A |
;| |
;|OUTPUT PARAMETERS: |
;| Received data byte (A=Data) |
;+------------------------------------------------------------------------------+
.I2Cm_RxData
ld Y,I2CSR2 ; Check the communication error status
.Rxerr jrne Rxerr ; Communication error check:infinity loop
bset I2CCR,#PE ; Touch the CR to pass "EV6"
btjf I2CSR1,#BTF,I2Cm_RxData ; Wait BTF ("EV7")
cp X,#$01 ; Checking for 2nd last byte
jrne Nack ; if not 2nd last byte jump
bres I2CCR,#ACK ; ACK disabled before 2nd last byte read
.Nack tnz A ; Check if it is the last byte to receive
jreq Rx
call I2Cm_Stop ; End of communication: stop condition
.Rx ld A,I2CDR ; Read data byte received
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C TRANSMIT BUFFER ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine transmits the output data buffer via I2C |
;| |
;|INPUT PARAMETERS: |
;| I2c dest @, sub @, Nb data byte to transmit (Y=sub_add, X=nb, A=dest_add) |
;| |
;|INTERNAL PARAMETERS: |
;| Y, X, A |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_Tx
call I2Cm_SetAddr ; Slave address selection on I2C bus
ld A, Y
call I2Cm_TxData ; Send sub-address throught I2C bus
.Txcont dec X ; X: number of data byte to transmit
jrmi Txend ; End of output data buffer reached
ld A, (buff_out,X) ; Next output buffer data byte selected
call I2Cm_TxData ; Send data byte throught I2C bus
jra Txcont
.Txend jra I2Cm_Stop ; Communication End: stop and return
ret
;********************************************************************************
;********************************************************************************
;+------------------------------------------------------------------------------+
;| |
;| I2C RECEIVE BUFFER ROUTINE |
;| |
;+------------------------------------------------------------------------------+
;|ROUTINE DESCRIPTION: |
;| This routine receives a data buffer via I2C |
;| |
;|INPUT PARAMETERS: |
;| Buffer @, sub @, Nb data byte to receive, I2c dest @ |
;| (Y=sub_add, X=nb, A=dest_add) |
;| |
;|INTERNAL PARAMETERS: |
;| Y, X, A |
;| |
;|OUTPUT PARAMETERS: |
;| None |
;+------------------------------------------------------------------------------+
.I2Cm_Rx
push A ; Store the dest_add in stack
call I2Cm_SetAddr ; Slave address selection on I2C bus
ld A, Y
call I2Cm_TxData ; Send sub-address throught I2C bus
pop A ; Restore the dest_add in stack
or A,#$01 ; Force the lsb device address to be 1
; (read mode)
call I2Cm_SetAddr ; Slave address selection on I2C bus
.Rxcont clr A ; Not yet the end of the communication
dec X ; X: number of data byte to receive
jrmi Rxend ; End of input data buffer reached
jrne Rxb
ld A,#$01 ; End communication request
.Rxb call I2Cm_RxData ; Receive data byte from I2C bus
ld (buff_in,X),A ; Next input buffer data byte stored
jra Rxcont
.Rxend bset I2CCR,#ACK ; Acknoledge after reception and return
ret
;********************************************************************************
;+------------------------------------------------------------------------------+
;| INTERRUPT SUB-ROUTINES SECTION |
;+------------------------------------------------------------------------------+
.dummy_rt iret ; Empty subroutine
.i2c_rt iret ; I2C Interrupt
;+------------------------------------------------------------------------------+
;| INTERRUPT VECTORS MAPPING |
;+------------------------------------------------------------------------------+
segment 'vectit'
DC.W dummy_rt ;FFE0-FFE1h location
DC.W dummy_rt ;FFE2-FFE3h location
.i2c_it DC.W i2c_rt ;FFE4-FFE5h location
DC.W dummy_rt ;FFE6-FFE7h location
DC.W dummy_rt ;FFE8-FFE9h location
DC.W dummy_rt ;FFEA-FFEBh location
DC.W dummy_rt ;FFEC-FFEDh location
.timb_it DC.W dummy_rt ;FFEE-FFEFh location
DC.W dummy_rt ;FFF0-FFF1h location
.tima_it DC.W dummy_rt ;FFF2-FFF3h location
.spi_it DC.W dummy_rt ;FFF4-FFF5h location
DC.W dummy_rt ;FFF6-FFF7h location
.ext1_it DC.W dummy_rt ;FFF8-FFF9h location
.ext0_it DC.W dummy_rt ;FFFA-FFFBh location
.softit DC.W dummy_rt ;FFFC-FFFDh location
.reset DC.W main ;FFFE-FFFFh location
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -