MikroC I2C problem

Thread Starter

khatus

Joined Jul 2, 2018
95
Hello guys i can't understand some line in mikroC i2c manual.

Note : Calculation of the I²C clock value is carried out by the compiler, as it would produce a relatively large code if performed on the library level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.



Can anyone explain the above line??
 

AlbertHall

Joined Jun 4, 2014
12,344
Before calling I2C1_Init you need a line declaring the clock frequency and it must be declared as a constant (your program cannot change it while running) or you could include the literal value in the call like this:
I2C1_Init(10000); // for a 10MHz clock
 

Thread Starter

khatus

Joined Jul 2, 2018
95
How do you determine 10000 for 10MHz clock??
Also the manual says
Initializes I²C with desired clock (refer to device data sheet for correct values in respect with Fosc).

which device is referred in the above line? master(pic micro controller) or the slave??
 

Thread Starter

khatus

Joined Jul 2, 2018
95
If you are writing the code for both the master and the slave then both pieces of code will need similar delarations.
And How do you determine 10000 for 10MHz clock??

i mean the value inside the function I2C1_Init(100000); should always be 100000?? Can i use different value??
 
Last edited:

BobaMosfet

Joined Jul 1, 2009
2,110
And How do you determine 10000 for 10MHz clock??

i mean the value inside the function I2C1_Init(100000); should always be 100000?? Can i use different value??
In rereading this, it's a bit hard to tell. They either want you to look at the datasheet for the MCU and get a value to submit to i2cinit call based on your MCU clock speed, or they want you to enter your MCU clock speed.

Why they say the calculation is lengthy is a mystery, it isn't.
 
Last edited:

BobaMosfet

Joined Jul 1, 2009
2,110
That does not sound right to me :(

I would have said that the value should be the desired I2C clock rate, assuming that the PIC is the master device.

100kHz I2C clock is a popular choice that works with a big variety of I2C slave peripherals and is not so fast that PCB layout and similar factors become an issue.

It is what the I2C slave devices require that would be the first thing to take into consideration when choosing I2C clock rate.
@hexreader- ignore that statement I rewrote it, go back to my original entry.
 

Thread Starter

khatus

Joined Jul 2, 2018
95


In the above picture it says if Both SCL and SDA are connected to a positive power supply voltage via resistors. This means that when the bus is free, both lines are high.

My question is
1. if i connect a slave device to SDA and SCL line does the slave pulled the lines(SDA & SCL) to low??
2. The I2C1_Start() command in mikroc compiler determines if I²C bus is free or not. what does it mean by free???


3. What is the use of I2C1_Repeated_Start() routine??I can't understand what it mean by repeated START signal?? why it is required ??
 

BobaMosfet

Joined Jul 1, 2009
2,110
Hello guys i can't understand some line in mikroC i2c manual.

Note : Calculation of the I²C clock value is carried out by the compiler, as it would produce a relatively large code if performed on the library level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.



Can anyone explain the above line??
In looking at this even further, the 'Description' line seems to be what you're looking for. You need to look at the datasheet for the MCU you're using, and basedon it's clockrate, use the values they suggest you use to submit to the i2c function.

All I can say is they made it a lot harder than it needs to be. I've written i2c libraries and the formula they are referencing is pretty straightforward.
 
Top