|
|
View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
EEPROM Write Cycle- Does it Hog the PIC's time? |
Posted: Thu Jun 23, 2005 4:24 am |
|
|
Hi
A newbie question on EEPROMs:
I have the following subroutine:
void pageWrite_ext_eeprom(long int address, byte mask){
int ik;
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
for(ik=0;ik<PAGE_SIZE;ik++){ //Write Page to EEPROM
i2c_write(dat[ik]);
}
i2c_stop();
}
The datasheet of 24LC256 says that when "stop" is written to the EEPROM, the EEPROM starts writting the data in the page to memory- this it says will take a maximum of 5ms.
My question is: how long (how many Xstal "ticks") will the above routine take?
when the i2c_stop() is executed, will the PIC require a further 5ms before the subroutine returns?
Thank you in advance for your help.
arrow |
|
|
valemike Guest
|
|
Posted: Thu Jun 23, 2005 5:34 am |
|
|
Code: |
.................... i2c_stop();
1236: BSF FC5.2
1238: BTFSC FC5.2
123A: BRA 1238
|
i2c_stop only takes a few microseconds, and you wouldn't have to wait 5ms just to issue the stop. (You are done with your eeprom writing already, and you are merely ending the i2c transaction)
in detail...
i2c_stop only takes one instruction cycle to issue the stop command. A stop condition would then complete something like two i2c clock cycles, that's why you see the btfsc instruction above. For example, if you are using the regular 100khz, then 1/100000 x 2= 20us. |
|
|
arrow
Joined: 17 May 2005 Posts: 213
|
|
Posted: Thu Jun 23, 2005 5:39 am |
|
|
Hi Valemike
Thank you for your reply- ok so the i2c_stop() is not hogging the time.
Can you please tell me how long the whole subroutine will take to execute? (I am using a 4MHz Xstal). and PAGE_SIZE=16
Once again thank you
arrow
void pageWrite_ext_eeprom(long int address, byte mask){
int ik;
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
for(ik=0;ik<PAGE_SIZE;ik++){ //Write Page to EEPROM
i2c_write(dat[ik]);
}
i2c_stop();
} |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 23, 2005 6:55 am |
|
|
The easiest way, is to run the routine in a simulator, and use the 'stopwatch' ability. The time will depend mainly on the I2C rate chosen, which you do not state.
Also if the chip at the other end of the I2C bus is 'busy' at the start, the code will wait in the initial loop for the acknowledge. Assuming that this does not occur, there will be 19 I2C transfers, each taking about 95uSec (at 100K I2C, and assuming the MSSP BRG value is correctly set for this, and depending on how long the slave holds the 'ack' line on each transfer). Probably about 2mSec.
Best Wishes |
|
|
Guest
|
|
Posted: Thu Jun 23, 2005 7:13 am |
|
|
Hi Ttelmah
Thank you for your post.
I am not sure how to set the clock speed on the i2c.
At the top of the file I have:
#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL)
but there is no reference to clock speed on the i2c.
I have 2 2.2k pull up resistors on the EEPROM.
I would be really thankful if you can tell me how to set the clock speed.
(The faster the better).
I am using the LF pic's and running off a 3.3V power supply.
arrow |
|
|
Ttelmah Guest
|
|
Posted: Thu Jun 23, 2005 2:44 pm |
|
|
#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL,FAST)
The default I2C specification, is 100KHz clock. There is a fast defintion at 400Khz clock, and adding the 'FAST' keyword will tell the compiler to try to program closer to this faster rate. Going beyond this, will depend on the version of your compiler. The 'general purpose' way, is to define the address of the I2C BRG as a BYTE variable, and set it yourself. The other method only applies to the latest compilers, which allow you to use:
#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL,FAST=800000)
and the compiler will try to program the rate you have specified.
In each case, the 'actual' rate will be limited by the hardware. For a 4Mhz master clock, the fastest rate possible is 1Mhz, and for the 400K specification, the fastest rate possible 'below' this, is 333KHz.
Best Wishes |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
|
Posted: Thu Jun 23, 2005 3:33 pm |
|
|
Anonymous wrote: |
I would be really thankful if you can tell me how to set the clock speed.
(The faster the better).
|
i must be missing something here. regardless of how fast you run the i2c bus, the rate at which you can write to the serial EEPROM is governed by how quickly it can finish it's internal write operation. if the EEPROM can only write 1 byte per 5 ms (+ a bit of overhead), that is all you can do as well -- no matter how fast you run the i2c bus.
or are you trying to keep the PIC busy with some other task during the EEPROM write cycle?
jds-pic |
|
|
Guest
|
|
Posted: Fri Jun 24, 2005 2:52 am |
|
|
Hi
I am a newbie and dont really know what is a simulator. I would really appreciate it if you give me a step by step example?
Also, Can someone please tell me how long the following code snippet will take in ms?
Thank you in advance
arrow
ChXL = get_timer1();
set_timer1(0);
interL = inter;
inter = 0;
DutyCycle = (float)(ChXH)/(float)(ChXH+ChXL);
DutyCycle = DutyCycle*255;
accel = (int)(DutyCycle);
dat[icnt] = accel;
icnt++;
if(icnt==PAGE_SIZE){
if(iPage==100){
break;
}
dat[0] = prev;
prev = dat[PAGE_SIZE-1];
pageWrite_ext_eeprom(iPage*PAGE_SIZE, mask);
iPage++;
icnt=0;
}
printf("+%x ",accel);
and the pageWrite_ext_eeprom subroutine is:
void pageWrite_ext_eeprom(long int address, byte mask){
int ik;
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
for(ik=0;ik<PAGE_SIZE;ik++){ //Write Page to EEPROM
i2c_write(dat[ik]);
}
i2c_stop();
}
PS
I have the accelerometer going at about 136Hz.
That means that the T = 7.35ms
Say the high time = the low time (approximately true), then I will have 7.35/2 = 3.68ms to perform the above tasks (Xstal = 4MHz, i2c running on the slow speed, using software, RS232 baud=19200). |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 24, 2005 3:11 am |
|
|
jds-pic wrote: | Anonymous wrote: |
I would be really thankful if you can tell me how to set the clock speed.
(The faster the better).
|
i must be missing something here. regardless of how fast you run the i2c bus, the rate at which you can write to the serial EEPROM is governed by how quickly it can finish it's internal write operation. if the EEPROM can only write 1 byte per 5 ms (+ a bit of overhead), that is all you can do as well -- no matter how fast you run the i2c bus.
or are you trying to keep the PIC busy with some other task during the EEPROM write cycle?
jds-pic |
An external EEPROM, can do a lot better than 1byte per 5mSec. They do one _block_ per write time (which is usualy below 4mSec on modern chips). The 'block write' routine, triggers the write when it finishes. So you can transfer the 16 bytes forming the block, as fast as the interface will allow, then trigger the write, and go away and do something else.
The post is about transfering the bytes to form such a block, and therefore the transfer is limited just by the bus speed, not by the write time of the EEPROM.
Best Wishes |
|
|
Guest
|
|
Posted: Fri Jun 24, 2005 3:26 am |
|
|
Hi Ttelmah
I understand what you write- that the EEPROM can be writting and the PIC can be doing something else in parallel.
However, it seems that the code I have posted above is taking longer than approximately 4ms. Can you please tell me if you think that the above code should be taking 4ms?
Is there a problem with the dat[] array?
All the best
arrow |
|
|
|
|
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
|