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 support@ccsinfo.com

Write more than 512B in a SD card? Please help

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



Joined: 24 Jan 2012
Posts: 4

View user's profile Send private message

Write more than 512B in a SD card? Please help
PostPosted: Sat Jan 28, 2012 10:12 am     Reply with quote

Hi guys!

I'm trying to use a SD card as data logger with tommi's code (mmc_spi_fat32.c).

I know that writes are in blocks of 512B. The problem is that I can only write 512B in the first sector. If I try to write more in the same program, it fails. The code don't change to the next sector.

But, If I write 512 bytes and I load again the same program to the PIC, then I can see 1024 bytes.

I made a test program that writes 512B to the SD card, but I can't write nothing else in the same program. How can I write more than 512 bytes?

Code:

#include <18F2455.h>

#device adc=10
#fuses XT,MCLR,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN
#use delay(clock=4000000)

#use i2c(master,sda=PIN_B0,scl=PIN_B1)


#include <string.h>
#include <usb_cdc.h>
#include "mmc_spi_fat32.h"
#include "mmc_spi_fat32.c"




void initSD(){

      while(MMCInit() != MMC_OK){ //espera inicialitzacio
         printf(usb_cdc_putc,"Init SD fail!\r\n");
         printf(usb_cdc_putc,"Try again...\r\n");
         delay_ms(100);
         MMCInit() == MMC_OK;
      }
      printf(usb_cdc_putc,"Init SD OK!\r\n"); 
      delay_ms(100);
      InitFAT();
      printf(usb_cdc_putc,"Init FAT OK!\r\n");
      delay_ms(100);
}



void main()   
{
   
   int8 file,gfilename[4];
   int8 t[5]; 
   int8 h[5];
   int8 p[6];
   int8 l=5, ln=6, i=0;
   int mesure=0;

   usb_cdc_init();
   usb_init();
   
   while(!usb_cdc_connected()) {}         // Espera fins detectar una transmisió USB
 
   if (usb_enumerated()){                 // El PC reconeix el dispositiu
 
      usb_task();

      initSD();
      strcpy(gfilename,"D.LOG");
      delay_ms(100);
      file = fopen(gfilename,'a'); // open EVENTS.LOG for append 
     
      delay_ms(100);
         mesure=0;
         while(mesure<32){          //512 BYTES
             delay_ms(100);
             
            for (i=0;i<=4;i++){
               t[i]=7;
               h[i]=7;
            }
   
            for (i=0;i<=5;i++){
               p[i]=7;
            }
           
            delay_ms(100);
            fwrite(t,l,file);
            delay_ms(100);
                     
            fwrite(h,l,file);
            delay_ms(100);
           
            fwrite(p,ln,file);
            delay_ms(100);
            fclose(file);
           
            mesure=mesure+1;
           
            printf(usb_cdc_putc," %u \n\r",mesure);

         }
             
         printf(usb_cdc_putc,"Test done!\r\n");
         while(1){}
         
     
   }
   
   
}


   
   
temtronic



Joined: 01 Jul 2010
Posts: 9177
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 28, 2012 1:22 pm     Reply with quote

comments...

I only see 3 writes totalling 16 '7's to the file before you close it in your loop....

Stuffing the same value in every byte is very, very bad for debugging.

Using 1 character variable names is bad and went out of fashion in 1979 when Microsoft BASIC allowed 2 character variables. Use longer names to self-describe WHAT the data is. You'll be able to read it and get more help from others.

It appears you're using an SPI style card so why do you have the I2C function ?

I don't have the drivers...
#include "mmc_spi_fat32.h"
#include "mmc_spi_fat32.c"
so I can't comment on how you're passing data to it or saving it.

It is 'normal' practice to use unique data for test purposes or you'll never figure out 'bugs'. For testing 512 byte 'records', I'd save a 'record number', then known data as 'filler'. If you want to test say 1000 record the first 4 bytes could be the record number.
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