CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

i2c speed- too slow?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

i2c speed- too slow?
PostPosted: Fri Jun 24, 2005 4:58 am     Reply with quote

Hi

I am really battling with the i2c and writting to an external EEPROM.

I have the following subroutine, which actually does work, however it seems too slow:

void pageWrite_ext_eeprom(long int address, byte mask){
int ik;
//***Line #1
START:
i2c_start();
if(i2c_write(0xa0 | mask)!=0) //Device must ACKNOWLEDGE
goto START;
i2c_write(address>>8); //High Byte of Address
i2c_write(address&0X00FF); //Low Byte of Address
//***Line #2
for(ik=0;ik<32;ik++){ //Write Page to EEPROM
i2c_write(dat[ik]);
}
//***Line #3
i2c_stop();
}

At the top of the file I have:

#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL)

I have 2 10k pull up resistors on the SDA and SCL lines.

The time it takes for a PIC16LF873 to go from Line 1 to Line 2 = 726 ticks
The time it takes to go from Line #2 to Line #3 = 7827ticks

It seems very very slow to me- can someone please tell me if these times are reasonable (I am using a 4MHz Xstal).
and If not, then what I am doing wrong?

Thank you in advance for your help.
arrow
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jun 24, 2005 5:11 am     Reply with quote

I have not used I2C routines of CCS however I don't see where you have set the clock rate for the I2C bus.

Also don't forget that when you issue the i2c_stop() the EEPROM will be busy for 5ms. If you attempt another write in this time then you will be locked in your goto loop.

Why did you use goto instead of a while loop?

Code:

while(i2c_write(0xa0 | mask)!=0) //Device must ACKNOWLEDGE
         ;

_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 5:15 am     Reply with quote

Hi asmallri

I dont know how to set the clock in i2c. My version of CCS seesm to set the clock speed of the i2c automatically.

Still it seems rather a long time to write 32 data points?

arrow
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jun 24, 2005 5:19 am     Reply with quote

DO you have a
Code:
#use delay(clock=....)
statement?
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 5:23 am     Reply with quote

Hi

Yes i have the line:

#use delay(clock=4000000)

at the top of the file.

arrow
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jun 24, 2005 5:34 am     Reply with quote

If the I2C clock is running at 100K (10 tick per edge) then these numbers look realistic.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 5:38 am     Reply with quote

Hi

Please can you show me rough calculations on how transfering 32bytes (excluding address writting) should take approximatley 7.8ms (7800 ticks on a 4MHz Xstal)?

Thank you
arrow
Ttelmah
Guest







PostPosted: Fri Jun 24, 2005 5:43 am     Reply with quote

Have you tried adding 'FAST'?.
What is your compiler version?. Unfrtunately some versions are better than others at calculating the timing values.
How long are your I2C lines, what is the likely capacitance?. 10KR, is rather high. It is pushing the maximum resistor value for 5v I2C. The minimum value for this resistor (assuming you are not using any series resistors in the bus), is about 2KR for a 5v bus. Assuming your bus has a relatively low capacitance (two inputs, and a short track run), then it will be OK, but I'd suggest going lower, especially if you want to push higher switching rates. 4K7, is a more 'common' general use value.
What is a 'tick'?. Is this a processor clock cycle, or a crystal clock cycle?. You need to specify which you are using.
The absolute 'fastest', that could be achieved using 100K I2C, with no 'hold' from the chip, is around 3600 processor cycles.
You are aware that the page size for the 24LC256, which was mentioned in another post, is 64bytes, not 32.

Best Wishes
arrow



Joined: 17 May 2005
Posts: 213

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 5:59 am     Reply with quote

Hi Ttelmah

Yes I have tried adding "FAST"-- that did nothing visible.

Compiler version is 2.686-- ie very old.

I use "tick" = XStal clock cycle/4. So in my case its 4MHz/4 => 1us

I am running off a 3.3V power supply using a 16LF873 chip.
I have used 2k ohm resistors- that did nothing visible.
Lines are very short in my case.

The way I see it is:
100khz gives 10us per i2c cycle
to write 32 bytes of data: 32*9*10us = 1980us or 1.98ms
I am getting 7.8ms or about 4 times longer.

Yes I am aware that the page size for the 256 is 64bytes. The problem with using 64byte page in my code is that i get mysterious data comming into the array. 32bytes only produces 1 corrupt data byte. That bad byte is due to the long time in the writting cycle.

If you could help me improve the speed of the i2c i would greately appreciate it.

All the best
arrow
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Jun 24, 2005 6:15 am     Reply with quote

Don't forget to take the for loop code into account.

You have not used FORCE_HW in the #use statement so the software driver is used with additional instructions are used for setting and clearing bits and for changing I/O direction.

At 1us clock period you may easily be spending more than this time processing the for loop.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Jun 24, 2005 6:41 am     Reply with quote

How about looking at the signals with a scope to see what is going on Idea

Also, do you have any interrupts? If any interrupts are occuring then your routines will appear longer than they actually are.
Ttelmah
Guest







PostPosted: Fri Jun 24, 2005 9:24 am     Reply with quote

First thing, writing a byte takes more than even 9 I2C clock times. The minimum time for the ack at the end of each byte, is one clock time, but generally most chips do stretch this a little, so 10 clock times is more likely.
Then the I2C write code, has to wait for the byte to complete, before sending the next byte. Add at least six or seven processor cycles for this. Similarly the loop will involve perhaps another ten cycles.
Your compiler is very old, look at the listing file, and check what value is actually being sent to the I2C baud rate generator. I'd expect it to be setting the rate to perhaps 11, giving a clock of perhaps 83KHz. 100K, is a 'max' spec for the original I2C bus, and the compiler tends to err slightly on the safe side when setting the value.
As others have said, force hardware use as well.
The lower resistors will help if you push the clock rate up, but at <100K, 10KR should be fine.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group