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

ExperimenterUK
Thu Nov 20 2014, 06:00 PM
@Mehboob.. I don't think you can do that.
Sat Nov 15 2014, 06:41 AM
Mehboob
Pls can you send me code in c of PWM generation using UART/serial port in 8051.
Fri Nov 14 2014, 02:51 PM
mohammad aqil
NEED A BOOK ON MICROPROCESSOR AND INTERFACING BY D.V.HALL. RIGHT NOW, I SHALL BE VERY THANK FULL TO U....
Thu Nov 13 2014, 02:15 PM
m
please code hex project avr blood pressure monitor
Thu Nov 13 2014, 05:56 AM
Lambo
hi mate, whats the ajay-bhargav? tell us more pls
ExperimenterUK
Wed Nov 12 2014, 12:56 AM
@tmtm000.. have you been able to join ?
ExperimenterUK
Wed Nov 12 2014, 12:54 AM
@Alice..become a member,then send a PM to ajay_bhargav the site owner.
Tue Nov 11 2014, 03:58 AM
Abhishek Bansal
Thanks a lot for your informative tutorials!
Mon Nov 10 2014, 09:03 AM
Alice
Hello, This is Alice from a PCB rototype manufacturer. Our company is interested in your website contents, and wonder if we can advertise our company with payment on your website. There are two ways to advertise: first, if we an get a instant quote section on your main site columns; second, whether we can get a advertising section if you have any avaliable one. If any of above ways avaliable, please contact me with your detailed scheme and advertising charge. Thanks. Alice
Sat Nov 08 2014, 11:58 AM
Mohamed_123sd
Signals and Systems, 2nd Edition by Alan V. Oppenheim Alan S.willsky with S.Hamid Nawab?

Downloads

Comments

VikiPeter
Tue Nov 18 2014, 09:13 AM
gentlboy
Sun Nov 16 2014, 11:13 PM
philipeastbourne
Wed Nov 12 2014, 05:32 AM
astra91
Mon Nov 10 2014, 08:11 AM
francise
Thu Nov 06 2014, 02:55 AM
ashbir
Tue Nov 04 2014, 11:13 PM
shruthig
Tue Nov 04 2014, 10:50 AM
sk8boardboy
Mon Nov 03 2014, 11:20 AM

Online

Guests: 72, Members: 0 ...

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

Members: 37547
Newest member: VikiPeter
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Nov 20 2014, 05:56 PM
You set the on/off times here as 47%float PWMcom...
vijay.parmar1992 Avatar
Posted By vijay.parmar1992
Thu Nov 20 2014, 04:06 AM
Right now i am studying Bootloader.I want to writ...
Max.otto Avatar
Posted By Max.otto
Wed Nov 19 2014, 03:11 PM
How can i get PWM on-off time..?pls check the atta...
Max.otto Avatar
Posted By Max.otto
Wed Nov 19 2014, 03:05 PM
I am using PSoC 5LP.pls check the attached design ...
ankitbajaj01 Avatar
Posted By ankitbajaj01
Wed Nov 19 2014, 01:49 PM
Thanks for the prompt response, I will try it out ...
engr1931 Avatar
Posted By engr1931
Wed Nov 19 2014, 11:45 AM
this one sir! i attached the whole project! ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 19 2014, 11:36 AM
I am not able to see any attachments. Please zip y...
engr1931 Avatar
Posted By engr1931
Wed Nov 19 2014, 11:33 AM
sir can i ask help in adding more number digits th...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 19 2014, 10:44 AM
Set duty cycle to 50% you will get equal on/off ti...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Nov 19 2014, 10:41 AM
Which controller are you using?