• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to implement an I2C into 8051 using Assembly Language?


New Member
I am currently on my Final Year Project named the Ultra Sonic Range Finder(SRF08). The project required me to capture the height of a human being into the 8051 MicroController and display the height out on a 16X4 LCD screen. Problem is that I was not given an I2C-bus controller and therefore was asked to implement I2C on the 8051 in order to interface with the SRF08.

As my knowledge of Assembly Language is very poor, I need help of getting a program that can implement an I2C into the MicroController. Anyone could help me with this, I will be very grateful.

Thanx alot! Please help me....


New Member
Come and take a look at Richard Steven Walz's ftp site:


also visit his home page: http://www.armory.com/~rstevew/

Pay attention to the file ftp://ftp.armory.com/pub/user/rstevew/8051/8051-bbs.lst. It lists some I2C related sample programs for the 8051.

One late night debugging session years ago, I was out of ideas trying to get a digital video demo kit from Philips up and running. Invented by Philips, it used I2C. Some files to make the demo kit work were missing and I chanced upon his site. Lo and behold, there was enough documentation from the site to save the day.


New Member
My project does not allow me to use extra hardware devices to form the I2C bus. I only can use assembly language to program the microcontroller to work as an I2C to receive the information scanned from the Ultrasonic Range Finder(SRF08 which uses I2C interface).

I got a program did by the previous student but I don't quite understand how it works and he told me that it was already 95% done... i try to attach the asm file with this message.

Pls help me with it. Thanx for replying me...



New Member
Elaine84 , how does he know it was 95% done ? ..
if it isnt done it may be only 50% done..
anyway , i would suggest you test the I2C ,in sections if possible, till you know what part is working , and what part is not...


New Member
He told me 95% because his supervisor gone through it with him or maybe is made up by himself. But As my knowledge is so low, I need very long time to intepret what he is doing in that program, somemore i never heard of I2C before. Moreover, I do not know whether that program made by him does or does not implement the I2C on the 8051 microcontroller.

A big headache to continue other person's work. Pls help me with this project. Thank you very much!!


New Member
Thanx.... I will look into it.

Anyway, i need suggestions that in what ways/methods can i implement the I2C on 8051. For example, is it possible to use Timer Modes in 8051?
Although i read through the resources, I am still not sure how to start the program.


New Member
Can anyone please help me with the project?? Till now I'm still lost... Still have not started any programming. I read through infomations of I2C and the Ultrasonic Range Finder(SRF08) quite a few times but still could not figure out how both communicates and how to receive/transmit to/from SRF08.

My project supervisor keep asking on my progress and he says the previous student's program may not be correct. I tries to ask him for help but he always refer me to read on I2C. I need help on which method should i use, whether a Timer Mode or what.


Active Member
You don't require to use any Timer for I2C implementation using software bit-banging method.
Software examples of I2C in both C and assembly are given above.
This if how you read/write any I2C device.
1) Send a start condition
2) Send device address (this is specific to all I2C devices) along with read/write bit.
3) Send memory/register address to read/write if required.
4) Perform read/write operation byte by byte along with appropriate ACK/NACK.
5) Send stop condition.


New Member
Only follow the steps you gave is good enough to transmit/receive data to/from the SRF08? How about the SCL clock signal, no need to generate from the microcontroller?

As my supervisor told me that I must know when to send in the clock pulse for each bit of the data to be sent over. The time for each clock pulse should be how long?


New Member
it's been said before, and I'll say it again.

you obviously don't understand the I2C protocol. you need to learn about it before you even think of trying to implement it in software.

yes, you need to manipulate both the SDA and SCL lines when communicating, and both of them have to be done in specific manners with proper timing. the SCL line is not just a simple continuous clock signal, it is used as both a clock during data transfer, as well as in start/stop conditions, acknowledge, etc.

it will be MUCH easier to simply READ about the protocol than trying to ask people on this forum about every single detail. we are of course willing to help, but if you want us to spoon-feed you every single aspect of I2C communication, when there are plenty of resources out there for you to use if you just take the time to read them, that's a bit much.

here's a document titled "the I2C bus and how to use it (including specifications)"

that will tell you absolutely everything you need to know about the protocol, and then you will have a solid base from which to start writing your program. I just wrote a full set of I2C routines for the PIC microcontroller yesterday for one of my own projects, with ONLY that document for reference, so I KNOW there's enough in there to get you going.


New Member
Thanx for your advice anyway... I have been to that pdf file before you have given me. I understand that specific clocking signal from different situation as you mention.

Acutually I am not sure about the duration of each clock pulse in order to send bit by bit data to the SRF08. How do I find out how long the Data Bit need to be stable on the SDA Line before one clock pulse has gone in order to send the bit over?

As for the Acknowledgement, its clock signal is always sent by the master. Is the SDA Line being force to low by the SRF08 or the master should set it to low?


New Member
the answers to those questions are in the document as well :roll:

page 3-9 explains the acknowledge condition... the master releases the SDA line and the device must pull it low.

and if you look on page 3-8, it shows that the SDA line can be changed during clock low, and should always be stable/valid for the entire duration of the clock pulse. so as long as the SDA line is stable before you output the clock pulse, you're okay. or if you really want numbers, just look on page 3-22, and you will see that tSU:DAT (data set-up time) is 250 nS.


New Member
ok thanx...

Anyone is fimilliar with the SRF08 Range Finder? I got doubts to clear after reading its documentation. About the Echo registers, must the microcontroller need to read all the 17 echos to take in one result? or the 1st echo will have the result I want which is the height of one person?

To check for completion of ranging, write to the Location 0 which is the software revision will do?

1)Slave address used - E0H
2)Write into Command Register - 00H
3)Ranging Mode - 51H

What else must i write to the slave? I do not need to change all the other settings like the address, analogue gain etc.


New Member
I still having problems with my assembly program. To read from the slave which is the SRF08 range finder, how I find out how many bytes of data it will sent to the micro-controller?

I have read through the SRF08 documentations and I know that I onli need to read the 1st echo high & low byte which is at location 2 & 3. How do I tell the slave that I want only this two result after master transmit the address for read??

Anyone, please help me with it... Thanx!!!

Latest threads

EE World Online Articles