Continue to Site

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.

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

i2c communication

Status
Not open for further replies.

Angy

New Member
I'm working on the i2c Communication interfacing my board to the display board and I'm really getting confused with it.
Can someone suggest some easy to read materials for beginners on i2c communication ?

Thanks in advance for your suggestions.
 
Are you doing it in software or hardware? Master or slave? What device are you trying to talk to and with what processor?

Mike.
 
processor is 16f946 chip
I use SAA1064, 4digit led driver with i2c interface.
i'm trying to use the i2c master mode.
thanks!
 
if you tie ADR pins to VEE then slave address of the SAA1064 is 0x70 ... some example of use would be (mikroC):

Code:
const char  SS[10] {63,6,91,79,102,109,125,7,127,111}; 
...

    I2C_Init(100000);
    I2C_Start();
    I2C_Wr(0x70); //slave addr
    I2C_Wr(0); //instruction byte
    I2C_Wr(0x17); //control byte
    I2C_Wr(SS[3]); // 4td digit (show 3)
    I2C_Wr(SS[2]); // 3rd digit (show 2)
    I2C_Wr(SS[1]); // 2nd digit (show 1)
    I2C_Wr(SS[0]); // 1st digit (show 0)
    I2C_Stop   

...

hope this helps, any compiler you use should have similar library functions...
 
Last edited:
If you want ASM code, the "Microchip Application Maestro" (can be installed as part of MPLAB installation) can create either polled and interrupt driven I2C master code.
 
Arhi:

Can you please explain the following:

SAA1064 is 0x70 = the slave address

Thanks.
 
angy, look at the data sheet of the device, depending on the voltage you get to ADR pin the device will have different slave address. As 3V0 already stated, I2C communication is master/slave, uC is master and it sends "requests" to the slaves. The first byte sent is always slave address + RW bit (0 for write and 1 for read).

The slave address of your device (quote from datasheet):
Four different slave addresses can be chosen by connecting ADR either to VEE, 3/8 VCC, 5/8 VCC or VCC. This results in
the corresponding valid addresses HEX 70, 72, 74 and 76 for writing and 71, 73, 75 and 77 for reading.

So, if you connect ADR pin to VCC then the example I shown would be:
Code:
const char  SS[10] {63,6,91,79,102,109,125,7,127,111}; 
...

    I2C_Init(100000);
    I2C_Start();
    I2C_Wr(0x76); //slave addr
    I2C_Wr(0); //instruction byte
    I2C_Wr(0x17); //control byte
    I2C_Wr(SS[3]); // 4td digit (show 3)
    I2C_Wr(SS[2]); // 3rd digit (show 2)
    I2C_Wr(SS[1]); // 2nd digit (show 1)
    I2C_Wr(SS[0]); // 1st digit (show 0)
    I2C_Stop   

...

clear?
 
Btw, this means that on the same bus you can have 4 SAA1064 drivers. Each have to have different slave address so you can address each one from micro controller. This means that using only 2 pins on the microcontroller you can have 16 digits (using 4x SAA1064)

I attached the Philips I2C manual .. there's no better document to read I2C from
 

Attachments

  • I2C from phillips.pdf
    4.1 MB · Views: 414
Last edited:
Thanks a lot Arhi, I got a good understanding of the theory.

I'm writing the code in assembly.I have two slaves ie two displays connected to the master.Now for starters I'm just trying to talk to one of the device. To display 000 On one of the displays, do you have any code snippets or examples?

Thanks.
 
sorry, I stopped using ASM long time ago, I just do not think that time that takes to write it, time that takes to "support/administer" it, and, god forbit, port to another platform is worth the few % of speed up you will get in comparison to some higher level ... so, imho, asm is just not productive enough.



you can get free microchip's C18 and C30 (student edition) that will cover pic18F*, pic24f* and pic30F* ... there's actually no use for pic16F* and older ones as they are at same price or even more expensive then 18F ones and have less power/features.

If you really want to go with 16F, there are still bunch of good compilers that are free (with few limitations like 2K limit for code). I can suggest mikroElektronika | Solutions for the embedded systems (mikroC) or CCS, Inc. - CCS C Compilers (pic C). I never tried but heard extremly nice words about SourceBoost Technologies (boost C) - all these compilers cover 18F too, but imho, if you want to go with 18F (and I do recommend it) microchip C18 is right way to go.

If you are still sure you want to go ASM style, nigel's tutorial 6 covers the i2c communication in asm (I already posted that link few messages ago)
 
  • Like
Reactions: 3v0
Thanks for the information Arhi. I appreciate it.
And I dont have any choice but to go with assembly cause most of the code is already written in assembly.
 
Most C compilers will allow you to mix asm and C. You can write some functions in asm and some in C and mix all thet up into final project :D but if you have to / want to go with asm, then use Nigel's tutorial 6 as example on how to drive i2c. Nigel's tutorials are easy to understand and easy to follow. If you have some questions about tutorial 6, post after you read the txt and go trough the code.
 
I have a question for you.
I have the i2c device bits SCL,SDA set as 3 and 4
I have this routine when I actually start sending the Bit (start bit) from the master to the slave.( I know This routine works for 16C micro.)

BANKSEL TRISC
bsf TRISC,SDA ; Set SDA for input
bsf TRISC,SCL ; Clock high
bcf STATUS,RP0
btfsc PORTC,SCL ; Skip if SCL is low
goto BIT1
goto error
Now on the watch window I see TRISC=0X18,PORTC=0X08 after it executes the third
line on the code. (here RC3=SCL,RC4=S DAFROM DATASHEET)

Now for 16F micro (here RC6=SCL,RC7=SDA FROM DATASHEET)
I see TRISC=0XC0,PORTC=0XCO on the watch window after it executes the third line on the code which eventually gives me a acknowledge error.

Can anyone tell me what I'm doing wrong?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top