📄 exp12.asm
字号:
; RCVDATA
;向器件指定子地址读取N个数据
;入口参数: 器件从地址SLAVEADD、器件子地址SUBADD、接收字节数NUMBYTE
;出口参数: 接收数据缓冲区RDBUFF
;占用:A、 R1、 R3、 CY
; receives one or more bytes of data from an I2C slave device.
RCVDATA:
nop
nop
PUSH 01H
PUSH 03H
PUSH ACC
MOV R3,NUMBYTE
; send start bit
LCALL STARTBIT ; acquire bus and send slave address
; send slave address
MOV A, SLAVEADD
LCALL SENDBYTE ; sets NOACK if NACK received
JB NOACK, STOPRCV ; if no acknowledge send stop
; send subaddress
MOV A,SUBADD
LCALL SENDBYTE
JB NOACK, STOPRCV ; if no acknowledge send stop
; send start bit
LCALL STARTBIT ; acquire bus and send slave address
; send slave address
INC SLAVEADD ; Set RW for reception
MOV A, SLAVEADD
LCALL SENDBYTE ; sets NOACK if NACK received
JB NOACK, STOPRCV ; Check for slave not responding.
LCALL DELAY ; this lets slave get data ready
MOV R1, #RDBUFF
RERDN:
nop
nop
LCALL RCVBYTE ; Receive next data byte.
MOV @R1, A ; Save data byte in buffer.
DJNZ R3, TACK
CALL RCVNACK ;最后一字节发非应答位
STOPRCV:
nop
nop
LCALL STOPBIT
JNB NOACK, RCVRET ; if slave sends NACK send error
SETB ERR ; sets the error flag
SETB I2CRS ; this resets the I2C interface
RCVRET:
NOP
NOP
POP ACC
POP 03H
POP 01H
RET
TACK:
nop
nop
LCALL RCVACK ;发应答位
INC R1
SJMP RERDN
RET
;____________________________________________________________________
;
;启动I2C总线子程序
;____________________________________________________________________
; delay
Delay: PUSH 07H
MOV R7,#5H
DJNZ R7,$
POP 07H
RET
;____________________________________________________________________
; ERROR
; this subroutine is run if a NACK is recieved from the slave
ERROR:
LCALL DSPERR ; display the error letter E
RET
;____________________________________________________________________
; STARTBIT
; Sends the start bit to initiate an I2C communication
STARTBIT:
NOP
CLR NOACK
SETB MDE ; enable SDATA pin as an output
NOP
NOP
NOP
NOP
SETB MDO
NOP
NOP
NOP
NOP
SETB MCO
NOP
NOP
NOP
NOP
CLR MDO ; low O/P on SDATA
LCALL DELAY ; delay 5 Machine cycles
CLR MCO ; start bit
RET
;____________________________________________________________________
; STOPBIT
; Sends the stop bit to end an I2C transmission
STOPBIT:
SETB MDE ; to enable SDATA pin as an output
NOP
NOP
NOP
NOP
CLR MDO ; get SDATA ready for stop
NOP
NOP
NOP
NOP
SETB MCO ; set clock for stop
LCALL DELAY
SETB MDO ; this is the stop bit
RET
;____________________________________________________________________
; RCVACK
;发送应答信号子程序
RCVACK:
SETB MDE ; Data pin of the master must be an..
NOP
NOP
NOP
NOP
CLR MDO ; Send no acknowledge, last byte.
NOP
NOP
NOP
NOP
SETB MCO ; Send no-acknowledge clock.
LCALL DELAY
CLR MCO ; clear clock
RET
;____________________________________________________________________
; RCVNACK
;发送非应答信号
RCVNACK:
SETB MDE ; Data pin of the master must be an..
; ..output for the acknowledge
NOP
NOP
NOP
NOP
SETB MDO ; Send no acknowledge, last byte.
NOP
NOP
NOP
NOP
SETB MCO ; Send no-acknowledge clock.
LCALL DELAY
CLR MCO ; clear clock
RET
;____________________________________________________________________
; SENDBYTE
; Send 8-bits in ACC to the slave
SENDBYTE:
MOV BITCNT,#8 ; 8 bits in a byte
SETB MDE ; to enable SDATA pin as an output
CLR MCO ; make sure that the clock line is low
SENDBIT:
RLC A ; put data bit to be sent into carry
MOV MDO,C ; put data bit on SDATA line
SETB MCO ; clock to send bit
LCALL DELAY
CLR MCO ; clear clock
DJNZ BITCNT,SENDBIT ; jump back and send all eight bits
CLR MDE ; release data line for acknowledge
SETB MCO ; send clock for acknowledge
LCALL DELAY
JNB MDI,NEXT ; this is a check for acknowledge
SETB NOACK ; no acknowledge, set flag
NEXT: CLR MCO ; clear clock
RET
;____________________________________________________________________
; RCVBYTE
; receives one byte of data in ACC from an I2C slave device.
RCVBYTE:
MOV BITCNT,#8 ; Set bit count.
CLR MDE ; to enable SDATA pin as an input
nop
nop
CLR MCO ; make sure the clock line is low
nop
nop
RCVBIT:
SETB MCO ; clock to recieve bit
nop
nop
CLR MCO ; clear clock
NOP
NOP
MOV C,MDI ; read data bit into carry.
RLC A ; Rotate bit into result byte.
DJNZ BITCNT,RCVBIT ; Repeat until all bits received.
; recieved byte is in the accumulator
RET
;____________________________________________________________________
;DISPLAY
DSPDATA:
PUSH 00H
PUSH 01H
PUSH 02H
PUSH ACC
MOV R0,#RDBUFF
MOV R1,#DSPBUFF
; MOV R2,#03H
CHA: MOV A,@R0
ANL A,#0FH
MOV @R1,A
INC R1
MOV A,@R0
SWAP A
ANL A,#07H
MOV @R1,A
INC R0
INC R1 ;seconds
MOV A,@R0
ANL A,#0FH
MOV @R1,A
INC R1
MOV A,@R0
SWAP A
ANL A,#07H
MOV @R1,A
INC R0
INC R1 ;minutes
MOV A,@R0
ANL A,#0FH
MOV @R1,A
INC R1
MOV A,@R0
SWAP A
ANL A,#03H
MOV @R1,A
INC R0
INC R1 ;hours
; DJNZ R2, CHA
MOV @R1,#0FFH
POP ACC
POP 02H
POP 01H
POP 00H
RET
;入口:@R0显示数据
DISP_data:
NOP
MOV A,@R0
MOV DPTR,#TAB
MOVC A,@A+DPTR
MOV DPP,#io_page
MOV DPTR,#led8seg_port
MOVX @DPTR,A
ret
;入口:R1显示位置
disp_bit:
NOP
MOV A,R1
MOV DPP,#io_page
MOV DPTR,#led8bit_port
MOVX @DPTR,A
RET
TAB: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,88H,83H,0C6H,0A1H,86H,8EH;0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
DELAY_1MS:
nop
MOV R6,#250
DELAY_1MS1:
NOP
NOP
DJNZ R6,DELAY_1MS1
RET
;____________________________________________________________________
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -