binary to bcd
nick bernard
Posts: 329
hia
i'm trying to write a little snippet to turn a 9 bit variable into 12 bit bcd variable split accross 3 bytes ( i'm displaying on a 3 digit 7 seg via a max 7219)
any help?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
i'm trying to write a little snippet to turn a 9 bit variable into 12 bit bcd variable split accross 3 bytes ( i'm displaying on a 3 digit 7 seg via a max 7219)
any help?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
Comments
I pulled a callable section out of one of my projects but it is for 5 packed BCD digits in 3 bytes from a 16 bit source.
Although the software does run in it's entirity, I have not test this snippet by itself. For only 9 bits, I believe all you need to do is change the first line from #16 to #9, and then also deal with unpacking the bcd bytes. The packed extra result byte can then also be discarded.
Hope this works for you.
Cheers,
Peter (pjv)
OOPS!!!!·Lost all the formatting, so here goes again!
;========================================================
bcdconvert·········mov ·convertcount,#16··········;do 16 bits
························clr· · convertbcdlo·······························;lo result packed digits
························clr· ··convertbcdmid···························· ;mid result packed digits
······················· clr· ··convertbcdhi····························· ·;hi result packed digits
bcdshiftboth······ mov ·fsr,#convertbinlo······················ ·;point to source lo byte
······················· clc
······················· rl····· ind··············································;shift lo source byte
······················· inc ·· fsr
······················· rl··· · ind············································· ;shift out of hi source byte
······················· inc·· ·fsr
······················· rl·· ···ind····························· ;shift into lo dest byte
······················· inc· · fsr
······················· rl···· ·ind····························· ;shift into next dest byte
(discard)··········· inc ·· fsr
(discard)··········· rl·······ind·····························;shift into hi dest byte
bcdendchek······· decsz convertcount
······················· skip
······················· ret
bcdadj·············· mov·· fsr,#convertbcdlo··········;point to dest lo byte
······················· call····bcddigitadj
······················· inc···· fsr
······················· call··· bcddigitadj
······················· inc··· ·fsr
······················· call·· ·bcddigitadj
······················· jmp····bcdshiftboth
bcddigitadj·········mov····w,#3
······················ add···· w,ind
······················ mov··· convertbcdgtemp,w
······················ snb···· convertbcdgtemp.3
······················ mov··· ind,w
······················ mov··· w,#$30
······················ add·· ·w,ind
······················ mov··· convertbcdgtemp,w
······················ snb···· convertbcdgtemp.7
······················ mov···· ind,w
······················ ret
Post Edited (pjv) : 8/11/2005 6:54:31 PM GMT
has several 8 bit to 12 bit bcd (aka half packed or packed bcd) routines...
why 9 bits? 8 bits is easy, but:
2^8th is 256 which only uses about 1/3 of the capasity of a 3 digit display
2^9th is 512 so that does fit in 3 digits...·gives about half the possible output values.
2^10th is 1024 which is a better fit for 3...
humm.. ok, there is a 10 bit to 4 digit(?) routine (why bother with the 25 values past 999?) at
http://www.sxlist.com/techref/microchip/math/radix/b2bu-10b4d-eag.htm
but it hasn't been converted to parallax / SASM notation yet... that requires a few seconds at:
http://www.sxlist.com/cgi-bin/mpasm2sasm2.exe
which then gives us:
Now, that obviously needs some polish, but it should save you some time. Note that this routine is NOT complete, it only figures out D1 by dividing blo/bhi by 10 and·saving the remander, but D2 is just the same thing over again with the value left in blo/bhi after D1 is extracted. So you can stop at D3 and have your 3 digit version.
Please consider publishing what you come up with both here and at sxlist.com?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!
james, i'm having trouble with your program
mov CMCON, W ;Digital I/O - what is cmcon, it doesnt compile
jmp B2d2 ;repeat - there is no b2d2 label
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
...and I've been staring at it for about an hour and I'll be damned if I can see how that could possibly work. I've read the page it came from and it was supposed to be a possible improvement on another routine, which was for 12 bits to 4 digits, but I've been staring at that for another hour and I don't see how that could work either.
It's either bloody brilliant or its been garbled somehow.
The CMCON line is a hold over from the PIC, you can whack that. But the missing label has me confused, as does the original 12 bit code. It appears to be a variation on Paysons code at:
http://www.sxlist.com/techref/ubicom/lib/math/radix/b2bu-16b5d_sx.htm for a 16bit to 5 digit conversion which DOES work and IS bloody brilliant (read: impossible to understand).
If I had the time, I would convert that 12 bit program to SASM and just run it.... to see if it worked at all... Then I would try to understand HOW it worked and THEN... I would try to understand how the idea (which I mistakenly posted above) is supposed to improve on it.
But I have to get back to work....
...the proof is left as an excercise for the student.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
James Newton, Host of SXList.com
james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
SX FAQ / Code / Tutorials / Documentation:
http://www.sxlist.com Pick faster!