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

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
pphamtuan
Thu Oct 09 2014, 03:58 AM

Online

Guests: 46, Members: 0 ...

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

Members: 37531
Newest member: pddung93
Forum Activity
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 11:19 AM
Use a 8bit shift register with latch control (e.g....
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 11:17 AM
You can use the attach file button to attach the i...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 11:12 AM
I think you need to first check your code what exa...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 09:11 AM
I don't have resources to show this but if I get t...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 09:09 AM
Initialise _data in the header file to some value ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 09:07 AM
8051 is slow in doing what? Well I don't think 805...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Oct 20 2014, 09:00 AM
I also checked in Intel manual however I could not...
msideal Avatar
Posted By msideal
Sun Oct 19 2014, 07:25 PM
Is there anyone who could help me in solving this ...
jona Avatar
Posted By jona
Sun Oct 19 2014, 02:03 AM
Has anyone used this device? I was able to get the...
kirangowle Avatar
Posted By kirangowle
Sat Oct 18 2014, 03:49 PM
Thanks ExperimenterUK