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).

Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jul 23 2014, 03:45 PM
What PIC is it ?Does your programmer see the PIC a...
pravin_35 Avatar
Posted By pravin_35
Wed Jul 23 2014, 11:53 AM
Can the Pickit3 read the Pics signature bytes and ...
diana1234 Avatar
Posted By diana1234
Wed Jul 23 2014, 07:20 AM
@ Ajay sirMay be there is power supply problem i t...
pravin_35 Avatar
Posted By pravin_35
Wed Jul 23 2014, 04:29 AM
Hi, Need library or sample code for PIC16f882,, Do...
Anup Teke Avatar
Posted By Anup Teke
Wed Jul 23 2014, 03:28 AM
Sir,I was doing like thisMCP3901_CS = 0;WriteSPI1(0x01);TempData = ReadSPI1( );TempData <<= 8;TempData |= ReadSPI1( );TempData <<= 8;TempData |= ReadSPI1( );MCP3901_CS = 1;and my problem solved by doing like thisMCP3901_CS = 0; WriteSPI1(0x01); TempData = ReadSPI1( ); TempData <<= 8; MCP3901_CS = 1; MCP3901_CS = 0; WriteSPI1(0x03); TempData |= ReadSPI1( ); TempData <<= 8; MCP3901_CS = 1; MCP3901_CS = 0; WriteSPI1(0x05); TempData |= ReadSPI1( ); MCP3901_CS = 1;
pravin_35 Avatar
Posted By pravin_35
Wed Jul 23 2014, 12:57 AM
Can the Pickit3 read the Pics signature bytes and ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jul 22 2014, 04:24 PM
Can the Pickit3 read the Pics signature bytes and ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Jul 22 2014, 04:16 PM
Does the DS1307 keep time ?That is, if you set it ...
pravin_35 Avatar
Posted By pravin_35
Tue Jul 22 2014, 01:05 PM
hi... i have done with a simple blinky program wi...
pravin_35 Avatar
Posted By pravin_35
Tue Jul 22 2014, 07:23 AM
Autoscale does not know what settings you keep on ...

Conversation

Wed Jul 23 2014, 05:49 AM
R.ABHISHEK
sir,pls message on microcontroller 8051 assambly language new program
Wed Jul 23 2014, 05:47 AM
R.ABHISHEK
sir,pls message on 8051 microcontroller assambly language program new quastions
ajay_bhargav
Tue Jul 22 2014, 09:18 AM
@Pragya Android application is taken from google play. Only Author has its source with him. Application is only used in this project.
Tue Jul 22 2014, 04:58 AM
Pragya
plz send me 8051 code and android code for android based device controller via Bluetooth .Here in this project I am using Hc-06 Bluetooth module
Thu Jul 10 2014, 09:57 AM
Caleb
plz someone mail me the source code for that project
Thu Jul 10 2014, 09:57 AM
Caleb
http://www.8051projects.net/download-d188-control-your-pc-tv-remote.html
ExperimenterUK
Tue Jul 08 2014, 03:25 PM
Post questions in the forum.. this area is for chat
Tue Jul 08 2014, 08:15 AM
P.Panchanyam
Sir i want some new ideas for my final year projects ,which would help for our soceity in future
Tue Jul 08 2014, 01:39 AM
jasvinder
Dear Sir Ref. to the discussion held with under sign, the detail is as under--- We are in weighing scale field since 1986. We are assembling the scales by purchasing the material from out source ( From India and other countries). Now we decide to make our own Card in our decided shape. So we want to develop a universal card , which we can make all types of scales of up 100000 counts with 7 segment LED Display . Also with other features like Printing, Counting, RS232 Etc. And Three Pass words at different levels. And we can edit the Softwear. What else you can provide in this field, also mention. We will pay you the development , softwear and testing charges. And up-gradation charges at time to time. Our web site is modern-scales.com Kindly revert.
Mon Jul 07 2014, 09:20 AM
tadashige matuda
Please tell me about pic instruction ADDWF fileName,f ; fileName=fileName+W. Why twice memory access (1)fileName+W,(2)fileName=fileName+W, be able in one instruction cycle. For MOVWF fileName ; memory access once in an one instruction cycle.

Downloads

Comments

vinosentinal
Mon Jul 21 2014, 08:28 PM
niks_here
Sun Jul 20 2014, 05:38 PM
athiramsankar
Sun Jul 20 2014, 12:00 PM
salemalqahtani
Sun Jul 20 2014, 12:55 AM
nagarajan.electro
Sat Jul 19 2014, 12:49 AM
praru
Sat Jul 12 2014, 07:51 AM
nmcleod1993
Thu Jul 10 2014, 09:02 AM
abdulmannan39
Thu Jul 10 2014, 05:50 AM

Online

Guests: 41, Members: 0 ...

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

Members: 37479
Newest member: vinosentinal