Reply by Andres June 27, 20072007-06-27
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 Elrica 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...
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 Elrica wrote: Ok! But if my number is a 16 bits value? Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??

---------------------------------
To: p...
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)
01(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 quers saber, y lo que ni imaginabas,
esten Yahoo! Respuestas (Beta).
Probalo ya!
Reply by Hany Esmael June 27, 20072007-06-27
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.
Reply by np np June 27, 20072007-06-27
Some hardware still works in BCD.
Some clock calender chips do.

----- Original Message ----
From: Eirik Karlsen
To: p...
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://mail.yahoo.net/uk" target="_blank" rel="nofollow">http://uk.rd.yahoo.com/evtD106/*http://mail.yahoo.net/uk
Reply by np np June 27, 20072007-06-27
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...
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 Elrica 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)

> 01(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.docs.yahoo.com/mail/winter07.html" target="_blank" rel="nofollow">http://uk.rd.yahoo.com/evtD106/*http://uk.docs.yahoo.com/mail/winter07.html
Reply by ericserdahl June 27, 20072007-06-27
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..., "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..., "rtstofer" wrote:
> >
> > --- In p..., "pseakins" wrote:
> > >
> > > --- In p..., Fagundes Elrica
> > > 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
>
Reply by Andres June 27, 20072007-06-27
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 Elrica escribi
ok, very thanks!!!!!

[]s

Marcelo

---------------------------------
To: p...
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 Elrica 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...
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 Elrica wrote: Ok! But if my number is a 16 bits value? Example= 1320 (bcd) = 528 (hex) --- How i do the conversion??

---------------------------------
To: p...
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)
01(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 quers saber, y lo que ni imaginabas,
esten Yahoo! Respuestas (Beta).
Probalo ya!
Reply by Eirik Karlsen June 11, 20072007-06-11
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
Reply by Eirik Karlsen June 11, 20072007-06-11
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
Reply by Lez June 11, 20072007-06-11
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!
Reply by rtstofer June 11, 20072007-06-11
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