A discussion group for the PICMicro microcontroller. Also called the Microchip PIC, this list is dedicated to the use and abuse of this fine, simple, microcontroller. Close to topic posts are welcome, ie. general electronics.
bcd 16 bits to bin - falcosms - Jun 2 22:23:10 2007
Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in
C (ccs PIC)
[]´s
Marcelo

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )
Re: bcd 16 bits to bin - Eirik Karlsen - Jun 3 5:52:38 2007
This is not C but...
E.g
9A(bcd)
=1001(9) 1010(A) (bin)
(16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is
16...
So for a 2-digit BCD number you simply multiply the MSD by 16, and then
add the LSD and you have the binary.
OK?
falcosms wrote:
> Hi
>
> I need a code example for conversion of a 16 bits BCD data in BIN , in
>
> C (ccs PIC)
>
> []´s
>
> Marcelo
>
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )RE: bcd 16 bits to bin - =?iso-8859-1?Q?Fagundes_El=E9trica?= - Jun 3 9:32:53 2007
Ok!
But if my number is a 16 bits value?
Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p...@yahoogroups.comFrom: e...@online.noDate: Sun, 3 Jun 2007 11:52:03 +0200Subject:
Re: [piclist] bcd 16 bits to bin
This is not C but... E.g 9A(bcd) =1001(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a
2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have
the binary.
OK?
falcosms wrote:
Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]´s
Marcelo-- ******************************************* VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
******************************************* Regards Eirik Karlsen
_________________________________________________________________
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN
Mobile!
http://mobile.msn.com

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - Eirik Karlsen - Jun 3 12:19:50 2007
"Just keep in mind the weight of each bcd digit in a binary number is
16... "
So...
(16x16x5) + (16x1x2) + (1x8) = 1320(dec)
And for 3C7E(hex), vhich is 15486(dec):
(16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
12288 + 3072 + 112 + 14 = 15486(dec)
Keep in mind that mul by 16 can be done by:
-Rotate Left 4 times
-Loading a nibble(4bits)
-Swapping nibbles, and merging / masking
For the digit multiplication you'll probably have to use a proper
multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elétrica wrote:
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
>
> -------------------------------------------------------------
> To: p...@yahoogroups.com
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> This is not C but...
> E.g
> 9A(bcd)
> =1001(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd digit in a binary
> number is 16...
> So for a 2-digit BCD number you simply multiply the MSD by
> 16, and then add the LSD and you have the binary.
>
> OK?
> falcosms wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits
> BCD data in BIN , in
> C (ccs PIC)
>
> []´s
>
> Marcelo
>
> --
> *******************************************
> VISIT MY HOME PAGE:
>
> LAST UPDATED: 23/08/2003
> *******************************************
> Regards
> Eirik Karlsen
>
> -----------------------------------------------------------------------
> Hotmail to go? Get your Hotmail, news, sports and much more! Check out
> the New MSN Mobile
>
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )RE: bcd 16 bits to bin - =?iso-8859-1?Q?Fagundes_El=E9trica?= - Jun 3 12:37:42 2007
Ok, but in the other side?
I get the number 1320 in BCD and i want to convert in bin (or hex). How i do that?
To: p...@yahoogroups.comFrom: e...@online.noDate: Sun, 3 Jun 2007 18:07:58 +0200Subject:
Re: [piclist] bcd 16 bits to bin"Just keep in mind the weight of each bcd digit in a
binary number is 16... "
So... (16x16x5) + (16x1x2) + (1x8) = 1320(dec)
And for 3C7E(hex), vhich is 15486(dec): (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) =
15486(dec) 12288 + 3072 + 112 + 14 = 15486(dec)
Keep in mind that mul by 16 can be done by: -Rotate Left 4 times -Loading a nibble(4bits)
-Swapping nibbles, and merging / masking
For the digit multiplication you'll probably have to use a proper multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elétrica wrote:
Ok!
But if my number is a 16 bits value?
Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p...@yahoogroups.com From: e...@online.no Date: Sun, 3 Jun 2007 11:52:03 +0200
Subject: Re: [piclist] bcd 16 bits to bin
This is not C but... E.g 9A(bcd) =1001(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a
2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have
the binary.
OK?
falcosms wrote:
Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]´s
Marcelo-- ******************************************* VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
******************************************* Regards Eirik Karlsen
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN
Mobile
-- ******************************************* VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
******************************************* Regards Eirik Karlsen
_________________________________________________________________
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN
Mobile!
http://mobile.msn.com

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - dlc - Jun 3 16:12:21 2007
Basic math, BCD has the representation in powers of 10, once you convert
to binary then you can make any representation you want, Hex is just
powers of 16 in each place.
DLC
Fagundes Elétrica wrote:
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
> ------------------------------------------------------------------------
> To: p...@yahoogroups.com
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> This is not C but...
> E.g
> 9A(bcd)
> =1001(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd digit in a binary number is
> 16...
> So for a 2-digit BCD number you simply multiply the MSD by 16, and
> then add the LSD and you have the binary.
>
> OK?
>
>
> falcosms wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits BCD data in
> BIN , in
> C (ccs PIC)
>
> []´s
>
> Marcelo
>
> --
> *******************************************
> VISIT MY HOME PAGE:
>
>
>
> LAST UPDATED: 23/08/2003
> *******************************************
> Regards
> Eirik Karlsen
>
> ------------------------------------------------------------------------
> Hotmail to go? Get your Hotmail, news, sports and much more! Check out
> the New MSN Mobile
--
-------------------------------------------------
Dennis Clark TTT Enterprises
www.techtoystoday.com
-------------------------------------------------

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - rtstofer - Jun 3 17:40:17 2007
--- In p...@yahoogroups.com, Fagundes Elétrica
wrote:
> Ok, but in the other side?
>
> I get the number 1320 in BCD and i want to convert in bin (or hex).
How i do that?
>
You have to be able to extract the digits, normalize them and multiply
by the appropriate power of ten. You have 16 bits for a 4 digit BCD
number. Each 4 bit nibble can represent 0..9.
Something like:
BIN = ((BCD & 0xF000) >> 12) * 1000 + ((BCD & 0x0F00) >> 8) * 100 +
((BCD & 0x00F0) >> 4) * 10 + (BCD & 0x000F)
You can see how each nibble is extracted, shifted over to normalize
the value to 0..9 and then multiplied by the appropriate power of ten.
Both BIN and BCD need to be unsigned 16 bit values.
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - Eirik Karlsen - Jun 4 0:19:10 2007
Very simple, just multiply the digits by their weight and sum!
(1000x1) + (100x3) + (10x2) + (1x0) = 1320dec, or 0101 0010 1000(bin),
or 528(hex)
One thing that is important is to be clear on what radix numbers are,
and in what radix those numbers is presented!
In BIN-BCD and BCD-BIN conversions its VERY easy to get confused!
The above is the "pen & paper" method. It is simple to understand but
it is not the fastest.
Many years ago I sat down and devised a much better procedure, took me
several days.
It is a 2-digit BCD2BIN and with a little extra code it can convert any
number of digits
This one is simple (little code) and it is very fast.
I won't go into much detail but the procedure is somewhat 'tweaked' and
obscure so its
not blatantly obvious how or why it works...but it does work, have used
it in many programs.
This is PIC18 asm:
;************** BCD2BIN16 ****************************
;Converts 2digit BCD number in WREG to BIN in WREG
;Uses ASCIIB5:ASCIIB4 as TEMP1:TEMP0
BCD2BIN16 MOVFF WREG,ASCIIB5 ;Get BCDBYTE0 and store in TEMP
SWAPF WREG ;Get MSD digit into low nibble
ANDLW 0X0F ;and discard LSD. This is a DIV by16.
MOVFF WREG,ASCIIB4 ;preseve BCD so operate on TEMP
ADDWF ASCIIB4 ;MUL BY 6
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
BSF STATUS,C ;Subtract result from BCD
MOVFF ASCIIB4,WREG ;and store in BCD
SUBWFB ASCIIB5,W ;WREG is now converted from BCD to BIN
RETURN
;*********************************************************
Since your BCD number has 4 digits you must run the routine on both
bytes, and multiply the
upper byte result by 100d before summing.
Like this
Upper byte:
13(bcd) [convert to BIN] = 0000 1101(bin), which is 0D(hex). Mul by
100(dec) = 10100010100(bin), which is 514(hex)
Lower byte:
20(bcd) [convert to BIN] = 0010 0000(bin), which is 14(hex)
Sum:
514(hex) + 14(hex) = 528(hex)
It figures??
E.K
Fagundes Elétrica wrote:
> Ok, but in the other side?
>
> I get the number 1320 in BCD and i want to convert in bin (or hex).
> How i do that?
>
> -------------------------------------------------------------
> To: p...@yahoogroups.com
> From: e...@online.no
> Date: Sun, 3 Jun 2007 18:07:58 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> "Just keep in mind the weight of each bcd digit in a binary
> number is 16... " So...
> (16x16x5) + (16x1x2) + (1x8) = 1320(dec)
>
> And for 3C7E(hex), vhich is 15486(dec):
> (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
> 12288 + 3072 + 112 + 14 = 15486(dec)
>
> Keep in mind that mul by 16 can be done by:
> -Rotate Left 4 times
> -Loading a nibble(4bits)
> -Swapping nibbles, and merging / masking
>
> For the digit multiplication you'll probably have to use a
> proper multiplication routine,
> minimum a 4by8 , with 12 or 16bit output.
> Fagundes Elétrica wrote:
>
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the
> conversion??
>
> -----------------------------------------
> To: p...@yahoogroups.com
> From: e...@online.no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to
> bin
>
> This is not C but...
> E.g
> 9A(bcd)
> =1001(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd
> digit in a binary number is 16...
> So for a 2-digit BCD number you simply
> multiply the MSD by 16, and then add the
> LSD and you have the binary.
>
> OK?
> falcosms wrote:
>
> Hi
>
> I need a code example for
> conversion of a 16 bits BCD
> data in BIN , in
> C (ccs PIC)
>
> []´s
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )RE: bcd 16 bits to bin - =?iso-8859-1?Q?Fagundes_El=E9trica?= - Jun 4 20:47:35 2007
ok, very thanks!!!!!
[]´s
Marcelo
To: p...@yahoogroups.comFrom: e...@online.noDate: Sun, 3 Jun 2007 23:39:26 +0200Subject:
Re: [piclist] bcd 16 bits to bin
Very simple, just multiply the digits by their weight and sum! (1000x1) + (100x3) +
(10x2) + (1x0) = 1320dec, or 0101 0010 1000(bin), or 528(hex)
One thing that is important is to be clear on what radix numbers are, and in what radix
those numbers is presented! In BIN-BCD and BCD-BIN conversions its VERY easy to get
confused!
The above is the "pen & paper" method. It is simple to understand but it is not the
fastest.
Many years ago I sat down and devised a much better procedure, took me several days. It
is a 2-digit BCD2BIN and with a little extra code it can convert any number of digits This
one is simple (little code) and it is very fast. I won't go into much detail but the
procedure is somewhat 'tweaked' and obscure so its not blatantly obvious how or why it
works...but it does work, have used it in many programs. This is PIC18 asm:
;************** BCD2BIN16 **************************** ;Converts 2digit BCD number in WREG
to BIN in WREG ;Uses ASCIIB5:ASCIIB4 as TEMP1:TEMP0 BCD2BIN16 MOVFF WREG,ASCIIB5 ;Get
BCDBYTE0 and store in TEMP SWAPF WREG ;Get MSD digit into low nibble
ANDLW 0X0F ;and discard LSD. This is a DIV by16. MOVFF
WREG,ASCIIB4 ;preseve BCD so operate on TEMP ADDWF ASCIIB4 ;MUL BY 6
ADDWF ASCIIB4 ; ADDWF ASCIIB4 ; ADDWF ASCIIB4
; ADDWF ASCIIB4 ; BSF STATUS,C ;Subtract
result from BCD MOVFF ASCIIB4,WREG ;and store in BCD SUBWFB
ASCIIB5,W ;WREG is now converted from BCD to BIN RETURN
;*********************************************************
Since your BCD number has 4 digits you must run the routine on both bytes, and multiply
the upper byte result by 100d before summing. Like this
Upper byte: 13(bcd) [convert to BIN] = 0000 1101(bin), which is 0D(hex). Mul by 100(dec)
= 10100010100(bin), which is 514(hex)
Lower byte: 20(bcd) [convert to BIN] = 0010 0000(bin), which is 14(hex)
Sum: 514(hex) + 14(hex) = 528(hex)
It figures??
E.K
Fagundes Elétrica wrote:
Ok, but in the other side?
I get the number 1320 in BCD and i want to convert in bin (or hex). How i do that?
To: p...@yahoogroups.com From: e...@online.no Date: Sun, 3 Jun 2007 18:07:58 +0200
Subject: Re: [piclist] bcd 16 bits to bin
"Just keep in mind the weight of each bcd digit in a binary number is 16... " So...
(16x16x5) + (16x1x2) + (1x8) = 1320(dec)
And for 3C7E(hex), vhich is 15486(dec): (16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) =
15486(dec) 12288 + 3072 + 112 + 14 = 15486(dec)
Keep in mind that mul by 16 can be done by: -Rotate Left 4 times -Loading a nibble(4bits)
-Swapping nibbles, and merging / masking
For the digit multiplication you'll probably have to use a proper multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elétrica wrote:
Ok!
But if my number is a 16 bits value?
Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
To: p...@yahoogroups.com From: e...@online.no Date: Sun, 3 Jun 2007 11:52:03 +0200
Subject: Re: [piclist] bcd 16 bits to bin
This is not C but... E.g 9A(bcd) =1001(9) 1010(A) (bin) (16x9) + (1xA) = 154(dec)
Just keep in mind the weight of each bcd digit in a binary number is 16... So for a
2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and you have
the binary.
OK?
falcosms wrote:
Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in C (ccs PIC)
[]´s -- ******************************************* VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
******************************************* Regards Eirik Karlsen
_________________________________________________________________
With Windows Live Hotmail, you can personalize your inbox with your favorite color.
www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_addcolor_0607

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - pseakins - Jun 5 10:05:16 2007
--- In p...@yahoogroups.com, Fagundes Elétrica
wrote:
> But if my number is a 16 bits value?
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
Thing is. A BCD number is already represented in Hex. So 1320 (bcd) -
is 0x1320. In decimal that would be 4896.
http://en.wikipedia.org/wiki/Binary-coded_decimal

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - rtstofer - Jun 5 20:25:59 2007
--- In p...@yahoogroups.com, "pseakins"
wrote:
>
> --- In p...@yahoogroups.com, Fagundes Elétrica
> wrote:
> > But if my number is a 16 bits value?
> > Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
>
> Thing is. A BCD number is already represented in Hex. So 1320 (bcd) -
> is 0x1320. In decimal that would be 4896.
>
> http://en.wikipedia.org/wiki/Binary-coded_decimal
>
I don't think so...
A BCD number and a hex number would be quite different. The BCD
number 1320 ordinarily represents the value 1320 decimal by convention
- it is just represented in nibble form. IIRC, the 8080 would
actually do arithmetic on BCD numbers with the DAA (Decimal Adjust
Accumulator) instruction (there seems to be some debate as to whether
it was implemented correctly). In fact, the IBM 1620 did all of its'
calculations using BCD numbers.
The hex value 0x1320 would represent 4896 decimal as indicated.
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - pseakins - Jun 6 6:17:52 2007
Richard,
You are way off the mark. Please do a bit of googling. Did you look
at the wiki reference I included?
The number 1320 when represented in BCD is 0x1320. That's what BCD
is. Binary coded decimal. A decimal value 0 to 9 in each of the four
nybbles.
--- In p...@yahoogroups.com, "rtstofer"
wrote:
>
> --- In p...@yahoogroups.com, "pseakins" wrote:
> >
> > --- In p...@yahoogroups.com, Fagundes Elétrica
> > wrote:
> > > But if my number is a 16 bits value?
> > > Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
> >
> > Thing is. A BCD number is already represented in Hex. So 1320
(bcd) -
> > is 0x1320. In decimal that would be 4896.
> >
> > http://en.wikipedia.org/wiki/Binary-coded_decimal
> > I don't think so...
>
> A BCD number and a hex number would be quite different. The BCD
> number 1320 ordinarily represents the value 1320 decimal by
convention
> - it is just represented in nibble form. IIRC, the 8080 would
> actually do arithmetic on BCD numbers with the DAA (Decimal Adjust
> Accumulator) instruction (there seems to be some debate as to
whether
> it was implemented correctly). In fact, the IBM 1620 did all of
its'
> calculations using BCD numbers.
>
> The hex value 0x1320 would represent 4896 decimal as indicated.
>
> Richard
>

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - rtstofer - Jun 6 11:49:39 2007
--- In p...@yahoogroups.com, "pseakins"
wrote:
>
> Richard,
>
> You are way off the mark. Please do a bit of googling. Did you look
> at the wiki reference I included?
>
> The number 1320 when represented in BCD is 0x1320. That's what BCD
> is. Binary coded decimal. A decimal value 0 to 9 in each of the four
> nybbles.
I agree that each decimal digit is encoded in a 4 bit nibble - no
question about that. However, there is no way in the world that 1320
(BCD) is equivalent (or equal) to 0x1320 because that is a radix 16 value.
The point is that the '0x' part specifies a hexadecimal constant (by
convention) and while the nibbles are the same, the value implied is not.
It isn't the electrical state of the bits that is the issue, it is
what they 'mean'. Each BCD nibble is a power of ten, not a power of 16.
There would be no conversion necessary to change 0x1320 to binary.
But there certainly is a little work involved in converting 1320 (BCD).
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: Re: bcd 16 bits to bin - dlc...@frii.com - Jun 6 12:16:37 2007
> --- In p...@yahoogroups.com, "pseakins"
wrote:
>>
>> Richard,
>>
>> You are way off the mark. Please do a bit of googling. Did you look
>> at the wiki reference I included?
>>
>> The number 1320 when represented in BCD is 0x1320. That's what BCD
>> is. Binary coded decimal. A decimal value 0 to 9 in each of the four
>> nybbles.
>
> I agree that each decimal digit is encoded in a 4 bit nibble - no
> question about that. However, there is no way in the world that 1320
> (BCD) is equivalent (or equal) to 0x1320 because that is a radix 16 value.
>
> The point is that the '0x' part specifies a hexadecimal constant (by
> convention) and while the nibbles are the same, the value implied is not.
>
> It isn't the electrical state of the bits that is the issue, it is
> what they 'mean'. Each BCD nibble is a power of ten, not a power of 16.
>
> There would be no conversion necessary to change 0x1320 to binary.
> But there certainly is a little work involved in converting 1320 (BCD).
>
> Richard
I agree with Richard here. You can encode hex into the BCD format and
that would be fine, however, BCD is specifically intended for decimal
(base 10) numbers for those processors that do decimal math (do any do
that any more anyway?) BCD is not hex, that would be BCH, if such a
thing exists.
DLC
--
Dennis Clark

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - rtstofer - Jun 6 15:00:38 2007
--- In p...@yahoogroups.com, dlc@... wrote:
>
> > --- In p...@yahoogroups.com, "pseakins"
wrote:
> >>
> >> Richard,
> >>
> >> You are way off the mark. Please do a bit of googling. Did you look
> >> at the wiki reference I included?
> >>
> >> The number 1320 when represented in BCD is 0x1320. That's what BCD
> >> is. Binary coded decimal. A decimal value 0 to 9 in each of the four
> >> nybbles.
> >
> > I agree that each decimal digit is encoded in a 4 bit nibble - no
> > question about that. However, there is no way in the world that 1320
> > (BCD) is equivalent (or equal) to 0x1320 because that is a radix
16 value.
> >
> > The point is that the '0x' part specifies a hexadecimal constant (by
> > convention) and while the nibbles are the same, the value implied
is not.
> >
> > It isn't the electrical state of the bits that is the issue, it is
> > what they 'mean'. Each BCD nibble is a power of ten, not a power
of 16.
> >
> > There would be no conversion necessary to change 0x1320 to binary.
> > But there certainly is a little work involved in converting 1320
(BCD).
> >
> > Richard
>
> I agree with Richard here. You can encode hex into the BCD format and
> that would be fine, however, BCD is specifically intended for decimal
> (base 10) numbers for those processors that do decimal math (do any do
> that any more anyway?) BCD is not hex, that would be BCH, if such a
> thing exists.
>
> DLC
> --
> Dennis Clark
>
Oddly enough, the ATmega128 (and I suppose the entire family) has a
'Half Carry Flag' to support BCD arithmetic and includes the
appropriate branch on condition for the flag.
I don't know of any current computers that natively use BCD. Even the
1620 didn't have a base 10 ALU. It used lookup tables.
In fact, if I had to do a 4 digit BCD to binary conversion, I
certainly wouldn't do 3 16-bit multiplies by constants. I would do a
table lookup in 3 tables for the high order 3 digits.
The problem for the PIC is that 16 bit multiplication is a lengthy
operation. Even shifting 16 bit values is time consuming.
It still requires isolating the nibbles but this is easy with SWAPF.
Then too, the tables will have 2 byte entries but that isn't too
inconvenient.
Heck, it's easy in C. The assembly language details are a little bit
more sticky. I think I would first code it in C and look at the
resulting assembly code. If it wasn't too bloated, I would leave it
alone. Otherwise, I would write this in assembly language because I
can't imagine it taking more than 50 lines, if that. Just a bunch of
FSR, INDF, SWAPF and ADDWF stuff.
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: Re: bcd 16 bits to bin - Eirik Karlsen - Jun 6 18:42:26 2007
Yes, but the 18 series has hardware 8x8 multiply in the ALU.
A 8x8 takes only one cycle.
This can be used for 8x8 or higher and it speeds up the process
10-60 times. Another good reason to use the PIC18 over the
smaller less capable (PIC16) chips.
I have made a couple of ASCII/BCD/BIN routines for handeling very large
numbers,
see headers below. There are a lot of math in these but still pretty
fast I think.
Still this type of routines are usually the slowest part of the program
where I've used them.
;******************************************************************************
ASCIIBCD2BIN24
;8 digit signed ASCII to BIN conversion routine
;ASCII input range is "-8388608" to "16777215"
;Any other data will result in garbage!
;Enter with ASCII BCD in ASCIIB7:ASCIIB0.
;Exits with binary in ASCIIB3:ASCIIB0. ASCIIB7:ASCIIB4 is garbage.
;Uses 6 temporary locations. Program memory: 412bytes. Cylcles approx.
495.
;VAR: ASCIIB7:ASCIIB0. TEMP3:TEMP0. MULCNT. BCDSIGN.
;Destroys: WREG. TEMP3:TEMP0. MULCNT. BCDSIGN.
;******************************************************************************
;*********************************************************************************
BIN2ASCIIBCD
;24bit BIN to signed ASCII BCD conversion routine
;BIN input range is "800000" to "7FFFFF"
;Output will be "-8388608" to " 8388607"
;Enter with BIN in ASCIIB2:ASCIIB0
;Exit with signed ASCII BCD in ASCIIB7:ASCIIB0
;VAR: ASCIIB7:ASCIIB0. BCDA3:BCDA0
;Destroys: WREG. BSR. INDF2. FSR2H:FSR2L. BCDA3:BCDA0
;Program memory: 202bytes. Cylcles approx. 1485.
;NOTE: due to indexed addressing BCDA3 MUST start at address 0X100 !!
;Example numbers:
;-8388608d=800000h, -0000001d=FFFFFFh, 0000000d=000000h,
+0000001d=000001h, +8388607d=7FFFFFh
;*********************************************************************************
rtstofer wrote:
> ..............The problem for the PIC is that 16 bit multiplication is
> a lengthy
> operation. Even shifting 16 bit values is time consuming.
>
> ...........
>
> Richard
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - rj_satterlee - Jun 8 13:54:32 2007
Hi-
Don't have the code with me here, but in general what I've found to
do the BCD to bin conversion is to clear an accumlating
register pair of registers.
For the 4 BCD digits, For each of the thousand's digits, add a
binary 1000(base ten) i.e. 0x03E8 and decrement the thousand digit.
Repeat until the thousand is exhausted. For example if the thousand
BCD digit is 5, add the 0x03e8 5 times. Once in each loop until
there are no more thousands left. Continue the process with the
hundreds digit adding 100(base 10) 0x64 each time. Then with the
10's 0x0A. For the digits, just add.
Worst case it will be 9 + 9 + 9 + 1 double precison adds for the PIC
code. Seems to work pretty quickly for me.
It's probably easier to code it up for your particular case that you
have in hand rather than try to shuffle around digits and stuff trying
to get all the data in the right place using a "canned" routine.
Best of luck!
Cheers,
Rich S.
--- In p...@yahoogroups.com, "falcosms"
wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits BCD data in BIN , in
> C (ccs PIC)
>
>
>
> []´s
>
>
>
> Marcelo
>

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - pseakins - Jun 11 1:12:54 2007
1320 BCD and 1320 hex is the same thing. It is very common for
beginners to get confused with the representation of register values.
The original poster was confused, I am simply trying to set him
straight. He thought that 1320 bcd equals 528 hex - it doesn't.
A BCD value of 1320 is stored in a 16 bit register (or high level
variable) in binary as 0001001100100000. No one can argue with this
fact. If you display the contents of that register in hex the value is
1320. If you display it in decimal it is 4896. The method of displaying
it is entirely up to the programmer and/or his debugging tools.
Depending on the radix of those tools it might be displayed in
different ways. Whether your processor has a few BCD related
instructions is irrelevant.
Now all the little boys who want to argue - please don't. I was doing
this stuff while your PARENTS were still in high school. I am right and
you are wrong.
> I agree that each decimal digit is encoded in a 4 bit nibble - no
> question about that. However, there is no way in the world that 1320
> (BCD) is equivalent (or equal) to 0x1320 because that is a radix 16
value.
>
> The point is that the '0x' part specifies a hexadecimal constant (by
> convention) and while the nibbles are the same, the value implied is
not.
>
> It isn't the electrical state of the bits that is the issue, it is
> what they 'mean'. Each BCD nibble is a power of ten, not a power of
16.
>
> There would be no conversion necessary to change 0x1320 to binary.
> But there certainly is a little work involved in converting 1320
(BCD).

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )
Re: bcd 16 bits to bin - rtstofer - Jun 11 2:41:57 2007
--- In p...@yahoogroups.com, "pseakins"
wrote:
>
> 1320 BCD and 1320 hex is the same thing. It is very common for
> beginners to get confused with the representation of register values.
> The original poster was confused, I am simply trying to set him
> straight. He thought that 1320 bcd equals 528 hex - it doesn't.
It does too... As long as I define 1320 (BCD) as 1*10^3 + 3*10^2 + 2
* 10^1 + 0 * 10^0 it is equal to 1320 decimal and any subsequent
conversion to binary is whatever it is: 0x528 or 10100101000b, whatever
Each nibble is a decimal value and its' position is a power of ten.
Bit positions are NOT a power of 2 in this case.
>
> A BCD value of 1320 is stored in a 16 bit register (or high level
> variable) in binary as 0001001100100000. No one can argue with this
> fact.
True
> If you display the contents of that register in hex the value is
> 1320.
Also true
> If you display it in decimal it is 4896.
Also true
> The method of displaying
> it is entirely up to the programmer and/or his debugging tools.
> Depending on the radix of those tools it might be displayed in
> different ways. Whether your processor has a few BCD related
> instructions is irrelevant.
Yes, it does matter. It gets to the issue of EQUAL. 1320 BCD doesn't
EQUAL 4896 or 0x1320 because it isn't a base 2 (or 16) number. It
EQUALS 1320 decimal or 0x528 after it is properly converted.
>
> Now all the little boys who want to argue - please don't. I was doing
> this stuff while your PARENTS were still in high school. I am right and
> you are wrong.
Doesn't mean all that much really. I've been messing around with this
stuff for nearly 40 years myself.
Take 0x1320 and add 0x700 and you get 0x1A20. Suddenly the hex
representation doesn't look so good.
Take 1320 (BCD) and add 700 (BCD) and you get 2020 (BCD) and it is the
fact that a Decimal Adjust Accumulator (DAA) instruction exists that
allows an otherwise binary arithmetic unit to come up with this result
by noting that the hex 'A' is not a valid decimal digit and correcting
accordingly.
Yes, 1320 (BCD) looks like 0x1320 when the register contents are
viewed but it doesn't EQUAL 0x1320 and you certainly can not do
arithmetic on the basis that it is equal.
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: Re: bcd 16 bits to bin - Eirik Karlsen - Jun 11 6:13:07 2007
Heh..
The before mentioned confusion about BCD conversions is cleary out in
the open now :o)
I fully support Richard's view of these things.
rtstofer wrote:
> --- In p...@yahoogroups.com, "pseakins"
wrote:
> >
> > 1320 BCD and 1320 hex is the same thing. It is very common for
> > beginners to get confused with the representation of register
> values.
> > The original poster was confused, I am simply trying to set him
> > straight. He thought that 1320 bcd equals 528 hex - it doesn't.
>
> It does too... As long as I define 1320 (BCD) as 1*10^3 + 3*10^2 + 2
> * 10^1 + 0 * 10^0 it is equal to 1320 decimal and any subsequent
> conversion to binary is whatever it is: 0x528 or 10100101000b,
> whatever
>
> Each nibble is a decimal value and its' position is a power of ten.
> Bit positions are NOT a power of 2 in this case.
>
> >
> > A BCD value of 1320 is stored in a 16 bit register (or high level
> > variable) in binary as 0001001100100000. No one can argue with this
> > fact.
>
> True
>
> > If you display the contents of that register in hex the value is
> > 1320.
>
> Also true
>
> > If you display it in decimal it is 4896.
>
> Also true
>
> > The method of displaying
> > it is entirely up to the programmer and/or his debugging tools.
> > Depending on the radix of those tools it might be displayed in
> > different ways. Whether your processor has a few BCD related
> > instructions is irrelevant.
>
> Yes, it does matter. It gets to the issue of EQUAL. 1320 BCD doesn't
> EQUAL 4896 or 0x1320 because it isn't a base 2 (or 16) number. It
> EQUALS 1320 decimal or 0x528 after it is properly converted.
>
> >
> > Now all the little boys who want to argue - please don't. I was
> doing
> > this stuff while your PARENTS were still in high school. I am right
> and
> > you are wrong.
>
> Doesn't mean all that much really. I've been messing around with this
> stuff for nearly 40 years myself.
>
> Take 0x1320 and add 0x700 and you get 0x1A20. Suddenly the hex
> representation doesn't look so good.
>
> Take 1320 (BCD) and add 700 (BCD) and you get 2020 (BCD) and it is the
>
> fact that a Decimal Adjust Accumulator (DAA) instruction exists that
> allows an otherwise binary arithmetic unit to come up with this result
>
> by noting that the hex 'A' is not a valid decimal digit and correcting
>
> accordingly.
>
> Yes, 1320 (BCD) looks like 0x1320 when the register contents are
> viewed but it doesn't EQUAL 0x1320 and you certainly can not do
> arithmetic on the basis that it is equal.
>
> Richard
>
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - pseakins - Jun 11 9:05:57 2007
> Take 0x1320 and add 0x700 and you get 0x1A20. Suddenly the hex
> representation doesn't look so good.
>
> Take 1320 (BCD) and add 700 (BCD) and you get 2020 (BCD) and it is the
> fact that a Decimal Adjust Accumulator (DAA) instruction exists that
> allows an otherwise binary arithmetic unit to come up with this result
> by noting that the hex 'A' is not a valid decimal digit and correcting
> accordingly.
>
> Yes, 1320 (BCD) looks like 0x1320 when the register contents are
> viewed but it doesn't EQUAL 0x1320 and you certainly can not do
> arithmetic on the basis that it is equal.
Rubbish.

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )
Re: bcd 16 bits to bin - rtstofer - Jun 11 9:27:26 2007
Consider a process control of some type where the operator inputs a
quantity to be dispensed via a thumbwheel switch.
The thumbwheel switch has 4 digits (or decades) each of which produces
a positive true binary representation of the decimal digits 0..9. We
would get, for each decade, the binary patterns 0000b .. 1001b or we
could think of them as hex 0x0 .. 0x9.
These 4 decades produce 16 bits which are routed to some register as
1320 (BCD) and look a lot like 0x1320. But they are not EQUAL to 0x1320.
Before we can use the value as a downcounter we need to convert to a
more convenient number base so our poor binary ALU doesn't have to do
a lot of BCD arithmetic.
I contend that the proper conversion of 1320 (BCD) to binary gives a
result of 0x0528 or 0000 0101 0010 1000b and not any other value.
Now the controller can just downcount ticks from the dispensing device
and stop at the appropriate amount.
We do not want to dispense 0x1320 units or, in decimal, 4896 units.
Richard

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )
Re: Re: bcd 16 bits to bin - Lez - Jun 11 9:30:32 2007
Its tomato tomatoe all over again..........
If the guy wants to store 1320 as 4 bytes each being a power of ten to the
next, will the world end?
what was the original question?????????
Its the most active this group has been in months and its turning into a
flame war!

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )
Re: Re: bcd 16 bits to bin - Eirik Karlsen - Jun 11 11:03:39 2007
Not a flame war (yet), hopefully it won't come to that either.
I guess we're discussing this just for the hell of it, or out of
an urge to put things right.
Lez wrote:
>
> Its tomato tomatoe all over again..........
> If the guy wants to store 1320 as 4 bytes each being a power of ten to
> the next, will the world end?
>
> what was the original question?????????
>
> Its the most active this group has been in months and its turning into
> a flame war!
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: Re: bcd 16 bits to bin - Eirik Karlsen - Jun 11 11:23:47 2007
No, truth not rubbish.
It may be that you are thinking something other than what is typed...
0x1320 is a hexadecimal number by convention, as indicated by the "x".
To emphasize:
0x1320 is NOT the same value as bcd1320...because the number bases are
different, in this case base 16 and base 10. The fact that the digits
are the same
does not mean the numbers is also the same.
BUT...and this may be cause of all the confusion :
0x1320 COULD represent a bcd number, e.g we could find 0x1320 in a
memory
buffer (or 16bit register) in the PIC chip...and if this was e.g a LCD /
LED display buffer
for numerical readout the 0x1320 should be interpreted as a bcd number,
although
the number itself is hexadecimal.
So it is clearly up to the viewer or programmer to KNOW what he is
looking at
and how to interpret it correctly.
Makes more sense now ?
pseakins wrote:
> > Take 0x1320 and add 0x700 and you get 0x1A20. Suddenly the hex
> > representation doesn't look so good.
> >
> > Take 1320 (BCD) and add 700 (BCD) and you get 2020 (BCD) and it is
> the
> > fact that a Decimal Adjust Accumulator (DAA) instruction exists that
>
> > allows an otherwise binary arithmetic unit to come up with this
> result
> > by noting that the hex 'A' is not a valid decimal digit and
> correcting
> > accordingly.
> >
> > Yes, 1320 (BCD) looks like 0x1320 when the register contents are
> > viewed but it doesn't EQUAL 0x1320 and you certainly can not do
> > arithmetic on the basis that it is equal.
>
> Rubbish.
>
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )RE: bcd 16 bits to bin - Andres - Jun 27 8:24:13 2007
Isend you the roytin.....good luck..
;you have to load the high byte in bin_h,
;and lower byte in bin_l before call routin
;and result is in bcd0 (units), bcd1 (10's)
;and bcd2 (100's)
binbcd clrf bcd0 ;Units
clrf bcd1 ;Decenas
clrf bcd2 ;Centenas
movlw .16
movwf count
loop1 rlf bin_l,1
rlf bin_h,1
rlf bcd0,w
call daj
movwf bcd0
rlf bcd1,w
call daj
movwf bcd1
rlf bcd2,w
call daj
movwf bcd2
decfsz count,1
goto loop1
return
;deecimal adjust
daj addlw (.256-.10)
skpnc
return
addlw .10
clrc
return
Fagundes Elétrica
escribió:
ok, very thanks!!!!!
[]´s
Marcelo
---------------------------------
To: p...@yahoogroups.com
From: e...@online.no
Date: Sun, 3 Jun 2007 23:39:26 +0200
Subject: Re: [piclist] bcd 16 bits to bin
Very simple, just multiply the digits by their weight and sum!
(1000x1) + (100x3) + (10x2) + (1x0) = 1320dec, or 0101 0010 1000(bin), or 528(hex) One
thing that is important is to be clear on what radix numbers are,
and in what radix those numbers is presented!
In BIN-BCD and BCD-BIN conversions its VERY easy to get confused!
The above is the "pen & paper" method. It is simple to understand but
it is not the fastest. Many years ago I sat down and devised a much better procedure,
took me several days.
It is a 2-digit BCD2BIN and with a little extra code it can convert any number of digits
This one is simple (little code) and it is very fast.
I won't go into much detail but the procedure is somewhat 'tweaked' and obscure so its
not blatantly obvious how or why it works...but it does work, have used it in many
programs.
This is PIC18 asm:
;************** BCD2BIN16 ****************************
;Converts 2digit BCD number in WREG to BIN in WREG
;Uses ASCIIB5:ASCIIB4 as TEMP1:TEMP0
BCD2BIN16 MOVFF WREG,ASCIIB5 ;Get BCDBYTE0 and store in TEMP
SWAPF WREG ;Get MSD digit into low nibble
ANDLW 0X0F ;and discard LSD. This is a DIV by16.
MOVFF WREG,ASCIIB4 ;preseve BCD so operate on TEMP
ADDWF ASCIIB4 ;MUL BY 6
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
ADDWF ASCIIB4 ;
BSF STATUS,C ;Subtract result from BCD
MOVFF ASCIIB4,WREG ;and store in BCD
SUBWFB ASCIIB5,W ;WREG is now converted from BCD to BIN
RETURN
;********************************************************* Since your BCD number has 4
digits you must run the routine on both bytes, and multiply the
upper byte result by 100d before summing.
Like this Upper byte:
13(bcd) [convert to BIN] = 0000 1101(bin), which is 0D(hex). Mul by 100(dec) =
10100010100(bin), which is 514(hex) Lower byte:
20(bcd) [convert to BIN] = 0010 0000(bin), which is 14(hex) Sum:
514(hex) + 14(hex) = 528(hex)
It figures?? E.K
Fagundes Elétrica wrote: Ok, but in the other side? I get the number 1320 in
BCD and i want to convert in bin (or hex). How i do that?
---------------------------------
To: p...@yahoogroups.com
From: e...@online.no
Date: Sun, 3 Jun 2007 18:07:58 +0200
Subject: Re: [piclist] bcd 16 bits to bin "Just keep in mind the weight of each bcd
digit in a binary number is 16... " So...
(16x16x5) + (16x1x2) + (1x8) = 1320(dec) And for 3C7E(hex), vhich is 15486(dec):
(16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
12288 + 3072 + 112 + 14 = 15486(dec) Keep in mind that mul by 16
can be done by:
-Rotate Left 4 times
-Loading a nibble(4bits)
-Swapping nibbles, and merging / masking For the digit multiplication you'll probably
have to use a proper multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elétrica wrote: Ok! But if my number is a 16 bits value? Example=
1320 (bcd) = 528 (hex) --- How i do the conversion??
---------------------------------
To: p...@yahoogroups.com
From: e...@online.no
Date: Sun, 3 Jun 2007 11:52:03 +0200
Subject: Re: [piclist] bcd 16 bits to bin
This is not C but...
E.g
9A(bcd)
=1001(9) 1010(A) (bin)
(16x9) + (1xA) = 154(dec) Just keep in mind the weight of each bcd digit in a binary
number is 16...
So for a 2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and
you have the binary. OK?
falcosms wrote: Hi I need a code example for conversion of a 16 bits BCD data
in BIN , in
C (ccs PIC) []´s
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
---------------------------------
Change is good. See what's different about Windows Live Hotmail. Check it out!
Andres
a...@yahoo.com.ar
Cordoba-Argentina
---------------------------------
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - ericserdahl - Jun 27 8:24:22 2007
Now I'm confused. Is it a question of "notation", "conversion"
or "interpretation"?
The 16 bit binary value of 000100110010000b or 000100110010000 base 2
can be used as a BCD (binary coded decimal) "number" representing the
value of 1320 decimal or 1320 base 10.
.
The 16 bit binary value of 000100110010000b or 000100110010000 base 2
can also be represented as a hexadecimal value as 1320H or 0x1320.
The 16 bit hexadecimal value of 1320H or 1320 base 16 is also equal
to the decimal value 4896 decimal or 4896 base 10.
Is the above true?
--- In p...@yahoogroups.com, "pseakins" wrote:
>
> Richard,
>
> You are way off the mark. Please do a bit of googling. Did you look
> at the wiki reference I included?
>
> The number 1320 when represented in BCD is 0x1320. That's what BCD
> is. Binary coded decimal. A decimal value 0 to 9 in each of the
four
> nybbles.
> --- In p...@yahoogroups.com, "rtstofer" wrote:
> >
> > --- In p...@yahoogroups.com, "pseakins" wrote:
> > >
> > > --- In p...@yahoogroups.com, Fagundes Elétrica
> > > wrote:
> > > > But if my number is a 16 bits value?
> > > > Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
> > >
> > > Thing is. A BCD number is already represented in Hex. So 1320
> (bcd) -
> > > is 0x1320. In decimal that would be 4896.
> > >
> > > http://en.wikipedia.org/wiki/Binary-coded_decimal
> > >
> >
> > I don't think so...
> >
> > A BCD number and a hex number would be quite different. The BCD
> > number 1320 ordinarily represents the value 1320 decimal by
> convention
> > - it is just represented in nibble form. IIRC, the 8080 would
> > actually do arithmetic on BCD numbers with the DAA (Decimal Adjust
> > Accumulator) instruction (there seems to be some debate as to
> whether
> > it was implemented correctly). In fact, the IBM 1620 did all of
> its'
> > calculations using BCD numbers.
> >
> > The hex value 0x1320 would represent 4896 decimal as indicated.
> >
> > Richard
>

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - np np - Jun 27 8:24:22 2007
first nibble & 0xf
add second nibble &0x0f *10
add third nibble &0xf00 *100
add fourth nibbe &0xf000 * 1000
www.ckp-railways.talktalk.net/pcbcad21.htm
----- Original Message ----
From: dlc
To: p...@yahoogroups.com
Sent: Sunday, 3 June, 2007 9:16:27 PM
Subject: Re: [piclist] bcd 16 bits to bin
Basic math, BCD has the representation in powers of 10, once you convert
to binary then you can make any representation you want, Hex is just
powers of 16 in each place.
DLC
Fagundes Elétrica wrote:
> Ok!
>
> But if my number is a 16 bits value?
>
> Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??
>
>
> ------------ --------- --------- --------- --------- --------- -
> To: piclist@yahoogroups .com
> From: eikarlse@online. no
> Date: Sun, 3 Jun 2007 11:52:03 +0200
> Subject: Re: [piclist] bcd 16 bits to bin
>
> This is not C but...
> E.g
> 9A(bcd)
> =1001(9) 1010(A) (bin)
> (16x9) + (1xA) = 154(dec)
>
> Just keep in mind the weight of each bcd digit in a binary number is
> 16...
> So for a 2-digit BCD number you simply multiply the MSD by 16, and
> then add the LSD and you have the binary.
>
> OK?
>
>
> falcosms wrote:
>
> Hi
>
> I need a code example for conversion of a 16 bits BCD data in
> BIN , in
> C (ccs PIC)
>
> []´s
>
> Marcelo
>
> --
> ************ ********* ********* ********* ****
> VISIT MY HOME PAGE:
>
> >
> LAST UPDATED: 23/08/2003
> ************ ********* ********* ********* ****
> Regards
> Eirik Karlsen
>
>
>
> ------------ --------- --------- --------- --------- --------- -
> Hotmail to go? Get your Hotmail, news, sports and much more! Check out
> the New MSN Mobile
--
------------ --------- --------- --------- --------- -
Dennis Clark TTT Enterprises
www.techtoystoday. com
------------ --------- --------- --------- --------- -
___________________________________________________________
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today
http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: Re: bcd 16 bits to bin - np np - Jun 27 8:24:34 2007
Some hardware still works in BCD.
Some clock calender chips do.
----- Original Message ----
From: Eirik Karlsen
To: p...@yahoogroups.com
Sent: Monday, 11 June, 2007 3:53:07 PM
Subject: Re: [piclist] Re: bcd 16 bits to bin
Not a flame war (yet), hopefully it won't come to that either.
I guess we're discussing this just for the hell of it, or out of
an urge to put things right.
Lez wrote:
Its tomato tomatoe all over again....... ...
If the guy wants to store 1320 as 4 bytes each being a power of ten
to the next, will the world end?
what was the original question???? ?????
Its the most active this group has been in months and its turning into
a flame war!
--
************ ********* ********* ********* ****
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
************ ********* ********* ********* ****
Regards
Eirik Karlsen
___________________________________________________________
What kind of emailer are you? Find out today - get a free analysis of your email
personality. Take the quiz at the Yahoo! Mail Championship.
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )Re: bcd 16 bits to bin - Hany Esmael - Jun 27 8:25:13 2007
ok I think it is easy to make it
but , tell me the kind of pic you want to program
falcosms
wrote:
Hi
I need a code example for conversion of a 16 bits BCD data in BIN , in
C (ccs PIC)
[]´s
Marcelo
---------------------------------
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com ) RE: bcd 16 bits to bin - Andres - Jun 27 8:25:28 2007
Fagundes:
I don't know in C, but I have a routine in .ASM.
If you need, tell me.
Sorry, my English is bad.....
Andres.
Cordoba - Argentina.
Fagundes Elétrica
escribió:
Ok, but in the other side?
I get the number 1320 in BCD and i want to convert in bin (or hex). How i do that?
---------------------------------
To: p...@yahoogroups.com
From: e...@online.no
Date: Sun, 3 Jun 2007 18:07:58 +0200
Subject: Re: [piclist] bcd 16 bits to bin
"Just keep in mind the weight of each bcd digit in a binary number is 16... " So...
(16x16x5) + (16x1x2) + (1x8) = 1320(dec) And for 3C7E(hex), vhich is 15486(dec):
(16x16x16x3) + (16x16xC) + (16x1x7) + (1xE) = 15486(dec)
12288 + 3072 + 112 + 14 = 15486(dec) Keep in mind that mul by 16
can be done by:
-Rotate Left 4 times
-Loading a nibble(4bits)
-Swapping nibbles, and merging / masking For the digit multiplication you'll probably
have to use a proper multiplication routine,
minimum a 4by8 , with 12 or 16bit output.
Fagundes Elétrica wrote: Ok! But if my number is a 16 bits value? Example=
1320 (bcd) = 528 (hex) --- How i do the conversion??
---------------------------------
To: p...@yahoogroups.com
From: e...@online.no
Date: Sun, 3 Jun 2007 11:52:03 +0200
Subject: Re: [piclist] bcd 16 bits to bin
This is not C but...
E.g
9A(bcd)
=1001(9) 1010(A) (bin)
(16x9) + (1xA) = 154(dec) Just keep in mind the weight of each bcd digit in a binary
number is 16...
So for a 2-digit BCD number you simply multiply the MSD by 16, and then add the LSD and
you have the binary. OK?
falcosms wrote: Hi I need a code example for conversion of a 16 bits BCD data
in BIN , in
C (ccs PIC) []´s Marcelo
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
---------------------------------
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN
Mobile
--
*******************************************
VISIT MY HOME PAGE:
LAST UPDATED: 23/08/2003
*******************************************
Regards
Eirik Karlsen
---------------------------------
Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN
Mobile
---------------------------------
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!

(You need to be a member of piclist -- send a blank email to piclist-subscribe@yahoogroups.com )