Shop OBEX P1 Docs P2 Docs Learn Events
Converting a DEC var into a HEX var — Parallax Forums

Converting a DEC var into a HEX var

xanatosxanatos Posts: 1,120
edited 2011-10-27 11:47 in BASIC Stamp
Hi,

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

Sign In or Register to comment.