PDA

View Full Version : MicroSD - SD card FAT32 newbie



xapmanis
- 17th September 2013, 15:06
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


' 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:



'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:




'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 !!!

mackrackit
- 17th September 2013, 23:55
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.

xapmanis
- 18th September 2013, 07:37
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

xapmanis
- 19th September 2013, 15:51
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.