datalogger using USB and SD/MMC

General discussion on mikroC.
Author
Message
reiger
Posts: 29
Joined: 01 Apr 2008 13:57

datalogger using USB and SD/MMC

#1 Post by reiger » 01 Apr 2008 14:05

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

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: datalogger using USB and SD/MMC

#2 Post by srdjan » 09 Apr 2008 12:27

Hi,
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
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.
http://www.mikroe.com/forum/viewtopic.php?p=68699#68699

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

a quick guide

#3 Post by reiger » 10 Apr 2008 17:36

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

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

a small add on

#4 Post by reiger » 10 Apr 2008 21:31

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.

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: a small add on

#5 Post by srdjan » 11 Apr 2008 09:52

Hi,
Thank you for sharing.

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

fat is going progressively slower

#6 Post by reiger » 14 Apr 2008 18:53

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];
}

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: fat is going progressively slower

#7 Post by srdjan » 15 Apr 2008 07:12

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.

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

solution for increasing delay?

#8 Post by reiger » 15 Apr 2008 11:28

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?

User avatar
srdjan
mikroElektronika team
Posts: 1552
Joined: 28 Dec 2005 12:47
Location: Serbia

Re: solution for increasing delay?

#9 Post by srdjan » 15 Apr 2008 15:15

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).
reiger wrote:Is the space reserved when I open a swap file?
Normally, once a swap file is created, the space reserved for it is occupied in FAT table.

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

pic18f4455 versus pic18f4550

#10 Post by reiger » 18 Apr 2008 13:34

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.

Isaac
Posts: 121
Joined: 13 Jul 2006 23:03
Location: Sinaloa, Mexico

Re: pic18f4455 versus pic18f4550

#11 Post by Isaac » 02 May 2008 19:47

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.
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
Posts: 29
Joined: 01 Apr 2008 13:57

#12 Post by reiger » 02 May 2008 20:52

I had a similar problem, I solved it by:

Code: Select all

 Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_END, CLK_IDLE_LOW, LOW_2_HIGH);
instead of

Code: Select all

 Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
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...

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

#13 Post by reiger » 02 May 2008 21:06

surely there is something fishy with that pll combination with mmc. In C18 it works fine, but mikroc fails on it.

reiger
Posts: 29
Joined: 01 Apr 2008 13:57

#14 Post by reiger » 02 May 2008 21:47

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.

Isaac
Posts: 121
Joined: 13 Jul 2006 23:03
Location: Sinaloa, Mexico

#15 Post by Isaac » 02 May 2008 22:07

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.
But the capacitive distorcion on the signal is generated by your personal circuitry or is in the EP4 board?

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? :(

Post Reply

Return to “mikroC General”