Page 1 of 1

I2C Speed

Posted: Tue Aug 05, 2008 1:03 pm
by Mark
Hi there,

The I2C component has 3 speeds, 100, 400 and 1,000 kHz. Is this a speed against an assumed clock speed or is it calculated from the actual clock speed? If a clock speed is assumed, what is it?

Thanks,

Mark

Re: I2C Speed

Posted: Tue Aug 05, 2008 5:20 pm
by Benj
Hi Mark

Using the Hardware setting the Baud rate is calculated using the actual clock speed specified by Flowcode and the I2C hardware registers to give the correct frequency. Using the software setting the baud rate is fixed at around 50khz.

Re: I2C Speed

Posted: Sun Aug 16, 2009 9:19 am
by georgesz113
Dear Ben,

It seems to me that there is a possible glitch in the I2C code regarding the baud rate (at least in version 3.4.7.48). I am running on a 40 MHz oscillator, so Fcyc is 10 MHz and I wanted a 1 MHz I2C clock frequency. But when you set this in the I2C component, it will produce 500 kHz instead of 1 MHz (similarly in the case of 400 kHz it will produce only 200 kHz). I had a look at the I2C code and it seems that the way you define the baud rate generator number (in the sspadd register) is a factor of two out. I had a quick look at a PIC datasheet and they list a few examples there two which also confirmed that you are a factor of two out.

For instance, one of their example is 40 MHz oscillator input and a desired 400 kHz clock. In this case, they write 18h as the BRG value, which is 24 in decimal, essentially means counting down 25 times. This value is decreased twice within the 1 clock cycle of Fcyc, therefore at a frequency of 2*Fcyc = 2*Fosc/4 = 20 MHz in this case. At 20 MHz, one count down takes 50 ns, so 25 counts would take 1250 ns or 1.25 us, which gives 800 kHz. This is the frequency of changing the logic state of the SCL pin, so the 'clock frequency' would be half of this which is indeed 400 kHz as wanted. In the C code that Flowcode generates, the BRG value is 20 which following the same logic would result in a change of logic state at 1 MHz but that would give a clock frequency of only 500 kHz.

Could you please help me to find where I can find within your code how the 1 MHz user input on the I2C component properties window gets translated to the BRG value of 20? I saw that this is parameter %c in the Get_Defines() function but I couldn't get any farther.

Thank you for your help.

Regards,
George

PS. Also within the I2C function you have 10 ms delays at the end of the stop function to make sure that the I2C bus is okay. I don't think this is necessary and it can be a real bugger to find this when someone wants a decent sampling frequency from say an I2C ADC as you won't be able to get even 100 Hz out of the system.

Re: I2C Speed

Posted: Mon Aug 17, 2009 8:51 am
by Sean
Hello George,

We will look into the I2C Baudrate issue.

If you want to edit the I2C component C code file (save the original file first), the Baudrate parameter is passed from Flowcode to the C code as the %c substitution in the line:

#define MX_MI2C_BAUD %c

You could replace this with:

#define MX_MI2C_BAUD (%c / 2)

or define a constant value if you always use the same settings.


The 10ms delay has been added to the MI2C_Stop macro to automatically guarantee completion of an EEPROM write operation - a common requirement in many of our examples. This can be removed from the C code, or commented out, if you want to increase speed of operation and manage any critical timings manually.