SD Card to RS-232

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

SD Card to RS-232

Post by Widgetman » Sun Dec 16, 2007 7:46 pm

Hi,
I am new to using the Swordfish compiler, and was wondering if there might be some examples out there showing me how to access a SD Card, and possibly send data from a file on the SD Card out the serial port. I also wanted to know if there are any examples showing how to support Z Modem Protocol. Any thoughts or help would be greatly appreciated.

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sun Dec 16, 2007 9:03 pm

Hello and welcome to the forum. The following code might get you started with reading from an SD card and sending it to the serial port.

Code: Select all

Device = 18F4620
Clock =  10

#option SD_SPI = MSSP
#option SD_SUPPORT_SUB_DIRECTORIES = True
Include "SDFileSystem.bas"
Include "USART.bas"
Include "Convert.bas"

Dim Index As LongWord
Dim ItemBoolean As Boolean
Dim ItemBit As Bit
Dim ItemByte As Byte
Dim ItemChar As Char
Dim ItemShortInt As ShortInt
Dim ItemInteger As Integer
Dim ItemWord As Word
Dim ItemLongInt As LongInt
Dim ItemLongWord As LongWord
Dim ItemFloat As Float

DelayMS(200)

USART.SetBaudrate(br38400)

USART.Write("Insert SD/MMC:",13,10)

Repeat
Until SD.Init(spiOscDiv16)

USART.Write("Opening File:",13,10)
SD.NewFile("HELLO.TXT")

ItemBoolean = True
ItemBit = 1
ItemByte = 20
ItemChar = "A"
ItemShortInt = -100
ItemInteger = -30000
ItemWord = 60000
ItemLongInt = -2000000000
ItemLongWord = 4000000000
ItemFloat = 1234.56789

USART.Write("Writing File:",13,10)
For Index = 1 To 512
   SD.Write(ItemBoolean, ItemBit, ItemByte, ItemChar, ItemShortInt, ItemInteger, ItemWord, ItemLongInt, ItemLongWord, ItemFloat)
Next

USART.Write("Closing File:",13,10)
SD.CloseFile

SD.OpenFile("HELLO.TXT")
For Index = 1 To 512
   SD.Read(ItemBoolean, ItemBit, ItemByte, ItemChar, ItemShortInt, ItemInteger, ItemWord, ItemLongInt, ItemLongWord, ItemFloat)
   USART.Write(DecToStr(ItemBoolean), " ", DecToStr(ItemBit), " ", DecToStr(ItemByte), " ", ItemChar, " ", DecToStr(ItemShortInt), " ", DecToStr(ItemInteger), " ", DecToStr(ItemWord), " ", DecToStr(ItemLongInt), " ", DecToStr(ItemLongWord), " ", FloatToStr(ItemFloat, 5), 13, 10)
Next

USART.Write("Finished:",13,10,13,10)
I am not familiar with the Z Modem Protocol, but if you let me know more about it, I might be able to help. If you want an example of using more of the features of the SD module, please ask - I haven't posted it here at the moment as it is quite long.

Kind regards,

Steve

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Sun Dec 16, 2007 9:18 pm

Hi,
Thanks a bunch Steven I think that will point me in the right direction. I had first started this idea using a PIC C compiler, but after many hours of aggravation I decided Swordfish was a better way to go. I hope it won't take me long to come up to speed on the compiler. It looks like a lot of fun. I have been playing with PIC parts since the 90's and still am amazed at how much I can do with a 3.00 part. I just ordered my compiler so I can't compile this example yet, but I should have it in a few days.
Thanks

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Compiler issue with example

Post by Widgetman » Thu Dec 20, 2007 7:30 pm

Hi Steven
I got my compiler in today and tried to compile the example you gave me for talking with a Sd card. I am getting a compile error TXIF not previously defined. Listed below is the code I want to start with. Is there any place that defines this error 113 ?
I tried the help but did not see it. Any thoughts or help would be appreciated.
Thanks


Device = 18F67J60
Clock = 25

#option SD_SPI = MSSP
#option SD_SUPPORT_SUB_DIRECTORIES = True
Include "USART.bas"
Include "SDFileSystem.bas"

Include "Convert.bas"

Dim Index As LongWord
Dim ItemBoolean As Boolean
Dim ItemBit As Bit
Dim ItemByte As Byte
Dim ItemChar As Char
Dim ItemShortInt As ShortInt
Dim ItemInteger As Integer
Dim ItemWord As Word
Dim ItemLongInt As LongInt
Dim ItemLongWord As LongWord
Dim ItemFloat As Float

DelayMS(200)

USART.SetBaudrate(br115200)

USART.Write("Insert SD/MMC:",13,10)

Repeat
Until SD.Init(spiOscDiv16)

USART.Write("Opening File:",13,10)
SD.NewFile("HELLO.TXT")

ItemBoolean = True
ItemBit = 1
ItemByte = 20
ItemChar = "A"
ItemShortInt = -100
ItemInteger = -30000
ItemWord = 60000
ItemLongInt = -2000000000
ItemLongWord = 4000000000
ItemFloat = 1234.56789

USART.Write("Writing File:",13,10)
For Index = 1 To 512
SD.Write(ItemBoolean, ItemBit, ItemByte, ItemChar, ItemShortInt, ItemInteger, ItemWord, ItemLongInt, ItemLongWord, ItemFloat)
Next

USART.Write("Closing File:",13,10)
SD.CloseFile

SD.OpenFile("HELLO.TXT")
For Index = 1 To 512
SD.Read(ItemBoolean, ItemBit, ItemByte, ItemChar, ItemShortInt, ItemInteger, ItemWord, ItemLongInt, ItemLongWord, ItemFloat)
USART.Write(DecToStr(ItemBoolean), " ", DecToStr(ItemBit), " ", DecToStr(ItemByte), " ", ItemChar, " ", DecToStr(ItemShortInt), " ", DecToStr(ItemInteger), " ", DecToStr(ItemWord), " ", DecToStr(ItemLongInt), " ", DecToStr(ItemLongWord), " ", FloatToStr(ItemFloat, 5), 13, 10)
Next

USART.Write("Finished:",13,10,13,10)

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Error 113

Post by Widgetman » Fri Dec 21, 2007 7:47 pm

Hi,
I have narrowed this error down to being related to using the 18F67J60 part. If I compile this code as a 18F25J10 part it compiles just fine. Any ideas on how to get the 18F67J60 to work ?
I have to use that part for my project I am doing.
Thanks

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sat Dec 22, 2007 1:34 am

I don't know about the particular PIC but "yo2lio" in the forums has developed a lot of code using these "J" devices.

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Getting Closer To Solution I hope

Post by Widgetman » Sat Dec 22, 2007 2:18 am

Well it seems if I comment out the USART.Write lines it will compile error free
I left in USART.SetBaudrate(br115200) so it would use the module at least 1 time on compile.

Any thoughts as to why I can not use the USART.Write function with a 18F67J60, but I can with a 18F25J10 ?

Any help would be appreciated. I did not intend to have to debug compiler issues with something so simple.
Thanks

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sat Dec 22, 2007 7:43 am

This is untested and not guaranteed in any way, but you could try the following:

i) Find the USART.bas file in the Mecanique/Swordfish/Library folder (either in Programs folder or ProgramData folder for Vista)
ii) Copy this into your own UserLibrary Folder and open this file
iii) Find the section near the top:

Code: Select all

// alias public interrupt flags...
public dim
   RCIF as PIR.5,  // receive buffer full 
   TXIF as PIR.4,  // transmit buffer empty 
   RCIE as PIE.5,  // receive interrupt enable
   TXIE as PIE.4,  // transmit interrupt enable
   RCIP as IPR.5,  // receive interrupt priority
   TXIP as IPR.4   // transmit interrupt priority
and change to:

Code: Select all

// alias public interrupt flags...
public dim
   RCIF as 5,  // receive buffer full 
   TXIF as 4,  // transmit buffer empty 
   RCIE as 5,  // receive interrupt enable
   TXIE as 4,  // transmit interrupt enable
   RCIP as 5,  // receive interrupt priority
   TXIP as 4   // transmit interrupt priority
Please let me know if this works or not - no promises!!

Regards,

Steve

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Sat Dec 22, 2007 3:01 pm

Hi Steven,
Thanks for the reply and help. I am pulling my hair out trying to figure this out. I tried what you suggested and got a compile error "Invalid expression"

I really think it has something to do with the library file for the 18F67J60 because I can use the 18F25J10 and it works fine with no changes to USART.bas

Unfortunately I am no up to speed using the Swordfish compiler so I have not forund a work around yet. If anyone has any ideas it would be appreciated. I have a hardware development board I am using made by CCS so I am kinda stuck using the 18F67J60.
Thanks

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Sat Dec 22, 2007 3:11 pm

Hi Steven
I figured out a way to get the example to compile, but I am not sure yet it will work correctly
Listed below is the subroutine I made a change in to get it working.
Any ideas why different parts would passin a DIM statement of TXIF as PIR.4 and the 18F67J60 file wont ?


Sub WriteItem(pText As String)
FSR0 = AddressOf(pText)
#if WDT
ASM
movf POSTINC0, W
bz $ + 12
ClrWDT
btfss PIR, 4 // Use to be PIR,TXIF
bra $ - 4
movwf TXRegister
bra $ - 12
End ASM
#else
ASM
movf POSTINC0, W
bz $ + 10
btfss PIR, 4 // Use to be PIR,TXIF
bra $ - 2
movwf TXRegister
bra $ - 10
End ASM
#endif
End Sub

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

SD Card Example

Post by Widgetman » Sat Dec 22, 2007 4:02 pm

Hi Steven
I was able to run the example you sent me for the SD Card to RS-232
The results I got was not what was expected though. I was wondering if you have any examples of reading FAT16 or FAT 32 directory of a card so I can verify my hardware is actually working with my software. I can format a card on the PC as FAT16 or FAT32 then drop a binary file on the card. I am trying to then read the directory of the card and send the results over RS-232. I did play with the example you provided for SD File System 2.0 on another link, but that also did not seem to do much. I just want a place to start from so I know I have a good base to build my application from. Any help or examples would be greatly appreciated.
Thanks

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Sat Dec 22, 2007 7:27 pm

Hi Steven,
I put together a simple routine based on what you gave me. I am trying to open a file with a specific name on a Sd Card and print the bytes of the file. When I run this test application it prints all 0's

Am I missing some sequence here to access a file using the SDFileSystem.bas routine to read a byte ?

Any thoughts or help would be appreciated.
Thanks
Device = 18F67J60
//Device = 18F25J10
//Device = 18F84J90
Clock = 25.00000
Config FOSC = HSPLL
Config FOSC2 = ON
Config WDTPS = 1024


#option SD_SPI = MSSP
#option SD_SUPPORT_SUB_DIRECTORIES = True



Include "SDFileSystem.bas"
Include "USART.bas"
Include "Convert.bas"



Dim Index As LongWord

Dim ItemByte As Byte


DelayMS(200)


USART.SetBaudrate(br115200)

USART.Write("Insert SD/MMC:", 13, 10)

Repeat
Until SD.Init(spiOscDiv16)

USART.Write("Opening File:",13,10)


SD.OpenFile("Test.bin")
For Index = 1 To 512
ItemByte = SD.ReadByte
USART.Write(DecToStr(ItemByte), 13, 10)
Next


SD.CloseFile

USART.Write("Finished:",13,10,13,10)

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

Post by Widgetman » Sat Dec 22, 2007 9:46 pm

Hi Steven,
I was able to cut and paste together the example listed below which will format my SD card now, then write a file to it, and read it back. I still have a question though. I need to work with Binary files on this home project I am creating. Is there support in SDFileSystem.bas to handle binary files ? I tried to do a read only of a file I copied to my SD Card from my PC, and it did not display any bytes. Please forgive my lack of skill with this compiler.

Any thoughts would be appreciated.
Thanks
Device = 18F67J60

Clock = 25.00000
Config FOSC = HSPLL
Config FOSC2 = ON
Config WDTPS = 1024


#option SD_SPI = MSSP
#option SD_SUPPORT_SUB_DIRECTORIES = True



Include "SDFileSystem.bas"
Include "USART.bas"
Include "Convert.bas"

Public Const
// MSSP clock speeds...
spiOscDiv64 = %10, // SPI master mode, clock = Fosc/64
spiOscDiv16 = %01, // SPI master mode, clock = Fosc/16
spiOscDiv4 = %00, // SPI master mode, clock = Fosc/04
// Dir search & Rename types...
sdFile = 0,
sdDirectory = 1,
// Dir search directions...
dirFirst = 0,
dirNext = 1,
dirPrevious = 2,
// ChDir commands...
cdDotDot = 0, // Change to parent directory
cdUp = 0, // Change to parent directory
cdBS = 1, // Change to Root Directory
cdRoot = 1, // Change to Root Directory
// Error codes...
errOK = $00,
errDiskFull = $01,
errNotFound = $02,
errRootDirFull = $03,
errDirNotEmpty = $04,
errExists = $05,
errInUse = $06,
errRWError = $07,
errFileNotOpen = $08,
errBeyondEOF = $09

Dim RED_LED As PORTB.5
Dim YELLOW_LED As PORTB.4

Dim Index As LongWord

Dim Response As Byte

Response = 0

DelayMS(200)


USART.SetBaudrate(br115200)

// initialise card...
If Not SD.Init(spiOscDiv16) Then
USART.Write("Init Failed:", 13, 10)
Low(RED_LED)
DelayMS(3000)
High(RED_LED)
Exit
Else
USART.Write("Init OK:", 13, 10)
Low(YELLOW_LED)
DelayMS(1000)
SD.QuickFormat()
USART.Write("Quick Format Finished:",13,10)
High(YELLOW_LED)
//write data to SD card...
USART.Write("Writing data, please wait...", 13, 10)
Endif
If SD.NewFile("test.txt") = errOK Then
For Index = 0 To 255
SD.Write("Line ",DecToStr(Index,3), 13, 10)
Next
SD.CloseFile

Endif
USART.Write("Reading data...", 13, 10)
If SD.OpenFile("test.txt") = errOK Then
Repeat
USART.Write(SD.ReadByte())
Low(YELLOW_LED)

Until SD.EOF
SD.CloseFile
High(YELLOW_LED)
USART.Write("Finished.", 13, 10)
Endif

Widgetman
Posts: 136
Joined: Sun Dec 16, 2007 7:39 pm
Location: Florida

FAT in Windows Same as FAT 16 ?

Post by Widgetman » Sat Dec 22, 2007 10:12 pm

Hi,
I have narrowed down my issue not to the fact that if I quickformat my SD Card, and write out a test file it works fine. I can also read the Sd Card with XP and open the test file. If however I add a file to my SD card after it is formatted by SD.QuickFormat, and I try to open the file using SD.Openfile routines I always get a EOF error. Is there something else I have to initialize when trying to read a file after it was stored by the PC ?

Thanks

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Sat Dec 22, 2007 10:53 pm

Sorry not to have picked this thread up today - I've been away from home since this morning. You have made good progress. I don't know why you are having problems with files written with XP if I'm honest, but I wonder it is due to long file names? These are not supported by the library. When creating the file in XP, can I suggest the following:

i) create the file somewhere else other than directly to the SD card,
ii) ensure that the card filename and extension are upper case and no longer than the 8:3 DOS format,
iii) finally copy or drag the file to the SD card.

If this still doesn't fix it, let me know,

Steve

Post Reply