📄 dt-107-005cn.txt
字号:
;*******************************************************************************
OutByte:
setb C
OutByteLoop:
rlc A ; SHIFT OUT THE BYTE, MSB FIRST
jnc OutByte0
setb P1.SDAbit
ajmp OutByte1
OutByte0:
clr P1.SDAbit
OutByte1:
acall ClockPulse ; CLOCK THE DATA INTO THE IIC Device
cjne A,#10000000b,OutByteNxt ; SKIP IF NOT DONE
jmp GetACK ; CHECK FOR AN ACK FROM THE DEVICE
OutByteNxt:
clr C ; LOOP IF ALL THE BITS HAVE
ajmp OutByteLoop ; NOT BEEN SHIFTED OUT
;******************************************************************************
;** Name: InByte
;** Description: Shifts in a byte from the IIC Device
;** Function: This subroutine shifts in a byte, MSB first, through the
;** assigned SDA/SCL lines on port 1. After the byte is received
;** an ACK bit is sent to the IIC Device.
;** Calls: ClockPulse
;** Input: None
;** Return Value: A = Received byte
;** Register Usage: A
;******************************************************************************
InByte:
mov A,#00000001b
setb P1.SDAbit ; SDA LINE FORCED HIGH
InByteNxt:
acall ClockPulse ; CLOCK THE Serial MEMORY AND SHIFT
rlc A ; INTO ACC. THE LOGIC LEVEL ON THE SDA
jnc InByteNxt ; LINE. THE DEVICE OUTPUTS DATA ON SDA
ret ; MSB FIRST
;******************************************************************************
;** Name: ClockPulse
;** Description: Generate a clock pulse
;** Function: This subroutine forces a high-low transition on the assigned SCL
;** pin of port 1. It also samples and returns the SDA line state
;** during high clock period.
;** Calls: None
;** Input: None
;** Return Value: C = SDA line status
;** Register Usage: None
;******************************************************************************
ClockPulse:
nop
setb P1.SCLbit ; BASED ON A 12MHz SYSTEM CRYSTAL THE
nop ; BUS CYCLE TIME IS 1 MICROSEC.
nop
nop
clr C ;
jnb P1.SDAbit,ClockPulseLo ;
setb C
ClockPulseLo:
clr P1.SCLbit ; LOWER THE CLOCK LINE
ret
;******************************************************************************
;** Name: OutACK
;** Description: Send out an ACK bit to the IIC Device
;** Function: This subroutine clocks an ACK bit to the IIC Device.
;** The ACK cycle acknowledges a properly received data by lowering
;** the SDA line during this period (9th clock cycle of a received
;** byte).
;** !!!!!!!!!! RETURN FROM CALL PARENT ROUTINE !!!!!!!!!!!
;** Calls: ClockPulse
;** Input: None
;** Return Value: None
;** Register Usage: None
;******************************************************************************
OutACK:
clr P1.SDAbit ; MAKE SURE THAT THE DATA LINE IS LOW ...
jmp ClockPulse ; CLOCK OUT THE ACK CYCLE
;******************************************************************************
;** Name: GetACK
;** Description: Clock the IIC Device for ACK cycle
;** Function: This subroutine returns the sampled logic level on the SDA during
;** high clock cycle. The IIC Device holds the SDA line HIGH if
;** it does not receive the correct number of clocks or it's stuck in
;** a previously initiated write command.
;** Calls: ClockPulse
;** Input: None
;** Return Value: C = ACKnowledge bit
;** Register Usage: None
;******************************************************************************
GetACK:
setb P1.SDAbit ; FORCE SDA LINE HIGH
acall ClockPulse ; GENERATE ONE CLOCK PULSE
ret
;******************************************************************************
;** Name: ACKPoll
;** Description: Wait for an ACK from the IIC Device
;** Function: This subroutine monitors the IIC Device response
;** following a dummy write command. The program returns when it
;** either receives an ACK bit or the maximum number of tries is
;** reached.
;** Calls: Start, SlavAddr, Stop
;** Input: None
;** Return Value: C = ACKnowledge bit [=0 ACK ,=1 No ACK was received]
;** Register Usage: A, R1
;******************************************************************************
ACKPoll:
mov R1,#MaxDelay ; LOAD MAX NO. OF ACK POLLING CYCLE
ACKPollnxt:
acall Start ; START THE ACK POLL CYCLE AND
clr C ; [C=0] WRITE OPERATION BIT
acall SlavAddr ; SEND THE SLAVE ADDRESS. THEN
; MONITOR THE SDA LINE FOR AN ACK FROM
; THE IIC Device. TERMINATE THE
acall Stop ; OPERATION BY A STOP CONDITION.
jnc ACKPollExit ; EXIT IF THE ACK WAS RECEIVED
djnz R1,ACKPollnxt ; LOOP WHILE THE MAXIMUM NO. OF CYCLES
; HAVE NOT EXPIRED. ELSE RETURN WITH C=1
ACKPollExit:
ret
;******************************************************************************
;** Name: Start
;** Description: Send a start command to the IIC Device
;** Function: This subroutine generates a start condition on the bus. The start
;** condition is defined as a high-low transition on the SDA
;** line while the SCL is high. The start is used at the beginning
;** of all transactions.
;** Calls: None
;** Input: None
;** Return Value: None
;** Register Usage: None
;******************************************************************************
Start:
setb P1.SDAbit ; FORCE THE SDA LINE HIGH
setb P1.SCLbit ; FORCE THE SCL CLOCK LINE HIGH
nop
nop
nop
clr P1.SDAbit ; BEFORE TAKING THE SDA LOW
nop
nop
nop
nop
clr P1.SCLbit ; FORCE THE SCL LOW
ret
;******************************************************************************
;** Name: Stop
;** Description: Send stop command to the IIC Device
;** Function: This subroutine generates a stop condition on the bus. The stop
;** condition is defined as a low-high transition on the SDA
;** line while the SCL is high. The stop is used to indicate end
;** of current transaction.
;** Calls: None
;** Input: None
;** Return Value: None
;** Register Usage: None
;******************************************************************************
Stop:
clr P1.SDAbit ; FORCE THE SDA LOW BEFORE TAKING
setb P1.SCLbit ; THE SCL CLOCK LINE HIGH
nop
nop
nop
nop
setb P1.SDAbit ; FORCE THE SDA HIGH (IDLE STATE)
ret
;******************************************************************************
;** Name: ResetIIC
;** Description: Resets the IIC Device
;** Function: This subroutine is written for the worst case. System interruptions
;** caused by brownout or soft error conditions that reset the main
;** CPU may have no effect on the internal Vcc sensor and reset
;** circuit of the IIC Device. These are unpredictable and
;** random events that may leave the IIC Device interface
;** logic in an unknown state. Issuing a Stop command may not be
;** sufficient to reset the IIC Device.
;** Calls: Start, Stop
;** Input: None
;** Return Value: None
;** Register Usage: R0
;******************************************************************************
ResetIIC:
mov R0,#0AH ; APPLY 10 CLOCKS TO THE DEVICE. EACH
ResetNxt:
acall Start ; CYCLE CONSISTS OF A START/STOP
acall Stop ; THIS WILL TERMINATE PENDING WRITE
djnz R0,ResetNxt ; COMMAND AND PROVIDES ENOUGH CLOCKS
ret ; FOR REMAINING BITS OF A READ OPERATION
;******************************************************************************
;** END OF IIC Device INTERFACE SOURCE CODE
;******************************************************************************
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -