datalogger using USB and SD/MMC
datalogger using USB and SD/MMC
Hi,
I'm building a prototype of a datalogger with a 16 bit ADS7805 ADC logging data on an SD-card (ADC will change in final design, took this one because is a DIP). I have no idea what the maximum writing speed is using the library in MikroC. I learned from the forum that it works rather slow, but that some people achieved high speed data transfer. How can I maximize it? Does anybody have a link to example code maximizing the data transfer rate. 100 kB/S would be great and overkill already.
thanks a lot,
Reiger
I'm building a prototype of a datalogger with a 16 bit ADS7805 ADC logging data on an SD-card (ADC will change in final design, took this one because is a DIP). I have no idea what the maximum writing speed is using the library in MikroC. I learned from the forum that it works rather slow, but that some people achieved high speed data transfer. How can I maximize it? Does anybody have a link to example code maximizing the data transfer rate. 100 kB/S would be great and overkill already.
thanks a lot,
Reiger
Re: datalogger using USB and SD/MMC
Hi,
http://www.mikroe.com/forum/viewtopic.php?p=68699#68699
You should use swap file if fat file system form is needed or plain sector read/write routines if fat file system is not required. Here is a topic you can check for writing speed achieved by some of our users.reiger wrote:Hi,
I'm building a prototype of a datalogger with a 16 bit ADS7805 ADC logging data on an SD-card (ADC will change in final design, took this one because is a DIP). I have no idea what the maximum writing speed is using the library in MikroC. I learned from the forum that it works rather slow, but that some people achieved high speed data transfer. How can I maximize it? Does anybody have a link to example code maximizing the data transfer rate. 100 kB/S would be great and overkill already.
thanks a lot,
Reiger
http://www.mikroe.com/forum/viewtopic.php?p=68699#68699
a quick guide
Hi,
Thanks for the quick reply. I have most things up and running now. I saw that the topic had a bunch of visitors so maybe a quick guide might be nice:
I used a pic 18f4455 (similar to 4550)
For the USB:
0) load the HID example in from the extra_examples directory
1) read carefully the oscillator chapter in the datasheet. I took a 8MHz crystal and had to go to 48MHz operation. So I went to project->edit project. select: PLLDIV2, CPUDIV2, FOSC_HSPLL_HS, WDT_OFF, LVP_OFF, XINST_OFF. All the rest may not be selected if not needed: so switch off FOSC_INTOSC_HS and FOSC_HS for now.
2) set the jumpers on the board correct, connect to the computer and if all is ok you should now see the connection message
For the MMC/SD:
this took me an hour more but here we go (forgot to switch of the ADC):
0) make sure your connections are correct: check with ohm meter to know for sure (remind that there are resistors in the circuit so the 'beep' of your ohm meter might not go off) I used PORTC pin 6 as Chip select pin (PORTC 3-4-5 are used by the USB, PORTC 6-7 by the SPI, PORTB0-1 by the SPI.).
1) take the fat16 example from the examples and take down that 48MHz installation we just did to for example 2 MHz, just to be sure your clock does not goes too fast.
2) disable all UART commands (because there is a pin that can not be used)
3) disable the ADC on PORTB: ADCON1 = 15;
4) add while(Mmc_Fat_QuickFormat(&PORTC, 6,"datalog")); under the first Spi_Init_Advanced command
5) set TRISD=0:PORTD=0; and add in between all the commands PORTD++;
6) switch on portb and portc on the right side of your easypic board and portd on the switch next to the led matrix.
and last but not least: merge the two blocks. Have fun.
that should be it
Reiger
Thanks for the quick reply. I have most things up and running now. I saw that the topic had a bunch of visitors so maybe a quick guide might be nice:
I used a pic 18f4455 (similar to 4550)
For the USB:
0) load the HID example in from the extra_examples directory
1) read carefully the oscillator chapter in the datasheet. I took a 8MHz crystal and had to go to 48MHz operation. So I went to project->edit project. select: PLLDIV2, CPUDIV2, FOSC_HSPLL_HS, WDT_OFF, LVP_OFF, XINST_OFF. All the rest may not be selected if not needed: so switch off FOSC_INTOSC_HS and FOSC_HS for now.
2) set the jumpers on the board correct, connect to the computer and if all is ok you should now see the connection message
For the MMC/SD:
this took me an hour more but here we go (forgot to switch of the ADC):
0) make sure your connections are correct: check with ohm meter to know for sure (remind that there are resistors in the circuit so the 'beep' of your ohm meter might not go off) I used PORTC pin 6 as Chip select pin (PORTC 3-4-5 are used by the USB, PORTC 6-7 by the SPI, PORTB0-1 by the SPI.).
1) take the fat16 example from the examples and take down that 48MHz installation we just did to for example 2 MHz, just to be sure your clock does not goes too fast.
2) disable all UART commands (because there is a pin that can not be used)
3) disable the ADC on PORTB: ADCON1 = 15;
4) add while(Mmc_Fat_QuickFormat(&PORTC, 6,"datalog")); under the first Spi_Init_Advanced command
5) set TRISD=0:PORTD=0; and add in between all the commands PORTD++;
6) switch on portb and portc on the right side of your easypic board and portd on the switch next to the led matrix.
and last but not least: merge the two blocks. Have fun.
that should be it
Reiger
a small add on
for those who never worked with the compiler: the clock frequency you see on the left side or in the edit project window is NOT your crystal, it is the frequency you just entered by editing the registers. It this case with the USB 48MHz should be entered and in the MMC case 2MHz.
Re: a small add on
Hi,
Thank you for sharing.
Thank you for sharing.
fat is going progressively slower
I want to write a cluster of files to a fat16 but every write goes slower and slower. What is going wrong? I use Mmc_Fat_Get_Swap_File to make a window to write in.
Code: Select all
if (!Mmc_Fat_Init(&PORTC, 6)) {
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
makefilename();
size = Mmc_Fat_Get_Swap_File(filesze, filename, 0x20);
ii=0;
while(numfiles<=maxnumfiles){
for(i=0; i<512; i++){Buffer[i] = i;}
Mmc_Write_Sector(size++, Buffer);
ii++;
if (size && ii>=filesze){
ii=0;
makefilename();
size = Mmc_Fat_Get_Swap_File(filesze, filename, 0x20);
}
}
}
void makefilename(){
filenr[0]++;
if(filenr[0]==kar){
filenr[0]='0';
filenr[1]++;
if(filenr[1]==kar){
filenr[1]='0';
filenr[2]++;
if(filenr[2]==kar){
filenr[2]='0';
filenr[3]++;
if(filenr[3]==kar){
filenr[3]='0';
filenr[4]++;
}
}
}
}
filename[3]=filenr[4];
filename[4]=filenr[3];
filename[5]=filenr[2];
filename[6]=filenr[1];
filename[7]=filenr[0];
}
Re: fat is going progressively slower
Hi,
The "writing to a file" process should have the same duration regardless of the file you are writing to. The "creating swap file" process increases its duration as the files are placed higher in the mmc/sd card memory. This is because each time free cluster search for a new swap file starts from the beginning of the FAT table and it takes longer to reach free clusters as more and more files are added.
The "writing to a file" process should have the same duration regardless of the file you are writing to. The "creating swap file" process increases its duration as the files are placed higher in the mmc/sd card memory. This is because each time free cluster search for a new swap file starts from the beginning of the FAT table and it takes longer to reach free clusters as more and more files are added.
solution for increasing delay?
So the solution is then to generate the files in advance with different size indicators? Or not to use FAT at all? Is the space reserved when I open a swap file?
Re: solution for increasing delay?
reiger wrote:So the solution is then to generate the files in advance with different size indicators?
Yes, and file size can be read later with Mmc_Fat_Get_File_Size routine if necessary.
reiger wrote:Or not to use FAT at all?
Once you create swap files, the writing speed will be the same as when FAT is not used, because you will be using plain write sector routine. If you do not need fat file organization of written data, you can gain the time needed for swap file creation (if saving that time means something to you).
Normally, once a swap file is created, the space reserved for it is occupied in FAT table.reiger wrote:Is the space reserved when I open a swap file?
pic18f4455 versus pic18f4550
I tried my code on a pic18f4455, it works fine. On the pic pic18f4550, which is the big brother the MMC card does not initialize. I have no idea what might be the problem.
Re: pic18f4455 versus pic18f4550
Excuse me reiger, i have one question, the speed of my PIC is 32MHz and even with a 64 prescalar in the spi initialization i am unable to detect the SD card, only when i change to 8MHz clock with no PLL i can read it!!. The problem is that i need to run as fast as posible in my program, so how can i initialize my FAT16 rutines with that clock?reiger wrote:I tried my code on a pic18f4455, it works fine. On the pic pic18f4550, which is the big brother the MMC card does not initialize. I have no idea what might be the problem.
I had a similar problem, I solved it by:
instead of
However I have the impression that C18 is more flexible in this issue, USB and MMC-fat16 is surely no option with this compiler. The main problem is the fact that the access to the ram is not as easy as in C18. I managed with C18 to build a mass storage device/datalogger pumping more than 50MB in 4 minutes trough that pic...
Code: Select all
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_END, CLK_IDLE_LOW, LOW_2_HIGH);
Code: Select all
Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
But the capacitive distorcion on the signal is generated by your personal circuitry or is in the EP4 board?reiger wrote:I forgot to give you the reason of sampling at the end. I saw some capacitive influence on my signals, so it is better to sample a bit later.
So there is no way to boot the PIC at 8 MHz and then switch to the PLL?
can the dsPIC manage a bigger preescaler for the SPI?