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