SD Card Library files can only be opened by creating program

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

SD Card Library files can only be opened by creating program

Post by TonyR » Sat Feb 18, 2012 2:37 am

Hi All,

Using SDfilesystem I can Quickformat and create an SD card file, save data in it and then re-open the file and read the data back no problems - if the save and open is in the same program.

But if I recompile exactly the same prog. above (without Quickformat) the SD card initialises OK but SD.OpenFile returns an error.

I want to be able to save a file on the SD card then be able to read it back at any time in the future.

Does any one have any clues .... thanks heaps

Code: Select all


'  Here is code to save and recall which works but after
'  commenting out quickformat and running again fails
'
'  RLABS is the SDcardfile system with its 4 pins remapped
'  
'
Device = 18F87J50
Clock = 48
Config FOSC = INTOSCPLL, PLLDIV = 2, CPUDIV = OSC1

#option SD_SPI = SW
Include "RLABS SDFileSystem.bas"
Include "usart2.bas"
Include "Convert.bas"
 
Dim n As Integer

OSCCON = %01111100  
OSCTUNE.6 = 1   

USART2.SetBaudrate(br115200)  

DelayMS(500)     ' allow PIC to stabilize

'
' Report progress of code to dumb terminal on Com2 at 115200
'
USART2.Write(27,"[2J", 27,"[f" )  ' CLS
USART2.Write("Create an SD card file", 13, 10) 
'
' Format SD card
'   
If SD.Init() Then
   USART2.Write("Formatting MicroSD card, please wait...", 13, 10)   
   QuickFormat
   USART2.Write("Format done OK", 13, 10)  
Else
   USART2.Write("SD Init Error, no formatting", 13, 10)  
End If
DelayMS(500)
' 
'
' Create 200 byte file
'  
' 
If SD.NewFile("sample.txt") = errOK Then
  For n = 0 To 200            
    SD.Write(n)
  Next 
  SD.CloseFile 
  USART2.Write("Successfully created sample.txt", 13, 10)  
Else
  USART2.Write("Error creating sample.txt file", 13, 10)  
End If   

'
' Open, print and close the file to prove its there at this moment
'
'

If SD.OpenFile("sample.txt") = errOK Then
       Repeat
         n = SD.ReadByte()
         USART2.Write("Byte from sample.txt ", DecToStr(n), 13, 10)   
       Until SD.EOF
      SD.CloseFile
Else    
     USART2.Write("Error opening sample.txt", 13, 10)   
     DelayMS(5000) 
EndIf  


TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

PS

Post by TonyR » Sat Feb 18, 2012 2:40 am

Sorry I should have added that the create file part of the code is also removed when recompiled so I should be able to read the file I wrote since the SD card is non volatile ?

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Post by bitfogav » Sat Feb 18, 2012 10:32 am

I think you need to call the SD.Init() atleast once to reinitialise the SD Card, before you call the SD.OpenFile().

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Sat Feb 18, 2012 11:34 pm

Thanks bitfogav.

There actually is. I used that block of posted code to format the SD card, then create the file, then read it back to confirm its there - which works.

Then stage two, I commented out the quickformat, but left in the sd.init, commented out the file creation stuff then recompiled.

I wish I could read what kind of error was happening.

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Problem solved

Post by TonyR » Sun Feb 19, 2012 12:24 am

My MicroSD card is soldered onto PIC due to whole assembly needing to be small.

When I reburnt the PIC with the code to read the pre-exisiting SD card file the PIC was starting to run the Quickformat from the old code, before the reprogramming stopped it and the new burn happened.

Its solved by putting a 30 second delay in before the Quickformat, to give the re-programming time to happen before the old code in the PIC starts up.

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Post by bitfogav » Sun Feb 19, 2012 1:11 am

I see what you are trying to do now?, why dont you just have a switch or jumper connected to one of the pins on the MCU, and then make up some routines in code?.

for example if you select a pin on the MCU and connect it to a switch, if you press the switch and hold it when you apply power to the MCU:
Then have a routine in code to SD.Init(), format the SD and SD.Write()

Then you could reset the power and then power up the MCU again with the switch not pressed and then have a different routine which:
SD.init() and SD.Read()
This way you have one piece of code to do both operations, and you dont have to keep selecting different programmes and reprogramming the MCU.

Example code:

Code: Select all

 
    If switch = 0 Then
       If SD.Init() Then 
         USART2.Write("Formatting MicroSD card, please wait...", 13, 10)    
         QuickFormat 
         USART2.Write("Format done OK", 13, 10)  
       Else 
         USART2.Write("SD Init Error, no formatting", 13, 10)  
       End If 
        DelayMS(500)
       If SD.NewFile("sample.txt") = errOK Then 
         For n = 0 To 200            
         SD.Write(n) 
       Next 
         SD.CloseFile 
         USART2.Write("Successfully created sample.txt", 13, 10)  
       Else 
        USART2.Write("Error creating sample.txt file", 13, 10)  
       End If 
    Else 
       SD.Init() 
       If SD.OpenFile("sample.txt") = errOK Then 
         Repeat 
           n = SD.ReadByte() 
           USART2.Write("Byte from sample.txt ", DecToStr(n), 13, 10)    
           Until SD.EOF 
           SD.CloseFile 
         Else    
           USART2.Write("Error opening sample.txt", 13, 10)    
           DelayMS(5000) 
         EndIf
    
    EndIf
I wish I could read what kind of error was happening.
By the way the SD.Init() routine can return error codes which could be used to assist in what errors are happening.

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Sun Feb 19, 2012 11:30 pm

Good suggestion, if I was going to do it all the time I would do that but that test was a one off. I just needed to verify that I could write a file with the SD Library and read it back later to confirm I can use the same hardware for a future project. I hate assuming something will work then down the track finding it doesn't :-)

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Post by bitfogav » Mon Feb 20, 2012 12:32 am

Cool :) I always enjoy tinkering with certain applications and then using them in future designs.

Post Reply