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

ajay_bhargav
Mon Oct 20 2014, 11:22 AM
@skrishna, please post in forum so we can help you better.
Tue Oct 14 2014, 01:45 PM
skrishna
how do i write an ALP equivalent 7segment display interfacing embedded c program (both 8051 microcontroller)
Sat Oct 11 2014, 06:41 PM
vaidehi
want details of seven segment LED display common cathode ( IC CD 4026)
Fri Oct 03 2014, 12:52 PM
Narsim
Rickey you inspired me a lot ... I am also planing like you ...
ExperimenterUK
Wed Oct 01 2014, 09:33 PM
We're back.. we had a bug, but posting is back to normal
ExperimenterUK
Sat Sep 13 2014, 04:34 PM
jaimin1212,Pimpalkhare Create a thread for your question
jaimin1212
Sat Sep 13 2014, 06:35 AM
i want to make self balancing robot using MPU6050 using atmega8.. please help to get data of gyro and accelerometer from this sensor through atmega8.
Thu Sep 11 2014, 01:00 PM
Sunil Pimpalkhare
Hello sir, I am trying to make Wearfin project https://www.youtube.com/watch?v=iZ6PVBvQf-g so can u help me in teaching how to built the program???
Thu Sep 11 2014, 06:21 AM
hariom panday
plz hlp me in my project
iec.naga
Tue Sep 09 2014, 05:19 AM
spi

Downloads

Comments

hiswe
Thu Oct 23 2014, 04:18 PM
pddung93
Sun Oct 19 2014, 01:56 PM
msideal
Thu Oct 16 2014, 08:48 AM
robotgirl
Tue Oct 14 2014, 06:23 PM
BRV64
Mon Oct 13 2014, 05:48 AM
menadjelia
Mon Oct 13 2014, 02:47 AM
kulbhushan
Sun Oct 12 2014, 12:20 PM
HarrySid
Fri Oct 10 2014, 05:15 AM

Online

Guests: 42, Members: 0 ...

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

Members: 37532
Newest member: hiswe
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 24 2014, 08:55 PM
Try holding STB high all the time.You might as wel...
msideal Avatar
Posted By msideal
Fri Oct 24 2014, 07:14 PM
Assembly Language Code:.INCLUDE "M16DEF.INC" LDI R...
msideal Avatar
Posted By msideal
Fri Oct 24 2014, 07:12 PM
Hi Friends,I changed my approach and now i'm using...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Oct 22 2014, 03:22 AM
Okay, it seems to be a simple serial connection.Th...
jona Avatar
Posted By jona
Tue Oct 21 2014, 10:29 PM
I found this slightly different data sheet on a pu...
majoka Avatar
Posted By majoka
Tue Oct 21 2014, 06:35 PM
after this Accept-language:en-us\r\n...
majoka Avatar
Posted By majoka
Tue Oct 21 2014, 06:33 PM
@ aksharAT89C51AC3 can bebut the final decision wi...
majoka Avatar
Posted By majoka
Tue Oct 21 2014, 06:29 PM
@ Okechukwubefore starting this project you should...
majoka Avatar
Posted By majoka
Tue Oct 21 2014, 06:18 PM
@ kirangowleyou have to set the microphone level a...