Jump to content

Recommended Posts

I am attempting my first I2C master/slave program. I have read almost everything I can find to help, but I'm confused on something (well, really many things, but I'll just start with this).


I have been reading the master/slave example on the sourceboost example downloads ( http://www.sourceboost.com/Products/BoostC/ExampleCode.html )


I have read the .h files and understand, I think, what's going on. What I don't get is the internal address of the slave 16f877. The code I am referring to is this:


// Write to the External 16F877A device
// s is a pointer to the string to be written to the EEPROM
// HW_address is the hardware address of the i2c device
// ic2_addr is the target internal address within the External device
// count is the number of bytes to be written starting at i2c_addr
void write_XFF(char *s, char HW_address, unsigned short i2c_addr, unsigned short count)
short i;

HW_address <<= 0x01; // Shift the 7-bit address Left by 1
HW_address &= 0xFE; // LSB = 0 for address/data write operation

i2c_write(HW_address); // send XFF i2c address

i2c_write(i2c_addr >> 8); // send XFF internal HIGH address
i2c_write((char) i2c_addr & 0x00ff); // send XFF internal LOW address

// XFF write loop
for (i=0;i<count;i++)

What is meant by the "the internal address of the External device"? When I read/write to the slave 16f877 am I writing to an EEPROM address?


I would rather learn to use the registers first so that I can understand what the boostc library is doing, but I haven't found a simple master/slave program that I could understand. If anyone knows of a reall really simple example that actually works, I would love to see it.

Share this post

Link to post
Share on other sites

Should I take this silence as a statement that no one knows what this program is doing or how i2c really works? I know all this stuff is magic and if you let the magic smoke out it won't work at all, but ....

Share this post

Link to post
Share on other sites



I don't think so.

Maybe the silence simply means that everybody noticed that you are trying to set up a comunication using a protocol that you don't know at all.

I would suggest you study how I2C works before trying to make, or even copy/paste, a program that uses it.


BTW all that stuff about internal versus external addresses only aplies to some devices.

As a matter of fact most of the addressing details vary from one device to another, so, besides knowing how I2C works, you also have to know the specifics of the device you are trying to access.




Best regards


Share this post

Link to post
Share on other sites

Amsterdam, 6th dec 2013/7th dec 2013

Hello Mityeltu,


In my opinion we live in a 8-bit hexadecimal world. So why bothering about confusing 7 bits addresses which have to be converted to 8 bits by shifting every time? I think it's easier to consider the slave as having one constant (unchangeable) WriteToSlaveAddress (even) and one constant ReadFromSlaveAddress (odd, namely WriteToSlaveAddress + 1). So two addresses of 8 bits each.

A useful document that could help would be:




Another absolute must is the AN734.pdf of Microchip available at:




especially the states on page 5.


The third (variable) address of the slave would be the place (register) from which you will read from or write to firstly. However, some devices send back their ReadFromSlaveAddress as a bonus firstly after which follows the data.

Somehow you should try knowing what is happening inside your master-controller. My humble method is outputting data and/or values of registers and/or remarks like my own invented error-messages, to the serial port TX after which comes a MAX232 IC that converts and reverses the 5 Volt TTL level to -10 .. +10 Volt RS232 level after which comes the input of the serial port of a PC that runs a simple Pascal or C program that displays anything in a readable shape on a monitor.

A master could be, indeed, the 16F877 and a slave (if you want to build one) could be the 16F88. I think that the last one is not very appropriate as a master (in a convenient language like Sourceboost C) unless you like bit-banging.

Hopefully you have an oscilloscope, I2C can be hard.


Regards, Remi

Edited by Remi Sans Famille

Share this post

Link to post
Share on other sites

Your content will need to be approved by a moderator

You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.