SD Card memory parameters

Post your requests and ideas on the future development of mikroPascal.
Post Reply
Author
Message
ValterG
Posts: 30
Joined: 27 Jun 2005 17:52
Location: Italy

SD Card memory parameters

#1 Post by ValterG » 12 Sep 2005 17:44

This program allows to read a few parameters of the Boot Sector of a formatted FAT16 SD Card.
The data are shown on a display to 2 rows and 16 characters.
The parameters are useful when wants to use the SD card as raw memory support.

Code: Select all

//******* SDParam***********************************************************
// This program read the parameters from SD Card memory of 64Mb or 128Mb
// and shows the information on LCD 16x2 display.
// This program works with an EASYPIC2, PIC18F452, 8MHz cristall MMC card support
// Set: Key = PORTA 0 whith jp8 = pull up, SW1 RA0 = ON, MMC card = PORTC,
// LCD 16x2 = PORTB 4 bit data.
// SD Card Memory tested:
// SD Card EXAGERATE 128Mb, SD Card Kingston 128Mb
// Parameters displayed:
// Fat type: Fat1 start sector and dimension, Fat2 start sector and dimension,
// Root directory start sector and dimension, Data start sector, data end sector
// Sectors available for Data.
// Memoryzed Start data sector and End data sector in the first data sector.
//*****************************************************************

program SDParam;

var   tmp, nn, oldstate : byte;
      Fat1, StFat2, RootD, NuSecRoD, x: integer;
      Dstart, DEnd, Djolly, Dst: longint;
      txt  : array[12] of char;
      data : array[512] of byte;
      
//Read SD Card Memory
Procedure Init_SDCard_1;
begin
      Lcd_Cmd(LCD_CLEAR);
      tmp := Sd_Init(PORTC, 2);  // Init Sd Card
      if tmp = 0 then
      begin
       Lcd_Out(1,1,'SD Init OK      ');
      end
       else
      Begin
       Lcd_Out(1,1,'SD Init Error   ');
      end;
end;
      
//read sector 0  boot sector of Sd Card
procedure Read_Boot_Sector_2;
begin
         Lcd_Cmd(LCD_CLEAR);
         tmp := Sd_Read_Sector(0, Data);
         if tmp = 0 then
         begin
           Lcd_Out(1,1,'Read BootSect Ok');
           For x := 0 to 4 do Lcd_chr(2,6+x,Data[54+x]); //Display type FAT
         end
         else
         begin
           Lcd_Out(1,1,'SD Read Error   ');
           Lcd_Out(2,1,'Reformat SD card');
         end;
end;

// Fat2 start sector and namber of sectors
Procedure FAT1_Start_Sector_3;
begin
         Lcd_Cmd(LCD_CLEAR);
         Fat1 := Data[23];
         Fat1 := Fat1 shl 8;
         Fat1 := Fat1 + Data[22];
         Lcd_Out(1,1,'Fat1 StSec n Sec');
         Lcd_Out(2,1,' 1   1');
         IntToStr(Fat1,txt);
         Lcd_Out(2,10,txt);
end;

// Fat2 start sector and namber of sectors
Procedure FAT2_Start_Sector_4;
begin
         Lcd_Cmd(LCD_CLEAR);
         StFat2 := Fat1 + 1;
         Lcd_Out(1,1,'Fat2 StSec n Sec');
         IntToStr(StFat2,txt);
         Lcd_chr(2,2,'2');
         Lcd_Out(2,4,txt);
         IntToStr(Fat1,txt);
         Lcd_Out(2,10,txt);
end;

// Root directory start sector and number of sectors
Procedure Root_Directory_Start_5;
begin
        Lcd_Cmd(LCD_CLEAR);
        RootD := Data[18];
        RootD := RootD shl 8;
        RootD := RootD + Data[17];   //Number of directory
        Lcd_Out(1,1,'StRootD  NumSect');
        IntToStr(1+(Fat1 * 2),txt);
        Lcd_Out(2,1,txt);            // display start Root dirctory sector
        NuSecRoD := (RootD * 32) Div 512;
        IntToStr(NuSecRoD,txt); //Display namber of sectors for RootDir
        Lcd_Out(2,8,txt);
end;

// Data: start sector
Procedure Data_Start_Sector_6;
begin
        Lcd_Cmd(LCD_CLEAR);
        Dstart := (Fat1 * 2) + NuSecRoD + 1;
        Lcd_Out(1,1,'Data Start Sect.');
        LongintToStr(Dstart,txt);
        Lcd_Out(2,1,txt);
end;

// Data End sector
procedure Data_End_Sector_7;
begin
        Lcd_Cmd(LCD_CLEAR);
        Dend := Data[35];
        Dend := Dend shl 24;
        Djolly := Data[34];
        Djolly := Djolly shl 16;
        Dend := Dend + DJolly;
        Djolly := Data[33];
        Djolly := Djolly shl 8;
        Dend := Dend + DJolly;
        Djolly := Data[32];
        Dend := Dend + DJolly;
        LongintToStr(Dend,txt);
        Lcd_Out(1,1,'Data End sector ');
        Lcd_Out(2,1,txt);
end;

// Available Sectors for data = DataEnd-DataStart
Procedure Data_Avaliable_Sectors_8;
begin
        Lcd_Cmd(LCD_CLEAR);
        LongintToStr(Dend - Dstart,txt);
        Lcd_Out(1,1,'Avaliable sect.');
        Lcd_Out(2,1,txt);
end;

// when memorize raw data in to data sectors, save the
// start sector + 1 and end sector in to first data sector
// for use in read data via Usart.
Procedure Write_Info_Start_End_Sectors_9;
begin
     for x := 0 to 511 do
     begin
     Data[x] := $00;  //Clear Data array
     end;
     Dend := Dend - 16;
     dst := Dstart + 1;
     Data[0] := lo(Dst);
     Data[1] := Hi(Dst);
     Data[4] := lo(Dend);
     Data[5] := Hi(Dend);
     Data[6] := Higher(Dend);
     Data[7] := Highest(Dend);
     Lcd_Cmd(LCD_CLEAR);
     tmp := Sd_Write_Sector(Dstart, Data);
       if tmp = 0 then
       begin
       Lcd_Out(1,1,'SD-card Write Ok');
       Lcd_Out(2,1,'End Test.       ');
       Delay_ms(3000);
       Lcd_Out(1,1,'Change SD card  ');
       Lcd_Out(2,1,'Press RA0 Key   ');
       end
       else
       begin
         Lcd_Out(1,1,'SD Write Error  ');
         Lcd_Out(2,1,'Reformat SD card');
       end;
end;

// main
begin
      PORTA := 0;               //set PORTA TTL
      ADCON1 := 6;
      TRISA := $01;             //RAO = in
      
      TRISB := 0;
      Lcd_Init(PORTB);           // Init LCD
      Lcd_Cmd(LCD_CURSOR_OFF);
      Lcd_Out(1,1,'Insert SD Card  ');
      Lcd_Out(2,1,'Press RA0 Key   ');

      oldstate := 0;
      nn := 1;
      
      while true do
      begin
       if Button(PORTA, 0, 1, 1) then oldstate := 255;
       if oldstate and Button(PORTA, 0, 1, 0) then
         begin
           case nn of
             1 : Init_SDcard_1;
             2 : Read_Boot_Sector_2;
             3 : FAT1_Start_Sector_3;
             4 : FAT2_Start_Sector_4;
             5 : Root_Directory_Start_5;
             6 : Data_Start_Sector_6;
             7 : Data_End_Sector_7;
             8 : Data_Avaliable_Sectors_8;
             9 : Write_Info_Start_End_Sectors_9;
             end;
           inc(nn);
           if nn > 9 then nn := 1;
          oldstate := 0;
         end;
      end;
end.
ValterG

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: SD Card memory parameters

#2 Post by zristic » 12 Sep 2005 19:52

Nice example. We will distribute it starting from next release. Do you want to leave an email for contact information?

ValterG
Posts: 30
Joined: 27 Jun 2005 17:52
Location: Italy

#3 Post by ValterG » 12 Sep 2005 21:49

Ok

My Mail address is: valtergennaro at alice dot it

best regards
ValterG

Post Reply

Return to “mikroPascal Wish List”