Unknown number of bytes. Padding with '0'

Hey guys,

I have recently got my VDRIVE working, and am ready to start using it for data logging. My problem starts with my real time clock (DS1307); The number of bytes changes.

I have written my code to turn the BCD from the RTC into binary, and when turned into ASCII for writing to the VDRIVE, the number can either be single or double digit. The VDRIVE reads this as either one or two bytes.

How can I tell the VDRIVE how many bytes to write?

I was thinking padding with zeros, but I wouldn't know how to go about it.

While writing this, I thought of sending null bytes. Or maybe printing the CR at the start of the data, and then having extra bytes at the end in the form of spaces. These spaces would just get cut off if they weren't needed / wont fit.
Hopefully my code copies okay...
Code:
' microLOG 
' Main Box 
' Daniel Collins 
 
symbol first_byte = b0 
symbol point = b1 
symbol temp = b2 
symbol loopcounter = b3 
 
Setup: 
hsersetup b9600_4, %01 
hi2csetup i2cmaster, %11010001, i2cslow, i2cbyte 
 
Init: 
pause 100 
 
 
init_lcd: 
sertxd ("Writing to LCD",CR,LF) 
serout 0, n600, ("*L", $80, "    micr") 
pause 50 
serout 0, n600, ("*L", "oLOG    ") 
sertxd ("Setting up VDRIVE",CR,LF) 
init_vdrive: 
sertxd ("<sent> E",CR,LF) 
hserout 0, ("E",CR) 
gosub get_response 
if first_byte <> "E" then init_vdrive 
pause 10 
gosub get_time 
gosub write_power 
  
 
Main: 
serin [200, skip], 2, n600, (":Q"), b4, b5  
sertxd ("HOPE",#b4," ",#b5,CR,LF) 
gosub get_time 
gosub openwriteclose 
 
skip: 
readadc 0, b14 
if b14 > 40 then gosub buttons 
 
serout 1, n600, (":E") 
 
goto main 
 
 
get_time: 
sertxd ("Getting Time") 
hi2cin 6, (b13) 
let b13 = bcdtobin b13 
hi2cin 5, (b12) 
let b12 = bcdtobin b12 
hi2cin 4, (b11) 
let b11 = bcdtobin b11 
hi2cin 2, (b10) 
let b10 = bcdtobin b10 
hi2cin 1, (b9) 
let b9 = bcdtobin b9 
hi2cin 0, (b8) 
let b8 = bcdtobin b8 
 
sertxd (#b13," ",#b12," ",#b11," ",#b10," ",#b9," ",#b8,CR,LF) 
 
return 
 
 
get_response: 
pause 1000 
sertxd ("<Response> ") 
point = 0 
get point, first_byte 
do 
    get point, temp 
    sertxd (temp) 
    inc point 
loop while temp <> CR 
sertxd (LF) 
sertxd (CR,LF) 
hserptr = 0 
 
return 
 
 
buttons: 
inc b6 
if b14 < 160 then gosub DS1 
if b14 = 0 then return endif 
if b14 < 230 then gosub DS2 
if b14 > 240 then gosub DS3 
 
return 
 
 
DS1: 
sertxd ("Button 1 was Pressed",CR,LF) 
b14 = 0 
return 
 
DS2: 
sertxd ("Button 2 was Pressed",CR,LF) 
b14 = 0 
return 
 
DS3: 
sertxd ("Button 3 was Pressed",CR,LF) 
b14 = 0 
return 
 
 
openwriteclose: 
sertxd ("<Sent> Check Drive",CR,LF) 
hserout 0,(CR) 
gosub get_response 
if first_byte <> "D" then openwriteclose 
 
sertxd ("<Sent> Open file",CR,LF) 
hserout 0,("opw data.csv",CR) 
gosub get_response 
 
sertxd ("<Sent> write to file",CR,LF) 
hserout 0,("wrf ",$00,$00,$00,$12,CR,#b13,"/",#b12,"/",#b11," ",#b10,":",#b9,":",#b8,",",#b4,",",#b5,",",#b6,$0A,CR) 
gosub get_response 
 
sertxd ("<Sent> Close file",CR,LF) 
hserout 0,("clf data.csv",CR) 
gosub get_response 
     
return 
 
 
write_power: 
sertxd ("Writing power-on time",CR,LF) 
sertxd ("<Sent> Check Drive",CR,LF) 
hserout 0,(CR) 
gosub get_response 
if first_byte <> "D" then openwriteclose 
 
sertxd ("<Sent> Open file",CR,LF) 
hserout 0,("opw data.csv",CR) 
gosub get_response 
 
sertxd ("<Sent> write to file",CR,LF) 
hserout 0,("wrf ",$00,$00,$00,$12,CR,#b13,"/",#b12,"/",#b11," ",#b10,":",#b9,":",#b8," POWER",$0A,CR) 
gosub get_response 
 
sertxd ("<Sent> Close file",CR,LF) 
hserout 0,("clf data.csv",CR) 
gosub get_response 
     
return
Thanks,
 
Last edited:

westaust55

Moderator
If you are only displaying the time from the RTC to the LCD display and not using it elsewhere then the BCDTOASCII command may be a usefual alternative.

For example:
b0 = $23 ; or 35
bcdtoASCII b0 , b2, b3
SEROUT 7, N2400, (b2, b3)

LCD display will show "23"

and any leading zero is displayed
so if b0 = $03
display shows "03"

if you want a blank then you could test variable b2 for the value $30 (or decimal 48) and change the value to " ", $20 or decimal 32 so a space is displayed.
 

hippy

Ex-Staff (retired)
What westaust55 says.

BCD data in reading from the RTC case always has two digits, it's just that you are choosing to lose the most significant zero digit using BcdToBin. That means either putting them back in later or using BcdToAscii which is easier all round.
 
The RTC is read, then stored as binary. This is then sent onto the VDRIVE.

I managed to solve the problem by adding extra bytes to the end. I counted the maximum number of bytes the entry could be, then the minimum, and added spaces as necessary. The VDRIVE automatically ignores any bytes you didn't tell it about.

The same goes for my 14M LCD Back pack. I wrote a little extra in to make it ignore any 0s sent to it (%00000000 not ASCII 0).

Thanks for the fast replies guys, This project is going to be entered into a science fair on Thursday, and today is Sunday. Weekend is over, so I'm glad I've finished it.
 
Top