WOW!
Check out this guy's idea:
http://www.is.wayne.edu/olmt/binary/page3.htm
Robert
![]()
WOW!
Check out this guy's idea:
http://www.is.wayne.edu/olmt/binary/page3.htm
Robert
![]()
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Don't know if this can help but this is what I use to go back and forth between a ds1302, a pic, a Siteplayer and an LCD.
Hope it helps.Code:k = rtcmin : gosub h2d : decmin = k 'or k = rtchr, rtcsec, etc... '((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))) 'Subroutine to convert from Hex to Dec '((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))) h2d: ' Convert Hex coded time data -> decimal data K = (K & $F )+((K>>4)*10) Return k = decmin : gosub d2h : rtcmin = k 'or k=dechr, decsec,etc.. '((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))) 'Subroutine to convert from Dec to Hex '((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))) d2h: ' Convert Decimal -> Hex coded time data K = (K DIG 1) * $10 + (K DIG 0) Return
I found this nice Assembler routine here:
http://www.piclist.com/techref/micro...b-5d16b-ph.htm
And best of all, he explains it all for people like me.He boasts it takes only 33 cycles in all to complete, which I consider fantastic compared to any of the alternatives I can possibly write in BASIC Pro (after it converts into Assembler).
He says it's free, I'm using it as an include and keeping all his credits; he deserves it. I don't like using someone else's free code, I like using my 'own stuff'', but this routine is just awesome. 5 bytes in, 2 bytes out, and no temporary variables, pretty sleek stuff.
And for those like me that don't know how to tie this into our BASIC programs, chapter 8.2 is where it's at.
; 5 digit decimal to 16 (17) bit binary. By Peter Hemsley, March 2003.
; Input decimal digits in D0 (LSD) to D4 (MSD)
; Output 16 bit binary in NUMHI and NUMLO
; No temporary variables required
; Code size: 33 instructions
; Execution time: 33 cycles (excluding Call and Return)
; Returns carry set if > 65535 (and NUMHI-LO MOD 65536)
dec2bin16
movf D1,W ; (D1 + D3) * 2
addwf D3,W
movwf NUMLO
rlf NUMLO,F
swapf D2,W ; + D2 * 16 + D2
addwf D2,W
addwf NUMLO,F
rlf D4,W ; + (D4 * 2 + D3) * 256
addwf D3,W
movwf NUMHI
rlf NUMLO,F ; * 2
rlf NUMHI,F
swapf D3,W ; - D3 * 16
subwf NUMLO,F
skpc
decf NUMHI,F
swapf D2,W ; + D2 * 16 + D1
addwf D1,W
addwf NUMLO,F
skpnc
incf NUMHI,F
swapf D4,W ; + D4 * 16 + D0
addwf D0,W
rlf NUMLO,F ; * 2
rlf NUMHI,F
addwf NUMLO,F
skpnc
incf NUMHI,F
movf D4,W ; - D4 * 256
subwf NUMHI,F
swapf D4,W ; + D4 * 16 * 256 * 2
addwf NUMHI,F
addwf NUMHI,F
return ; Q.E.D.
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Definition:
BCD represents each of the digits of an unsigned decimal
as the 4-bit binary equivalents.
UNPACKED BCD:
Unpacked BCD representation contains only one decimal digit per byte.
The digit is stored in the least significant 4 bits; the most significant
4 bits are not relevant to the value of the represented number.
PACKED BCD:
Packed BCD representation packs two decimal digits into a single byte.
* * *
Example values:
Decimal------Binary---------Unpacked BCD----------Packed BCD
0-----------0000 0000---------------0000 0000-----0000 0000
1-----------0000 0001---------------0000 0001-----0000 0001
2-----------0000 0010---------------0000 0010-----0000 0010
3-----------0000 0011---------------0000 0011-----0000 0011
4-----------0000 0100---------------0000 0100-----0000 0100
5-----------0000 0101---------------0000 0101-----0000 0101
6-----------0000 0110---------------0000 0110-----0000 0110
7-----------0000 0111---------------0000 0111-----0000 0111
8-----------0000 1000---------------0000 1000-----0000 1000
9-----------0000 1001---------------0000 1001-----0000 1001
10----------0000 1010-----0000 0001 0000 0000-----0001 0000
11----------0000 1011-----0000 0001 0000 0001-----0001 0001
12----------0000 1100-----0000 0001 0000 0010-----0001 0010
13----------0000 1101-----0000 0001 0000 0011-----0001 0011
14----------0000 1110-----0000 0001 0000 0100-----0001 0100
15----------0000 1111-----0000 0001 0000 0101-----0001 0101
16----------0001 0000-----0000 0001 0000 0110-----0001 0110
17----------0001 0001-----0000 0001 0000 0111-----0001 0111
18----------0001 0010-----0000 0001 0000 1000-----0001 1000
19----------0001 0011-----0000 0001 0000 1001-----0001 1001
20----------0001 0100-----0000 0010 0000 0000-----0010 0000
==================================================
Decimal to Packed BCD:
==================================================
Example decimal 16 to Packed BCD
decVal= 16 decimal
Formula:
Packed_bcdVal = (decVal / 10) << 4 + (decVal // 10)
(decVal / 10)
00010000 / 00001010 = 00000001
Shifts the result left 4 places
00000001 << 4 = 00010000
(decVal // 10) modulus
00010000 // 10 = 00000110
(00000110 , Dec 6 is the remainder).
00010000 + 00000110 = 00010110
Result: 00010110
(00010110 represent the Packed BCD value for decimal 16)
==================================================
Packed BCD to Decimal:
==================================================
Example Packed BCD 00010110 to decimal
Packed_BCD = 00010110
Formula:
decVal = (Packed_BCD_high_nibble * 10) + Packed_BCD_low_nibble
Packed_BCD_high_nibble = 0001
Packed_BCD_low_nibble = 0110
(Packed_BCD_high_nibble * 10)
0001 * 1010 = 1010
1010 + 0110 = 00010000
Result: 00010000
(00010000 = decimal 16)
==================================================
Best regards,
Luciano
Packed, unpacked, I graduated from college in '83, let's see, that's 5 years ago?
I never used that in 20 years as programmer, forgot that basic stuff. I'm looking at your 2 digit conversion example and trying to figure how you'd go about converting 6 decimal digits to binary in 2 bytes. Now my brain hurts...
Robert
![]()
Last edited by Demon; - 24th February 2005 at 00:16.
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Woah, the more I read about converting, the more confused I get. When I counted binary, it went like this:
09. 0000 1001
10. 0000 1010
11. 0000 1011
12. 0000 1100
13. 0000 1101
14. 0000 1110
15. 0000 1111
My problem is that this data will be used for addressing on external memory chips; 24C256.
Robert
?
My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.
Not as dumb as yesterday, but stupider than tomorrow!
Bookmarks