DEC to HEX
starbot
Posts: 4
I'm using a DS1302 real time clock for a project. The clock is set in hex values I believe, I'm using a DEC variable x to set time, how do i convert this to a HEX to then output it to the 1302. Thanks
Comments
In this·clock code the register addresses are represented in binary(%00010), the control register commands in Hex($80) and the data in decimal (12). It all gets converted to binary. Basically you can send it decimal numbers no problem.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (Jonb) : 4/17/2005 7:00:40 AM GMT
· bcdVal = (decVal / 10 << 4) + (decVal // 10)
You can go the other direction too:
· decVal = (bcdVal.NIB1 * 10) + bcdVal.NIB0
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Code:
setting:
number=-1
y=-1
GOSUB inbutton
SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]"?f"]
SELECT number
CASE =1
x=x+1
CASE =2
x=x-1
CASE =3
y=0
ENDSELECT
SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]DEC x]
PAUSE 100
IF y<>0 THEN setting
RETURN
set:
Temp = $10
RTCCmd = CtrlReg
GOSUB WriteRTC
Temp = 98
RTCCmd = YrReg
GOSUB WriteRTC
Temp = $08
RTCCmd = MonReg
GOSUB WriteRTC
Temp = $27
RTCCmd = DateReg
GOSUB WriteRTC
GOSUB setting
Temp = x
RTCCmd = HrsReg
GOSUB WriteRTC
x=0
GOSUB setting
Temp = x
RTCCmd = MinReg
GOSUB WriteRTC
Temp = $00
RTCCmd = SecReg
GOSUB WriteRTC
Temp = $80
RTCCmd = CtrlReg
GOSUB WriteRTC
readtime:
GOSUB ReadRTCBurst
SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]DEC Hours,":",DEC Minutes,":",DEC Seconds.HIGHNIB,DEC Seconds.LOWNIB," ",DEC Month.HIGHNIB,DEC Month.LOWNIB,"/",DEC Date.HIGHNIB, DEC Date.LOWNIB,"/",DEC Year.HIGHNIB, DEC Year.LOWNIB]
PAUSE 80
SEROUT 0, BAUDRATE, 1, [noparse][[/noparse]"?f"]
GOTO readtime
WriteRTCRAM:
'Write to DS1202 RTC
HIGH RTCReset
SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%11\2,Temp]
LOW RTCReset
RETURN
WriteRTC:
'Write to DS1202 RTC
HIGH RTCReset
SHIFTOUT Dta, Clk, LSBFIRST, [noparse][[/noparse]%0\1,RTCCmd\5,%10\2,Temp]
LOW RTCReset
RETURN
ReadRTCBurst:
HIGH RTCReset
SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,BrstReg\5,%10\2]
SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Seconds,Minutes,Hours,Date,Month,Year,Year]
LOW RTCReset
RETURN
ReadRTCRAM:
HIGH RTCReset
SHIFTOUT DTA, Clk, LSBFIRST, [noparse][[/noparse]%1\1,RTCCmd\5,%11\2]
SHIFTIN DTA, Clk, LSBPRE, [noparse][[/noparse]Temp]
LOW RTCReset
RETURN
An interesting note, all posters in this thread have the name Jon (I'm Jon Gowa).
·· In case you need it, I also recently posted some very easy to use code for accessing the DS1302, including the on-board RAM.· The code handles 12 and 24 hour modes very easily as well.· You can find it here:
http://forums.parallax.com/showthread.php?p=531080
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com