Convert a Decimal value to Hex.
FformulaA
Posts: 9
Hi,
This is a clock project that displays the time on an lcd. There is a keypad for interaction. Basically a user can enter the current time from the keypad and set the clock. My problem is that the user will enter 59 as the minute but I need to convert that number to hex before I send it to the DS1302. So a 59 will become a 89. Without making a select/case for each number 0 thru 59 to convert, is there an easy way to convert minutes2 to hex and set it to minute? This is what i have so far:
Sub Get_Key
Thanks,
fformulaa
This is a clock project that displays the time on an lcd. There is a keypad for interaction. Basically a user can enter the current time from the keypad and set the clock. My problem is that the user will enter 59 as the minute but I need to convert that number to hex before I send it to the DS1302. So a 59 will become a 89. Without making a select/case for each number 0 thru 59 to convert, is there an easy way to convert minutes2 to hex and set it to minute? This is what i have so far:
minutes = 0 loopdone = 0 SEROUT LCD, Baud, [noparse][[/noparse]LcdCls] ' Clear The LCD Display PAUSE 5 SEROUT LCD, Baud, [noparse][[/noparse]"Enter minutes. 0 - 59 "]', LcdCR] ' loop through until the # key is pressed signaling that iput is done. Show only ' the last 2 keys pressed. so if 1 is entered 1 will show. 2 is pressed. 12 will show. ' 3 is pressed 23 will show, etc. DO IF keypress = 1 THEN 'key is pressed IF minutes > 1999 THEN minutes = minutes //100 'drop first number for the next in line GOSUB get_key PAUSE 50 minutes2 = minutes // 100 'squish down to 2 digits SEROUT LCD, Baud, [noparse][[/noparse]LCDline3] PAUSE 50 SEROUT LCD, Baud, [noparse][[/noparse]"Minutes:", DEC minutes2]', LcdCR] 'shows the new numbers IF LoopDone = 1 THEN EXIT 'if # key was pressed exit loop ENDIF LOOP minutes = minutes2
Sub Get_Key
Get_Key: SERIN TM,396,[noparse][[/noparse]KeyIn] 'DEBUG CRSRXY, 0, 0, HEX2 keyin, CR SELECT KeyIn CASE 0 'one was pressed password = (password * 10) + 1 CASE 1 '2 was pressed password = (password * 10) + 2 CASE 2 '3 was pressed password = (password * 10) + 3 CASE 3 'A was pressed to clear the hour password = 1 CASE 4 '4 was pressed password = (password * 10) + 4 CASE 5 '5 was pressed password = (password * 10) + 5 CASE 6 '6 was pressed password = (password * 10) + 6 CASE 8 '7 was pressed password = (password * 10) + 7 CASE 9 '8 was pressed password = (password * 10) + 8 CASE 10 '9 was pressed password = (password * 10) + 9 CASE 13 '0 was pressed password = (password * 10) + 0 CASE 14 '# was pressed. Finish and goto next step loopdone = 1 ENDSELECT RETURN
Thanks,
fformulaa
Comments
http://forums.parallax.com/showthread.php?p=691369
You need to convert the keypad values, not into HEX, but into BCD. You'd use something like...
DSMinutes = ((minutes2 / 10)*16) + (minutes2 // 10)