Assembly Implementation

As discussed earler, we can implement I2C by bit-banging on 8051 using any two pins. Here in implementation below we have used P0.0 for SDA (serial data) and P0.1 for SCL (serial clock).

;***************************************
;Ports Used for I2C Communication
;***************************************
	sda equ P0.0
	scl equ P0.1

;***************************************
;Initializing I2C Bus Communication
;***************************************
i2cinit:
	setb sda
	setb scl
	ret

;****************************************
;ReStart Condition for I2C Communication
;****************************************
rstart:
	clr scl
	setb sda
	setb scl
	clr sda
	ret

;****************************************
;Start Condition for I2C Communication
;****************************************
startc:
	setb scl
	clr sda
	clr scl
	ret

;*****************************************
;Stop Condition For I2C Bus
;*****************************************
stop:
	clr scl
	clr sda
	setb scl
	setb sda
	ret

;*****************************************
;Sending Data to slave on I2C bus
;*****************************************
send:
	mov r7,#08
back:
	clr scl
	rlc a
	mov sda,c
	setb scl
	djnz r7,back
	clr scl
	setb sda
	ret

;*****************************************
;ACK and NAK for I2C Bus
;*****************************************
ack:
	clr sda
	setb scl
	clr scl
	setb sda
	ret

nak:
	setb sda
	setb scl
	clr scl
	setb scl
	ret

;*****************************************
;Receiving Data from slave on I2C bus
;*****************************************
recv:
	mov r7,#08
back2:
	clr scl
	setb scl
	mov c,sda
	rlc a
	djnz r7,back2
	clr scl
	setb sda
	ret
		

C Implementation

#define SDA P0_0
#define SCL P0_1

void I2CInit(){
	SDA = 1;
	SCL = 1;
}

void I2CStart(){
	SCL = 1;
	SDA = 0;
	SCL = 0;
}

void I2CRestart(){
	SDA = 1;
	SCL = 1;
	SDA = 0;
}

void I2CStop(){
	SDA = 0;
	SCL = 1;
	SDA = 1;
}

void I2CAck(){
	SDA = 0;
	SCL = 1;
	SCL = 0;
}

void I2CNak(){
	SDA = 1;
	SCL = 1;
	SCL = 0;
}

unsigned char I2CSend(unsigned char Data){
	 unsigned char i, ack_bit;
	 for(i=0;i<8;i++){
	 	SCL = 0;
		if ((Data & 0x80) == 0)
			SDA = 0;
		else
			SDA = 1;
		SCL = 1;
		Data<<=1;
	 }
	 SCL = 0;
	 ack_bit = SDA;
	 SCL = 1;
	 SCL = 0;
	 return !ack_bit;
}

unsigned char I2CRead(){
	unsigned char i, Data=0;
	for(i=0;i<8;i++){
		SCL = 0;
		SCL = 1;
		if(SDA)
			Data |=1;
		if(i<7)
			Data<<=1;
	}
	SCL = 0;
	SDA = 1;
	return Data;
}
		

Using the above implementation is really easy, next part uses an example of interfacing DS1307 with AT89C51 to read RTC registers (0x00 to 0x07).

Conversation

Tue Aug 19 2014, 04:51 PM
allen ndumu
please i will like to know if the problem of speed control of dc motor using keypad and l298 was if yes can i have the complete project send it to the address i really need to see it
Tue Aug 19 2014, 10:47 AM
Amit Bisht
sir i want to have some trending projects on which i could work related to ATmega 16 microcontroller
ExperimenterUK
Sat Aug 16 2014, 04:16 PM
@diligent . create a thread
Sat Aug 16 2014, 03:17 PM
diligent
in assembly langauage programming..
Sat Aug 16 2014, 03:14 PM
diligent
will anyone help me?
Thu Aug 14 2014, 10:24 PM
Amirudin Tone
i help, i need liberary IC 4047 in the proteus. thanks
ExperimenterUK
Thu Aug 14 2014, 03:59 PM
@NARAYANA are you getting an error ?
Tue Aug 12 2014, 11:47 PM
NARAYANA
i am not able to get reg please provide me registration link
Tue Aug 12 2014, 10:32 AM
saloni
sir I was needing literature survey of''ANDROID CONTROLLED SPY ROBOT USING MICROCONTROLLER"
ajay_bhargav
Mon Aug 11 2014, 12:30 PM
@nisargpatel and @NARAYANA please post questions in forum.

Downloads

Comments

hasmukhjain
Wed Aug 20 2014, 06:28 AM
rablo
Tue Aug 19 2014, 10:10 AM
faizhameed
Tue Aug 12 2014, 10:39 PM
ind3siszive
Sun Aug 10 2014, 01:05 PM
snaxxx68
Sat Aug 09 2014, 11:06 AM
[email protected]
Sat Aug 09 2014, 08:58 AM
rajiabdul
Mon Aug 04 2014, 01:31 PM
giash
Sat Aug 02 2014, 04:44 AM

Online

Guests: 47, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 37496
Newest member: hasmukhjain
Forum Activity
hasmukhjain Avatar
Posted By hasmukhjain
Thu Aug 21 2014, 01:50 AM
for that try to add a variable wch can save your p...
jayachandra Avatar
Posted By jayachandra
Wed Aug 20 2014, 08:11 AM
thanq for reply @hasmukhjain,actually my applicati...
hasmukhjain Avatar
Posted By hasmukhjain
Wed Aug 20 2014, 06:40 AM
Hi Mr jayachandra,I feel ur if statement for switc...
nisargpatel Avatar
Posted By nisargpatel
Wed Aug 20 2014, 01:29 AM
see we are measuring electrical parameters by CT, ...
jayachandra Avatar
Posted By jayachandra
Tue Aug 19 2014, 08:22 AM
HELLOim facing problem with eeprom, i wrote a prog...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Aug 16 2014, 04:11 PM
You will have to be more specific about what you a...
nisargpatel Avatar
Posted By nisargpatel
Sat Aug 16 2014, 06:03 AM
Anyone have idea related to AC/DC current measurem...
sairfan1 Avatar
Posted By sairfan1
Wed Aug 13 2014, 01:48 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Aug 12 2014, 04:34 PM
Usually its better to have a LED driver circuit in...
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Aug 12 2014, 04:01 PM
There are so many apps available. Did you try a se...