Number to text/character conversion
![72sonett](https://forums.parallax.com/uploads/userpics/784/nLGC5MB5OWPIL.jpg)
I have a real time clock (RTC) and an LCD connected to a BS2sx. Both work OK separately. I have a subroutine that returns year, month, day, hours & minutes from the RTC and another subroutine that sends single characters to the LCD.
This code gets date/ time from the RTC and shows it OK in a DEBUG screen;
Now I want to show date & time on an LCD, like:
How can I do this in a more elegant way?
This code gets date/ time from the RTC and shows it OK in a DEBUG screen;
... GOSUB readRTC DEBUG HOME, "20",HEX2 Year,"/",HEX2 Month,"/",HEX2 Date,"-" DEBUG HEX2 Hours,":",HEX2 Minutes, CR ...as 2014/11/12-20:54
Now I want to show date & time on an LCD, like:
char = "2" ' this is OK GOSUB SendChar char = "0" ' this too GOSUB SendChar char = Year(1) ' this does not work... GOSUB SendChar char = Year(2) ' this does not work... GOSUB SendChar char = "/" ' this is OK GOSUB SendChar ... ' etcTo display correctly on the LCD I need to split the value 14 of the year into the characters ´1´ and ´4´ (similarly for month, day, hour etc). I was thinking of SELECT ... CASE but that would be too long and tedious
How can I do this in a more elegant way?
Comments
EDIT: nvm, do what Mike Green said, I forgot RTCs use BCD and not normal decimal
This now shows "2014/11/12-21:56" on the LCD...
Create a subroutine called SendHex and a byte variable called hexed like this:
hexed VAR byte
SendHex:
char = hexed / 16 + "0"
GOSUB SendChar
char = hexed // 16 + "0"
GOTO SendChar
To output some two-digit numbers from the RTC, do:
hexed = Year
GOSUB SendHex
char = "/"
GOSUB SendChar
hexed = Month
GOSUB SendHex
... and so on.
You can get fancier by storing a format string in the EEPROM along with your program by using DATA statements and READ and WRITE and by writing an interpreter for the format string so that your RTC display might look like:
format DATA "20",1,"/",2,"/",3,"=",4,":",5,0
You'd do:
ptr VAR word
ptr = format
GOSUB SendForm
with:
SendForm:
READ ptr,char
ptr = ptr + 1
IF char = 0 THEN RETURN
IF char >= 32 THEN
GOSUB SendChar
GOTO SendForm
ENDIF
hexed = table(char-1)
GOSUB SendHex
GOTO SendForm
A zero byte in the format string terminates the format.
A byte between 1 and 31 is the index into a table of variable values ("table"). In your case, this is the RTC info.
Any other byte value is sent to the display as is.
In your case, "table" is the same as "Year" and you'd use "table VAR Year" to define that alias.
Yes, I could do that.
But that will go at the expense of program space.
Actually there is also a SmartCard reader attached to the BStamp, as part of an electric gate opening/closing control program. The program has some messages stored in RAM (using PUT). The main loop of the program checks the card reader for a card and displays date & time on the LCD.
When a card is inserted in the reader, the program checks if it is a valid card by comparing two control characters on the card with two characters coded in the program. If the card is OK, date & time are written to the card and the gate is opened/closed, meanwhile displaying appropriate messages on the LCD (using GET).
This takes up slightly less program space as well, great!
But what if ´Month´ were a Nib size variable?