📄 routine.txt
字号:
;;######################## start I2C #################################################################
;;function:this program is to start the I2C
;;input:void
;;output:void
;;####################################################################################################
StartI2C MOV #0AH,R15
BIS.B #40H,&P2OUT ;P2.6->SDA = 1 make SDA= 1 ,to creat start condition
NOP
NOP
BIS.B #80H,&P2OUT ;P2.7->SCL = 1 tsu:STA = 4.7 us
StartI2CL1 NOP
DEC R15
JNZ StartI2CL1
BIC.B #40H,&P2OUT ;P2.6->SDA.when SCL=1 & SDA =1 -> 0
;
MOV #0AH,R15
StartI2CL3 NOP ;thd:STA = 4us
DEC R15
JNZ StartI2CL3
;
BIC.B #80H,&P2OUT ;P2.7->SCL = 0 t_low = 4.7us
MOV #0AH,R15
StartI2CL2 NOP
DEC R15
JNZ StartI2CL2
RET
;;######################## End I2C #################################################################
;;function:this program is to end the I2C manipulate
;;input:void
;;output:void
;;####################################################################################################
EndI2C MOV #0AH,R15
BIC.B #40H,&P2OUT ;P2.6 make SDA= 0 ,to creat end condition
NOP
NOP
BIS.B #80H,&P2OUT ;P2.7->SCL = 1
EndI2CL1 NOP
DEC R15
JNZ EndI2CL1 ;tsu:sto >= 4.7us
BIS.B #40H,&P2OUT ;P2.6->SDA when SCL=1 & SDA =0 -> 1
;
EndI2CL3 MOV #0AH,R15
NOP
DEC R15
JNZ EndI2CL3
;
MOV #0AH,R15
BIC.B #80H,&P2OUT ;P2.7->SCL = 0
EndI2CL2 NOP
DEC R15
JNZ EndI2CL2
RET
;;##################### send Acknowledge or unacknowledge to the slave device ########################
;;function: master device sends acknowledge to slave device
;;input:if to send acknowledge ,the value is stored in &I2CTran
;;output:void
;;####################################################################################################
SendAck MOV #08H,R15
BIC.B #40H,&P2OUT ;P2.6 make SDA = 0,to send acknowledge signal to slave device
CMP.B #ACK,&I2CTran
JZ ACKI2CL3
BIS.B #40H,&P2OUT ;P2.6 make SDA = 1,to send unacknowledge signal to slave device
ACKI2CL3 NOP
NOP
NOP
BIS.B #80H,&P2OUT ;P2.7->SCL = 1,t_high = 4 us
ACKI2CL1 NOP
DEC R15
JNZ ACKI2CL1
BIC.B #80H,&P2OUT ;P2.7->SCL = 0 ,t_low = 4.7 us
MOV.B #0AH,R15
ACKI2CL2 NOP
DEC R15
JNZ ACKI2CL2
RET
;;####################### receive acknowledge from the slave device #################################
;;function: receive the acknowledge flag form the slave device
;;input: void
;;output:you can through inv.b &I2CTran,bit.b #40h,&I2CTran,if true ,get receive ack signal
;;####################################################################################################
RevACK MOV #08H,R15
BIC.B #40H,&P2DIR ;P2.6 direction is set to input
BIS.B #80H,&P2OUT ;P2.7->SCL = 1,t_high = 4 us
RevACKL1 NOP
DEC R15
JNZ RevACKL1
MOV.B &P2IN,&I2CTran ;
MOV #08H,R15
BIC.B #80H,&P2OUT ;P2.7->SCL = 0,t_high = 4.7 us
RevACKL3 NOP
DEC R15
JNZ RevACKL3
BIS.B #40H,&P2DIR ;P2.6 direction is set to output
INV.B &I2CTran
MOV.B #88H,&I2CTran ;ack get
BIT.B #40h,&I2CTran
JZ RevACKL2
MOV.B #77H,&I2CTran ;ack not get
RevACKL2 RET
;;####################### Write a byte data or command to the slave device ###########################
;;function: send a byte data or command to the slave device
;;input:the data need to send is stored in &I2CTran
;;output:void
;;####################################################################################################
WRI2C1B PUSH R14
PUSH R13
MOV #08H,R14
MOV.B &I2CTran,R13
;begin to 1 bit send
WRI2C1BL4 BIC.B #40H,&P2OUT ;P2.6 make SDA = 0
BIT.B #80H,R13
JZ WRI2C1BL1
BIS.B #40H,&P2OUT ;P2.6 make SDA = 1
WRI2C1BL1 NOP
NOP
NOP
BIS.B #80H,&P2OUT ;P2.7->SCL = 1,t_high = 4 us
MOV #08H,R15
WRI2C1BL2 NOP
DEC R15 ;delay 4 us
JNZ WRI2C1BL2
BIC.B #80H,&P2OUT ;P2.7->SCL = 0,t_low = 4.7us
MOV #04H,R15
WRI2C1BL3 NOP
DEC R15
JNZ WRI2C1BL3 ;delay
;end 1 bit send
RLA R13
DEC R14
JNZ WRI2C1BL4
POP R13
POP R14
RET
;;######################## Receive a byte data from the slave device #################################
;;function:receive a byte data from the slave device
;;input:void
;;output:the data is stored in &I2CTran
;;####################################################################################################
RecvI2C1B PUSH R14
PUSH R13
MOV #08H,R14
MOV #00H,R13
BIC.B #40H,&P2DIR ;P2.6 direction is set to input
RecvI2C1BL4 MOV #06H,R15
BIS.B #80H,&P2OUT ;P2.7->SCL = 1,t_high = 4 us
RecvI2C1BL1 NOP
DEC R15 ; delay
JNZ RecvI2C1BL1
MOV.B &P2IN,&I2CTran ;
SETC
BIT.B #40H,&I2CTran
JNZ RecvI2C1BL2
CLRC
RecvI2C1BL2 RLC.B R13
MOV #07H,R15
BIC.B #80H,&P2OUT ;P2.7->SCL = 0,t_low = 4.7us
RecvI2C1BL3 NOP
DEC R15
JNZ RecvI2C1BL3
DEC R14
JNZ RecvI2C1BL4
MOV.B R13,&I2CTran ;get the data form the slave device
BIS.B #40H,&P2DIR ;P2.6 direction is set to output
POP R13
POP R14
RET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -