Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Append line of text to file.

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> KokkeKat FAT-free SD card lib
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 751

poland.gif
PostPosted: Thu Dec 25, 2014 3:27 am    Post subject: Append line of text to file. Reply with quote

I built a single-chip decoder Sel Call (using ICP). Added to the RTC and display. Now I would like to write to the card detected events

...and after fiew tests..

Everything work super! Very Happy
Back to top
View user's profile Visit poster's website
amirf

Bascom Member



Joined: 25 Aug 2009
Posts: 154

indonesia.gif
PostPosted: Tue Dec 30, 2014 1:31 pm    Post subject: Reply with quote

Please share it EDC , tq
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 751

poland.gif
PostPosted: Tue Dec 30, 2014 2:32 pm    Post subject: Reply with quote

In the new year I decided to improve not only the skills, but also foreign languages Very Happy I will try.
To write something on the card you have in the beginning to recognize card type and recognize the file type. To do this, run one after the other two sub-programs. "Sdinit" and "Sdinitfs". If at the end of the variable "Sdstatus = 0" means that we can write bytes one after another. Therefore, a temporary variable that rewrite the text you want to save the buffer Overlay I created.
I'm not a specialist and the first time I write something on the card with the use of the library. Surely you can do better.

Code:
$regfile = "m32def.dat"
$crystal = 16000000
$hwstack = 128
$swstack = 64
$framesize = 64

$include "KokkeKat_FAT-free_SD_lib_decl.bas"
' my settings in this config file

'Const Sdinitdetail = 1
'Const Sdfsactive = 1
'Const Sdusefind = 1
'Const Sdusefindstoreentry = 0
'Const Sdusedirlist = 0
'Const Sddirlistarraysize = 12
'Const Sdrmode = 1
'Const Sdwmode = 2
'Const Sduseappend = 1
'Const Sdcleartail = 0
'Const Sdusefsinfo = 1
'Const Sduselfn = 0
'Const Sduselfncompare = 0
'Const Sdusewipe = 0
'Const Sdusedirlistdelete = 0
'Const Sdusesizeinfo = 0
'Const Sdusecrc7 = 0

Dim Temp_string As String * 20
Dim Temp_bufor(20) As Byte At Temp_string Overlay
Dim Send As Byte , Send_len As Byte , Datas As Byte
Dim Something_must_be_saved As Byte                         'for this example
'init card
Lcd "Init SD"
 Gosub Sdinit
If Sdstatus = 0 Then
   Gosub Sdinitfs
End If
Home
 Wait 1
If Sdstatus = 0 Then
 Lcd "Okay!  "
Else
 Lcd "No Card"
End If

'### DOUBLE SPEED of SPI      It is in the declarations but I preferred it to have a test on top
Spsr.0 = 1                                                  ' Double speed (results in f osc / 2)
Spcr.1 = 0                                                  ' f osc / 4
Spcr.0 = 0
'### --------------- ###
Wait 1
Enable Interrupts
Lowerline : Lcd "Waiting..."




'*****************************************************
'*********************** START ***********************
'main loop
Do

  If Something_must_be_saved = 1 Then Gosub Save_to_sd

Loop
End
'************************ END ************************
'*****************************************************
$include "KokkeKat_FAT-free_SD_lib_code.bas"




'zapis na karte
Save_to_sd:

If Sdstatus = 0 Then

 Gosub Sdreadfsinfo
Sdentrynames = "LOGGED     "                                'name of file on card (11 characters)
Sdstartdirclusterd = 0
'     I have RTC
'Sdyear = Year
'Sdmonth = Month
'Sdday = Day
'Sdhours = Hour
'Sdminutes = Minute
'Sdseconds = Second

Gosub Sdpreparetoappend                                     'find file and last record

Sdclosefile = 0                                             ' Always make sure that Sdclosefile = 0 before writing
Sdbyterw = 32                                               ' Write "Space"
Gosub Sdwritebyte                                           'this saves one single byte to card

'#
For Datas = 1 To 3
       'written in a single line will be three consecutive text variables
   Select Case Datas
    Case 1 : Temp_string = Time_string
     Send_len = 20
    Case 2 : Temp_string = Rezultat_string
     Send_len = 20
    Case 3 : Temp_string = Temp_str
     Send_len = Len(temp_str)                               'variables such as different lengths (example)
   End Select

 For Send = 1 To Send_len   'variables such as different lengths (example)
   If Sdclosefile = 0 Then
      Sdbyterw = Temp_bufor(send)
       Gosub Sdwritebyte 'this saves one single byte to card
   Else
   'Handle partition full
   End If

 Next
  If Sdclosefile = 0 Then
      Sdbyterw = 32                                         ' Write "Space"
      Gosub Sdwritebyte   'this saves one single byte to card
   Else
      ' Handle partition full
   End If
Next
'#

    'from this point enter and mark the end of the line
   If Sdclosefile = 0 Then
      Sdbyterw = 13                                         ' Write "Enter"
      Gosub Sdwritebyte
   Else
      ' Handle partition full
   End If

   If Sdclosefile = 0 Then
      Sdbyterw = 10                                         ' Write "LF"   next line
      Gosub Sdwritebyte
   Else
      ' Handle partition full
   End If

Gosub Sdfinalizeafterwriting                                ' After writing the last byte, close the file
Gosub Sdwritefsinfo                                         ' Save the updated fsinfo variables back


End If

Return


On the need to create the file. For example LOGGED.txt. Then rename it so that it does not have an extension. "LOGGED" (11 characters - insert spaces). If you want to use the "normal" name is required unless additional 254B

...and this example of the output and write on the card. You can see three separate variables in one line

Nie 28-12-2014 23:45 401040140234201 Fiat
Nie 28-12-2014 23:46 401040140340240 Ford
Nie 28-12-2014 23:46 403040340340341 Opel
Pon 29-12-2014 00:03 401040140234201 Fiat
Pon 29-12-2014 00:05 401040140340240 Ford
Pon 29-12-2014 00:05 403040340340341 Opel

With PCF8583 + 100year calendar + SelCall Decoder + Kokkekat + LCD program takes ~16KB

Have fun and nice day Very Happy
Back to top
View user's profile Visit poster's website
KokkeKat

Bascom Member



Joined: 08 May 2011
Posts: 59
Location: Stockholm

sweden.gif
PostPosted: Fri Jan 02, 2015 10:09 pm    Post subject: Reply with quote

Great work and thanks for sharing!

Smile

Niclas
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> KokkeKat FAT-free SD card lib All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum