View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
i2c speed a function of the PIC Xstal? |
Posted: Mon Jul 04, 2005 9:31 am |
|
|
Hi
Is the clock speed of the i2c a function of the PIC Xstal?
I have been using a 4MHz Xstal, and the following code:
Code: | #use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL, FAST) |
and
Code: |
START:
i2c_start();
if(i2c_write(0xa0 | mask)!=0) //Device must ACKNOWLEDGE
goto START;
iTimer = 0;
set_timer1(0);
i2c_write(address>>8); //High Byte of Address
iTimer = get_timer1();
printf("+%x%x ", (iTimer>>8)&0X00FF, iTimer&0X00FF);
set_timer1(0);
i2c_write(address&0X00FF); //Low Byte of Address
iTimer = get_timer1();
printf("+%x%x ", (iTimer>>8)&0X00FF, iTimer&0X00FF);
set_timer1(0);
i2c_write(232);
i2c_write(232);
i2c_write(232);
i2c_write(232);
iTimer=get_timer1();
printf("-%x%x ", (iTimer>>8)&0X00FF, iTimer&0X00FF);
i2c_stop();
|
I have found that the first printf (first address byte) takes 235 Xstal "ticks"- the length due to the operation on the address.
the second printf- 2nd byte of the address takes
240 Xtal "ticks"- once again due to the address computation.
the 4 data writes take on average 42.5 Xstal "ticks" ie. 170 ticks for 4 writes. The last translates to approximately 211kHz.
Surely the i2c should be running at 400kHz- ie two times faster?
What am I doing wrong?
Thank you in advance for your help
Regards
arrow |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jul 04, 2005 11:24 am |
|
|
First, if you want to measure the speed of the I2C then you must measure the duration of the clock and not how long it takes to send 4 bytes.
But to answer your question, yes the xtal frequency affects the speed. The hardware mssp uses the sspadd register as a baud rate generator. By writing different values to this register, you will get different speeds. This value is decremented twice per instruction cycle time or 1/2 of the xtal freq. CCS handles this value if you use their setup functions. Obviously the software I2C functions will run faster if the clock is faster. You will have to look at the lst file to determine if CCS accounts for this or you could change the use delay and see the difference. |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Tue Jul 05, 2005 2:45 am |
|
|
Hi
Thank You for your reply.
Is it normal for the i2c_write(address) to take so much longer than i2c_write(data)?
I am finding that writting the address takes about 4 times longer than writtign data.
Can you please tell me what I am doing wrong?
Regards
arrow |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Tue Jul 05, 2005 2:46 am |
|
|
Hi
Thank You for your reply.
Is it normal for the i2c_write(address) to take so much longer than i2c_write(data)?
I am finding that writting the address takes about 4 times longer than writtign data.
Can you please tell me what I am doing wrong?
Regards
arrow |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Tue Jul 05, 2005 2:47 am |
|
|
Hi
Thank You for your reply.
Is it normal for the i2c_write(address) to take so much longer than i2c_write(data)?
I am finding that writting the address takes about 4 times longer than writtign data.
Can you please tell me what I am doing wrong?
Regards
arrow |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jul 05, 2005 6:38 am |
|
|
No. Take a look at it with a scope if you want to see what is going on. |
|
|
|