Matt,
In that case I might go back to my earlier approach because I do not
like to have a big Pic collection. I bought the 18F4455 because I
would use a 512 byte array instead of the program memory part I used
in the 16F876a.
I would like to share my baby with some others but I dont know if
there are interested ones.
I 'll take the risk and here is the program I use at this moment:

 -- <title SD_card_3 simplified version just for read functies
-- for test purpose only added include 16f877a_bert
include 16f877a_bert
pin_A1 = input -- used to stop output datastream for a while 0 = stop
pin_C3_direction = output --- SPI
pin_C5_direction = output --- SPI
pin_C4_direction = input  --- SPI

pin_C2_direction = output ---CS for SD card
var volatile bit CS is pin_C2
CS = high

var volatile byte*4 BASIC
var volatile byte*4 MBR  -- is start sector first partition from
MasterBootRecord
var volatile byte MBRr[4] at MBR

var volatile byte *4 FiS -- file size
var volatile byte FiSr[4] at FiS

var volatile word BPS  --nr bytes in each sector, normally 512
var volatile byte BPSr[2] at BPS
var volatile byte SPC  -- nr sectors in each cluster
var volatile byte Sect_nr  -- sector location within the cluster
var volatile word RES  -- nr reserved sectors
var volatile byte RESr[2] at RES
var volatile byte CFA  -- nr  FAT files
var volatile word SPF  -- nr sectors in each FAT
var volatile byte SPFr[2] at SPF
var volatile word RootStart -- start sector file-list with entries
var volatile byte RS[2] at RootStart
var volatile word SIP  -- start sector in root dir
var volatile byte SIPr[2] at SIP

var volatile word SIP_next  -- next start sector in root dir
--- if this is 0xFFFF then there are no further sectors
var volatile byte SIPr_next[2] at SIP_next


var volatile word BLE ----- nr dir entries : nr possible files( names
etc)
var volatile byte BLEr[2] at BLE
var volatile byte EntrSec  -- nr of entries / sector
var volatile word Fat    -- file position in Fat table
var volatile byte Fatr[2] at FAT
var volatile word FatStart  -- sector where fat table starts
var volatile byte FtS[2] at FatStart
var volatile word DataStart -- sector nr where data starts
var volatile byte DS[2] at DataStart
var volatile word RAM = 7500 -- at least 512 from th end of RAM
var volatile byte sd_flag

var volatile byte NMr[12] -- file name
var volatile byte * 4 File_size
var volatile byte FS[4] at File_size

var byte null = 0xFF
--------------------------------------------------
function SPI(byte in x) return byte is
         SSPBUF = x
         while (!BF) loop end loop
         return SSPBUF
end function
-------------------------------------------------
procedure Command(byte in a1,dword in basic, byte in a2) is
-- send a command to the card and sets the SD_flag
var byte a
var byte BS[4] at basic
a1 = a1 + 64
if a2 == 0 then a2 = null else a2 = 0x95
end if
spi(null)
spi(a1)
spi(BS[3])
spi(BS[2])
spi(BS[1])
spi(BS[0])
spi(a2)
spi(null)
SD_flag =spi(null)
end procedure
---------------------------------------------
-- init SD card
procedure SD_init is
SMP = 0
CKE = 0
CKP = 1
SSPM0 = 1
SSPM1 = 0
SSPM2 = 0
SSPM3 = 0
SSPEN = 1
CS = 1
SD_flag = 0
    while SD_flag !=1 loop -- wait for sd_flag == 1
         CS = 1
                for 10 loop
                   SPI(null)
                end loop
         CS = 0
         Command(0,0,1)
    end loop
    while SD_flag != 0 loop -- wait for SD_flag == 0
          Command(1,0,0)
    end loop
end procedure
--------------------------------------------------------------
procedure block_read( dword in basic) is -- reads a block from card
into ram
var byte a
var word i = RAM-- for counter on basic memory place RAM
basic = basic * 512 -- idicate sector nr
command(17,basic,0)  -- if sd_flag != 0 then report read_faillure
while SPI(null) != 0xFE loop end loop
   for 512 loop
         a = SPI(null)
         Program_EEprom_write(i,a)
         i =i + 1
    end loop
SPI(null)
SPI(null)
end procedure
------------------------------------------------
----------------------------------------------------------
procedure block_print (word in xx, word in yy)is -- xx == start yy==
number
var word i -- for counter
i = RAM + xx
var byte it[2] at i
var byte a
   for yy loop
        a = Program_EEprom(i)
        Serial_sw_write(a)   ---- choose which one
        ---Serial_hw_write(a)
        delay_1mS(10)
        i = i + 1
    end loop
end procedure
------------------------------------------------------
Procedure Startup_SDcard is
var byte a
var word i -- for counter

SD_INIT    -- read the Master Boot Record for the start of part 1 boot
record
command(17,0,0)-- if sd_flag != 0 then report read_faillure
i = 0
while SPI(null) != 0xFE loop end loop
   for 512 loop
         a = SPI(null)
      if i == 454 then MBRr[0] = a end if
      if i == 455 then MBRr[1] = a end if
      if i == 456 then MBRr[2] = a end if
      if i == 457 then MBRr[3] = a end if
       i =i + 1
    end loop
SPI(null)
SPI(null)
 -- now read the boot record for onther parameters
command(17,MBR *512,0)-- if sd_flag != 0 then report read_faillure
i = 0
while SPI(null) != 0xFE loop end loop
   for 512 loop
         a = SPI(null)
           if i == 11 then  BPSr[0] = a end if
           if i == 12 then  BPSr[1] = a end if
           if i == 13 then  SPC = a end if
           if i == 14 then  RESr[0] = a end if
           if i == 15 then  RESr[1] = a end if
           if i == 16 then  CFA = a end if
           if i == 17 then  BLEr[0] = a end if
           if i == 18 then  BLEr[1] = a end if
           if i == 22 then  SPFr[0] = a end if
           if i == 23 then  SPFr[1] = a end if
          i =i + 1
    end loop
SPI(null)
SPI(null)

---- now calculate adresses in sectorsnrs
--
EntrSec = BPS/32
-- each entry has 32 bytes, so BPSr/32 is nr of entries/sector for FAT
16 = 16
-- and BLE / (BPSr/32) is de nr of sectors for Root directory
----BLE = BLE/EntrSec
 -- we use the nr of used sectors therefore change total entries in
sectors

FatStart = MBR + RES              -- this is the start of the FAT
table
RootStart = FatStart +(SPF * CFA)  -- this the start sector of the
Root
DataStart = RootStart + BLE/EntrSec -- this is the data start sector
end procedure
---------------------------------------------------------
procedure Entry_read(word in x) is  -- read the file header info
var word i -- for counter
var byte a
basic = RootStart +x/EntrSec   -- the start of the sector
x = (x % EntrSec) * 32  -- the position within the sector
command(17,basic *512,0) -- if sd_flag != 0 then report read_faillure
i = 0
while SPI(null) != 0xFE loop end loop
   for 512 loop
          a = SPI(null)
           if i == x then  NMr[0] = a end if   -- file name 0-11
           if i == x+1 then  NMr[1] = a end if
           if i == x+2 then  NMr[2] = a end if
           if i == x+3 then  NMr[3] = a end if
           if i == x+4 then  NMr[4] = a end if
           if i == x+5 then  NMr[5] = a end if
           if i == x+6 then  NMr[6] = a end if
           if i == x+7 then  NMr[7] = a end if
           if i == x+8 then  NMr[8] = a end if
           if i == x+9 then  NMr[9] = a end if
           if i == x+10 then  NMr[10] = a end if
           if i == x+11 then  NMr[11] = a end if

           if i == x+26 then  SIPr[0] = a end if  -- FAT start cluster
nr
           if i == x+27 then  SIPr[1] = a end if

           if i == x+28 then  FiSr[0] = a end if  -- File size
           if i == x+29 then  FiSr[1] = a end if
           if i == x+30 then  FiSr[2] = a end if  --
           if i == x+31 then  FiSr[3] = a end if

       i =i + 1
    end loop
SPI(null)
SPI(null)
end procedure

procedure load_next_cluster is
-- SIP = start
var word i -- for counter
var byte a
var byte b
FAT = (SIP*2)%512
 basic = FatStart + (SIP*2)/512
 command(17,basic *512,0) -- if sd_flag != 0 then report read_faillure
i = 0
while SPI(null) != 0xFE loop end loop
   for 256 loop
          a = SPI(null)
          b = SPI(null)
           if i == FAT then
                SIPr[0]= a
                SIPr[1]= b
          end if
          i =i + 2
    end loop
SPI(null)
SPI(null)

If SIP > 0xFFF0 then
Serial_sw_write(48)
else
Serial_sw_write(SIP+48)
end if
--Serial_sw_write(SIPr[0])
--Serial_sw_write(SIPr[1])
end procedure

procedure load_next_data is
basic = DataStart + (SIP-2)* SPC
basic = basic + Sect_nr
block_read(basic)
Sect_nr = Sect_nr + 1
    if Sect_nr == SPC then
       load_next_cluster
        Sect_nr = 0

    end if
end procedure
---------------- PROGRAM --------
var byte teller


Startup_SDcard
-- --[                 [Entry_read(1) is reading the second file from
the card.
-- --[                 [0 = first and mainly the name of the card
-- --[                 [if the first character (NMr[0]) is not in
between 32
-- --[                 [and 122 then the next file have to be read
--forever loop
delay_1S(10)

-- basic = FatStart + (SIP*2)/512
-- block_read(basic)
-- block_print(0,512)



Entry_read(1)
-- now he knows the name ande the SIP which is the Fat start
teller= 0
    for 32 loop
        Serial_sw_write(NMr[teller])
        delay_1mS(10)
        teller = teller + 1
    end loop
Sect_nr = 0
delay_1S(1)
Serial_sw_write(SIP+48)


-- now read the data
while SIP < 0xFFF7 loop
 --Serial_sw_write(SIPr[0]+48)
 --Serial_sw_write(SIPr[1]+48)
--delay_1S(1)
load_next_data

block_print(0,512)
--delay_1S(1)

end loop


--end loop





-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to