SD card - display filename of all files

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 - display filename of all files

#1 Post by ValterG » 26 Sep 2005 14:17

This program allows to read a few parameters of the Boot Sector of a formatted FAT16 SD Card.
The File names and start filedata sector are shown on a display to 2 rows and 16 characters.

Code: Select all

//*************************** SD_FileName *************************************
// This program shows on a LCD 16x2 display the FileNames of the files
// memorized in the SDCard; showing also the beginning sector Datafile.
// Having formatted the SDCard with Win98 FAT16 and the names of the
// files are of 8 characters plus 3 of extension.
// This program works with an EASYPIC2, PIC18F452, 4MHz 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
// First test: copy from PC and paste to Sdcard of 38 Jpg images.
// Second test: copy from PC and paste to Sdcard of 10 mixed files (txt,doc,pdf,exe)
// Parameters displayed:
// File name and number of start DataFile sector.
// The size of file not displayed (becose longint error).
//****************************************************************************


program SD_FileName;

var   tmp, TxEnd, RegW, os1, num, rds : byte;
      Fat1, SizeRootD, NuSecRootD : integer;
      Filestart : longint;
      data : array[512] of byte;
      RootDirStart, DataStart, step, i : word;
      txtl : string[11];

label Start;
label Loop1;
label loopfile;
label loopx;
label enddir;
label Error1;
label Error2;

procedure Del1sec;      //delay
begin
     Delay_ms(1000);
end;


// Result a key presses
function  FunKey(Paddr : byte) : Byte;
var
    Tasto: byte;
begin
    Tasto := 0;
    if Button(Paddr, 0, 1, 1) then os1 := 255;
    if os1 and (Button(Paddr, 0, 1, 0)) then
     begin
     os1 := 0;
     Tasto := 1;
     end;
     Result := Tasto;
end;

//read Param of Sd Card Boot sector
procedure ReadPar_Boot_Sector;
begin
        //FAT1 size
        Fat1 := Data[23];
        Fat1 := Fat1 shl 8;
        Fat1 := Fat1 + Data[22];
        //RootDirectory size
        SizeRootD := Data[18];
        SizeRootD := SizeRootD shl 8;
        SizeRootD := SizeRootD + Data[17];
        //RootDirectory sectors size
        NuSecRootD := (SizeRootD * 32) Div 512;
        //RotDir start at sector
        RootDirStart := (Fat1 * 2) + 1;
        DataStart := RootDirStart + NuSecRootD;
end;

//Read and display sector start file
Procedure ReadSectorStartFile;
var  xval : byte;
begin
         //read start sector  of file
         xval := 0;
         xval := Data[step + 27];
         FileStart := xval;
         Filestart := Filestart shl 8;
         xval :=  Data[step + 26];
         Filestart := Filestart + xval; //claster number of start File
         Filestart := ((Filestart * 4)+ DataStart) - 8; //sector number of start file

         LongintToStr(FileStart, txtl);
         Lcd_Out(2,1,txtl);             //Display sector start file
end;

// main
begin
      PORTA := 0;               //set PORTA TTL for Keys ">"
      ADCON1 := 6;
      TRISA := $01;             //RAO = in

      TRISB := 0;
      Lcd_Init(PORTB);           // Init LCD
      Lcd_Cmd(LCD_CURSOR_OFF);

      RegW := 0;
      TxEnd := 0;
      
      Lcd_Cmd(Lcd_Clear);
      Lcd_Out(1,1,'Insert SD Card');
      Lcd_Out(2,1,'Press > Key');
Start:
      RegW := FunKey(PORTA);        // scan Keys
      if RegW = 0 then Goto Start;  // no key pressed
      if RegW = 1 then              // Key = >
      begin
      Lcd_Cmd(Lcd_Clear);
      tmp := Sd_Init(PORTC, 2);     // Init Sd Card
      if tmp <> 0 then goto error1;
      Lcd_Out(1,1,'SD Init OK');
      Del1Sec;
      LCD_Cmd(LCD_CLEAR);
      Lcd_Out(1,1,'Read Boot Sector');
      Lcd_Out(2,1,'Press > Key');
      RegW := 0;
      end;
Loop1:
      RegW := FunKey(PORTA);        // scan Keys
      if RegW = 0 then Goto Loop1;  // no key pressed
      if RegW = 1 then              //key >
      begin
      tmp := Sd_Read_Sector(0, Data); //read boot sector of sd-card
      if tmp <> 0 then goto Error2;
      ReadPar_Boot_Sector;            //read parameters of boot sector
      tmp := Sd_Read_Sector(RootDirStart, Data);  //read rootdir
      if tmp <> 0 then goto Error2;
      LCD_Cmd(LCD_CLEAR);
      Lcd_Out(1,1,'View Files name');
      Lcd_Out(2,1,'Press > key   ');
      step := 0;
      rds := 0;
      
loopfile:
        RegW := FunKey(PORTA);           //scan key
        if RegW = 0 then Goto loopfile;  //no key pressed
        if RegW = 1 then
        begin
        Lcd_Cmd(LCD_CLEAR);
        
loopx:

        if (Data[Step] > $1F) and ( Data[Step+2]= $00) then
        begin
          step := step + 32;
          goto loopx;
        end;
          if (Data[Step] > $1F) and (Data[Step + 2] > $1F) then
          begin
            num:=1;
            for i := step to step + 11 do
            begin
              if num = 9 then
              begin
                Lcd_chr(1,num,'.');
                inc(num);
              end;
                Lcd_chr(1,num,Data[i]);
                inc(num);
            end;
            ReadSectorStartFile;    // read number of sector start file
            step := step + 32;
            if step = 512 then      // read root sector + 1
            begin
              inc(rds);
              tmp := Sd_Read_Sector(RootDirStart + rds, Data);
              step := 0;
            end;
          end;

        If Data[Step] = $00 then     // if no char no file
        begin
          Lcd_Cmd(LCD_CLEAR);
          Lcd_Out(1,1,'No file');
          RegW := 0;
          goto loop1;
        end;
       end;
       RegW := 0;
       goto loopFile;
      end;
      
Error1:
    Lcd_Cmd(Lcd_Clear);
    Lcd_Out(1,1,'SD Init Error');
    Del1Sec;
    goto Error1;

Error2:
    Lcd_Cmd(Lcd_Clear);
    Lcd_Out(1,1,'SD Read Error');
    Lcd_Out(2,1,'Reformat SD card');
    Del1Sec;
    goto error2;

end.
Best regards

ValterG

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

Re: SD card - display filename of all files

#2 Post by zristic » 26 Sep 2005 15:00

Very useful, thanks. Can we distribute this with the compiler?

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

#3 Post by ValterG » 26 Sep 2005 17:56

Yes, this is freeware

ValterG

galea.dk
Posts: 3
Joined: 30 Jan 2006 23:24
Contact:

Could not get this code working

#4 Post by galea.dk » 30 Jan 2006 23:30

BUT! as you have guessed I found out why!

before the line
tmp := Sd_Read_Sector(RootDirStart, Data); //read rootdir
I inserted
RootDirStart := RootDirStart + 1;
And suddenly I could see all the files on my Nokia 32Mbyte MMC card

I will do some more check's on fat, and should I stumble on something GOOD - I will do a repost!

hedisurfer
Posts: 79
Joined: 07 Nov 2009 22:36

#5 Post by hedisurfer » 16 Nov 2009 16:13

Does anybody migrate this code to C ?

Post Reply

Return to “mikroPascal Wish List”