⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.asm

📁 web51的开发库
💻 ASM
字号:
;############################################;# PROCEDURY pro komunikaci po sbernici	I2C #;############################################;	.global	read_current	.global	read_random;.nolist.include "param.inc".list;	.bitcomm I2Cpresent, 1	.text	.using	0read_current:	; AT24Cxx Current Address Read function.	; Called with programmable address in A. Returns data in A.	; Returns CY set to indicate that the bus is not available	; or that the addressed device failed to acknowledge.	lcall	start	jc	x45		; abort if bus not available	mov	a, #(FADDR | 0b00000001); set fixed address and read operation	lcall	shout		; send device address	jc	x44		; abort if no acknowledge	lcall	shin		; receive data byte	lcall	NAK		; do not acknowledge byte	clr	c		; clear error flagx44:	sjmp	stopx45:	retread_random:	; AT24Cxx Random Read function.	; Called with programmable address in A, byte address in	; register pair DPTR. Returns data in A.	; Returns CY set to indicate that the bus is not available	; or that the addressed device failed to acknowledge.	; Send dummy write command to set internal address.	lcall	start	jc	x47		; abort if bus not available	mov	a, #(FADDR & 0b11111110)	; set fixed address	lcall	shout		; send device address	jc	x46		; abort if no acknowledge	mov	a, DPH		; send high byte of address	lcall	shout		;	jc	x46		; abort if no acknowledge	mov	a, DPL		; send low byte of address	lcall	shout		;	jc	x46		; abort if no acknowledge	; Call Current Address Read function.	sjmp	read_currentx46:	sjmp	stopx47:	retstart:	; Send START, defined as high-to-low SDA with SCL high.	; Return with SCL, SDA low.	; Returns CY set if bus is not available.	setb	SDA	setb	SCL	; Verify bus available.	setb	c		; set error flag	jnb	SDA, failstart	; jump if not high	jnb	SCL, failstart	; jump if not high;	nop			; enforce setup delay and cycle delay	clr	SDA;	nop			; enforce hold delay;	nop			;;	nop			;;	nop			;	nop			;	clr	SCL	cpl	c		; clear error flagfailstart:	retstop: 	; Send STOP, defined as low-to-high SDA with SCL high.	; SCL expected low on entry. Return with SCL, SDA high.	clr	SDA	nop			; enforce SCL low and data setup;	nop	setb	SCL;	nop			; enforce setup delay;	nop			;;	nop			;;	nop			;	nop			;	setb	SDA	retshout:	; Shift out a byte to the AT24Cxx, most significant bit first.	; SCL, SDA expected low on entry. Return with SCL low.	; Called with data to send in A.	; Returns CY set to indicate failure by slave to acknowledge.	; Destroys A.	setb	C		;Ack BITshoutLp:rlc	a		; move bit into CY	mov	SDA, c		; output bit	nop			; enforce SCL low and data setup	setb	SCL		; raise clock	jz	shoutAck;	nop			; enforce SCL high;	nop			;	clr	C		;	clr	SCL		; drop clock	sjmp	shoutLp		; next bitshoutAck:;	nop			; enforce SCL high;	nop			;;	nop			;;	nop			;	mov	c, SDA		; get ACK bit	clr	SCL		; drop ACK clock	retshin:	; Shift in a byte from the AT24Cxx, most significant bit first.	; SCL expected low on entry. Return with SCL low.	; Returns received data byte in A.	mov	a,#1		; stop marker	setb	SDA		; make SDA an inputshinL:	nop			; enforce SCL low and data setup;	nop			;;	nop			;	setb	SCL		; raise clock;	nop			; enforce SCL high	nop			;	mov	c, SDA		; input bit	rlc	a		; move bit into byte	clr	SCL		; drop clock	jnc	shinL		; next bit	retACK:	; Clock out an acknowledge bit (low).	; SCL expected low on entry. Return with SCL, SDA low.	clr	SDA		; ACK bit	nop			; enforce SCL low and data setup;	nop			;	setb	SCL		; raise clock;	nop			; enforce SCL high;	nop			;	nop			;	nop			;	clr	SCL		; drop clock	retNAK:	; Clock out a negative acknowledge bit (high).	; SCL expected low on entry. Return with SCL low, SDA high.	setb	SDA		; NAK bit	nop			; enforce SCL low and data setup;	nop			;	setb	SCL		; raise clock;	nop			; enforce SCL high;	nop			;	nop			;	nop			;	clr	SCL		; drop clock	ret	.section reset_device, #alloctestI2Cpresent:	clr	I2Cpresent	mov	dptr,#0		;offset of ID in I2C EEPROM	lcall	read_random	cjne	A,#'@',notI2C	lcall	read_current	cjne	A,#'B',notI2C	lcall	read_current	cjne	A,#'C',notI2C	lcall	read_current	cjne	A,#'L',notI2C	setb	I2CpresentnotI2C:;;;&&& end of modul I2C &&&;	;; END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -