MicroSD - SD card FAT32 newbie


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2012
    Location
    Ptolemaida
    Posts
    50

    Default MicroSD - SD card FAT32 newbie

    Hello there,
    Im trying 4-5 days now to understand how to read and write a .txt file from a microSD card with SPI method.
    I have read many posts here on melabs forums but i still didn't manage to handle a read/write at the card.

    I used PIC18F26K22 with osc 8MHz x4PLL = 32Mhz. I use Hserout for debugging and hardwareSPI for the data communication betwen the MCU and the card, which are
    Code:
    ' Alias PIC pins and registers
    SDO		Var	PORTC.5	' SPI data out
    SDO_TRIS	Var	TRISC.5	' SPI data out direction
    
    SDI		Var	PORTC.4	' SPI data in
    SDI_TRIS	Var	TRISC.4	' SPI data in direction
    
    SCL		Var	PORTC.3	' SPI clock
    SCL_TRIS	Var	TRISC.3	' SPI clock direction
    
    SD_CS		Var	PORTC.2	' SD card chip select
    SD_CS_TRIS	Var	TRISC.2	' SD card chip select direction
    I want to work with FAT32 format for my project and just to read for starters a txt file. I used a file i found in a post SDFSHC32D.bas and tried to adjust it with to my MCU. There was 2 pins SD_WE and SD_CD that i changed them into:

    Code:
    'SD_CD		Var	PORTB.4	' SD card detect
    'SD_CD_TRIS	Var	TRISB.4	' SD card detect direction
    
    SD_CD       var bit  ' SD card detect Pseudo bit
    SD_CD_TRIS  var bit  ' SD card detect direction Pseudo bit
    SD_CD_used	Var	Bit	'flag to determine if CD is used or not
    SD_CD_used = 0	'SD_CD not used
    
    'SD_WE		Var	PORTA.4	' SD card write protect
    'SD_WE_TRIS	Var	TRISA.4	' SD card write protect direction
    
    SD_WE       var bit  ' SD card write protect Pseudo bit
    SD_WE_TRIS  var bit  ' SD card write protect direction Pseudo bit
    SD_WE_used	var	bit	'flag to determine if WE is used or not
    SD_WE_used = 0	'SD_WE not used
    Do you think this could cause any problems ?? The thing is that i dont want to use Card Detection or Write Protection... I didn't know any other way to skip them so i did change them into bit variables



    My code is this:

    Code:
    'DEFINES  
    
    include "modedefs.bas"
    include "fuses_18f26k22.bas"
    include "eusart_defines_ep7.bas"
    DEFINE OSC 8
    
    
    
    '  REGISTERS 
    TRISA= %00000000  
    TRISB= %00000011
    TRISC= %00010000
    'CMCON= 7 'Comparator module OFF
    INTCON=  %11000000 'Set Pheripheral Interrupts And INT0IE
    INTCON2.7= 1
    'INTCON3.3= 1
    'PIE1.5= 1 'RC1IE: EUSART1 Receive Interrupt Enable bit
    ANSELA=0
    ANSELB=0
    ANSELC=0
    
    include "SDFSHC32D.bas"
    SDC_UseHardSPI = TRUE
    
    
    '   Initialization  
    Init:
      PORTB= 0
      PORTA= 0
      PORTC= 0
      hserout ["Init routine: OK!",13,10]
      PAUSE 100
    
    ' Main 
    Main:
      hserout ["Before FSInit ",dec fat_error]
      Gosub FSInit
      pause 10
      Gosub FSInit
      hserout ["FSInit FAT_error= ",dec fat_error]
    	If (FAT_error != 0) Then 
    	  hserout ["FSInit FAT_error= ",dec fat_error]
    	endif
      FAT_FileName[0] = "N" 
      FAT_FileName[1] = "A" 
      FAT_FileName[2] = "M"
    	FAT_FileName[3] = "E" 
      FAT_FileName[4] = "S" 
      FAT_FileName[5] = " "
    	FAT_FileName[6] = " " 
      FAT_FileName[7] = " "
    	FAT_FileName[8] = "T" 
      FAT_FileName[9] = "X" 
      FAT_FileName[10]= "T"    
      
      FAT_mode = "r"		' Read mode
      Gosub FSfopen
      hserout ["FSOpen FAT_error= ",dec fat_error]
    	If (FAT_error != 0) Then
    	  hserout ["FSOpen FAT_error= ",dec fat_error]
      endif
      FAT_count = 1		' Read 1 byte to buffer at a time
      Gosub FSfread
    	While (FAT_error = 0)
    		Hserout [FAT_dest[0]]
    		FAT_count = 1	' Read 1 byte to buffer at a time
        Gosub FSfread
    	Wend  
    	pause 500
    
    goto Main                        
    end
    I hope someone could drop any ideas because im new with Cards and im stuck !!!

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: MicroSD - SD card FAT32 newbie

    What is the error shown?
    But, as far as I know you need to run the MCU at 40MHz plus.
    and
    I will suggest you get this working with the code from MeLabs first.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Apr 2012
    Location
    Ptolemaida
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: MicroSD - SD card FAT32 newbie

    Hello mackrackit,
    i found that there is a problem with FSinit subroutine, pic's programm stuck in waitforcmdtimeout loop of SDFSHC32d file without returning to main programm. I tried a fix that a user(Alexey) did and worked out!!
    ....
    ....
    loopcount var byte
    loopcount= 0
    waitforcmdtimeout:
    if loopcount = 255 then
    SD_CS = 1
    return
    endif
    loopcount = loopcount +1 'ExtraCode
    ....
    ....
    But i got responce :
    FAT_error= 6 SDC_status= 1 SDC_response= 255
    FAT_error= 7 SDC_status= 1 SDC_response= 255

    Also i forgot to say that i run the program at 32Mhz but still same results. Q: Is there need of 40Mhz ++ speeds ?? Isn't 32Mhz enough ?
    I think MeLabs has code only for FAT16 and not for FAT32 example. Anyhow i'll use your advise and try to use one FAT16 example when i get my hands on 1 2Gb or less microSD

  4. #4
    Join Date
    Apr 2012
    Location
    Ptolemaida
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: MicroSD - SD card FAT32 newbie

    As it seems it's a pitty... But picbasic can't handle SD/microSD FAT32 formated cards yet... I hope someone in the future will add Statements, include files etc etc for this work.
    One man's magic is ANOTHER man's engineering.
    Supernatural is a null word

Similar Threads

  1. SDHC (FAT32 for SD cards)
    By mackrackit in forum Code Examples
    Replies: 22
    Last Post: - 15th October 2023, 20:16
  2. FAT32 SD cards use with sdfshc32d.pbp
    By Alexey in forum PBP Extensions
    Replies: 22
    Last Post: - 10th March 2015, 16:17
  3. MicroSD Cards and SDFS
    By rsocor01 in forum General
    Replies: 33
    Last Post: - 28th August 2010, 06:53
  4. yes card
    By jonas2 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th March 2007, 18:25
  5. SD-Card
    By glaubertadeu in forum General
    Replies: 1
    Last Post: - 27th April 2005, 15:57

Members who have read this thread : 2

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts