Converting a DEC var into a HEX var
xanatos
Posts: 1,120
Hi,
I would have thought that this would work:
But it doesn't. OK... here's what I have. I get a DEC2 value (charHold) from a subroutine elsewhere. One of the things I need to do with this DEC2 value is stuff it into a DS-1302 to set the clock using the usual Set_Time subroutine that is everywhere for the DS-1302 (see below). Normally, the values are DEC values but stored as HEX... for example, to manually set the clock at program start I can use the following:
But if I set the date by setting the var "year" equal to the DEC output of my subroutine, instead of 11, I get 0B, which is HEX for 11... hence my idea to try setting "year" equal to the HEX2 value of my subroutine's output. I feel stupid. :-) I've been using the 1302 for years - this is the first time I've had to give it data generated from a keypad instead of either DEBUGIN or the program itself...
A search of the PBasic manual turned up nothing except using the formatters in SERIN and SEROUT, or DEBUGIN/OUT. I can DEBUGIN HEX2 year, but I can't set year = HEX2 value. Likewise I found a LOT of stuff on the forums about how the DS-1302 needs the DEC values set as HEX, etc... but nothing I saw explained how to convert from DEC to HEX values.
How do I do this???
I even tried using the formatter in the Set_Time subroutine, changing it from
SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0]
to
SHIFTOUT DataIO, Clock, LSBFIRST, [HEX2 secs, HEX2 mins, HEX2 hrs, HEX2 date, HEX2 month, day, HEX2 year, 0]
But as I'm sure you already know, that didn't work either. :-) I must fundamentally misunderstand where the formatters can be used...
Thanks very much, as usual... I'd be lost without you people!
Dave
Ye Olde RTC Set_Time Subroutine, for reference:
I would have thought that this would work:
year = HEX2 charHold
But it doesn't. OK... here's what I have. I get a DEC2 value (charHold) from a subroutine elsewhere. One of the things I need to do with this DEC2 value is stuff it into a DS-1302 to set the clock using the usual Set_Time subroutine that is everywhere for the DS-1302 (see below). Normally, the values are DEC values but stored as HEX... for example, to manually set the clock at program start I can use the following:
year = $11 ' Set Year month = $10 ' Set Month date = $26 ' Set Date day = 4 ' Set Day [Sun, Mon, Tue, Wed, Thu, Fri, Sat] hrs = $18 ' Set Hours mins = $58 ' Set Minutes secs = $00 ' Set seconds GOSUB Set_Time
But if I set the date by setting the var "year" equal to the DEC output of my subroutine, instead of 11, I get 0B, which is HEX for 11... hence my idea to try setting "year" equal to the HEX2 value of my subroutine's output. I feel stupid. :-) I've been using the 1302 for years - this is the first time I've had to give it data generated from a keypad instead of either DEBUGIN or the program itself...
A search of the PBasic manual turned up nothing except using the formatters in SERIN and SEROUT, or DEBUGIN/OUT. I can DEBUGIN HEX2 year, but I can't set year = HEX2 value. Likewise I found a LOT of stuff on the forums about how the DS-1302 needs the DEC values set as HEX, etc... but nothing I saw explained how to convert from DEC to HEX values.
How do I do this???
I even tried using the formatter in the Set_Time subroutine, changing it from
SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0]
to
SHIFTOUT DataIO, Clock, LSBFIRST, [HEX2 secs, HEX2 mins, HEX2 hrs, HEX2 date, HEX2 month, day, HEX2 year, 0]
But as I'm sure you already know, that didn't work either. :-) I must fundamentally misunderstand where the formatters can be used...
Thanks very much, as usual... I'd be lost without you people!
Dave
Ye Olde RTC Set_Time Subroutine, for reference:
' -----[ Subroutines ]--------------------------------------------------------------------------------------------------------- Set_Time: ' DS1302 Burst Write HIGH CS1302 ' Select DS1302 SHIFTOUT DataIO, Clock, LSBFIRST, [WrBurst] SHIFTOUT DataIO, Clock, LSBFIRST, [secs, mins, hrs, date, month, day, year, 0] LOW CS1302 ' Deselect DS1302 RETURN
Comments
ds1302byte = (decimalByte / 10) << 4 + (decimalByte // 10)
You can convert back through the same sort of process as follows.
decimalByte = (ds1302byte >> 4) * 10 + (ds1302byte & $0F)
Yes, you've noticed that the formatters only work for SERIN / SEROUT and the debug equivalents.
Thanks again. I just found your old (2007) thread on the same subject: http://forums.parallax.com/showthread.php?99190-Convert-a-Decimal-value-to-Hex.&highlight=DS1302+set+keypad
I am using this now and it's running perfectly. Thank you!
Dave