;************************************************************************
;		I2C Protocol Library
;
;	Written by:	Ajay Bhargav
;	Email:		bhargav.ajay@gmail.com
;	URL:		www.rickeyworld.tk
;	Description:	I2C Subroutines for I2C communication
;			these subroutines covers start, stop, send, recieve
;			etc. They can be used by anyone and can be changed
;			and distributed.
;
;	IMPORTANT:	Some I2C devices may be slow so you need to add one
;			or two nops where ever it required.
;
;	NOTE:		This library works ony with single master multi-slave 
;			configuration. If you find any problem mail me.
;************************************************************************


;************************************************************************
;		Ports Used for I2C Communication
;************************************************************************
	sda equ P0.0
	scl equ P0.1

;************************************************************************
;		Start Condition for I2C Communication
;************************************************************************

startc:
	clr scl
	setb sda
	nop
	nop
	setb scl
	nop
	nop
	clr sda
	ret

;************************************************************************
;			Stop Condition For I2C Bus
;************************************************************************

stop:
	nop
	nop
	clr sda
	setb scl
	nop
	nop
	setb sda
	nop
	nop
	clr scl
	ret

;************************************************************************
;		Sending Data to slave on I2C bus
;		       with Acknowledgement
;************************************************************************

send:
	mov r7,#08
back:
	clr scl
	nop
	nop
	nop
	rlc a
	mov sda,c
	setb scl
	nop
	nop
	nop
	clr scl
	nop
	nop
	nop
	djnz r7,back
	setb sda
	setb scl
	nop
	nop
	nop
	nop
	clr scl
	nop
	nop
	nop
	ret

;************************************************************************
;		Receiving Data from slave on I2C bus
;		       with Acknowledgement
;************************************************************************

recv:
	mov r7,#08
back2:
	setb sda
	setb scl
	nop
	nop
	nop
	mov c,sda
	rlc a
	clr scl
	nop
	nop
	nop
	djnz r7,back2
	setb sda
	clr scl
	nop
	nop
	nop
	clr sda
	setb scl
	nop
	nop
	nop
	clr scl
	nop
	nop
	nop
	mov @r0,a
	inc r0
	ret