BCD Conversions

bwhorton

Guest
B
I am at a stumbling block and neither my instructor nor my textbook has put forth a suitable answer.

We have been covering the TOD/FRD instruction sets in AB SLC 5/04 using RSLogics. However, I don't understand exactly what BCD is, or more accurately, how to convert it from Binary or HEX or vice-versa.

I apologize if this has been answered previously, but if someone will point me in the right direction, I'd be much appreciative!

Ben
 
Hex is the way we'd all count if we had eight fingers on each hand. BCD is hex with six fingers amputated.

In BCD, each group of four bits represents one decimal digit. The decimal number 1234, rendered in BCD would be 0001 0010 0011 0100. I left the spaces between each group of four bits to help you see the pattern. You can also think of the least significant group of four bits as corresponding to the units column of a decimal number and the bits representing the (decimal) values of 1, 2, 4, and 8. The next most significant group of four bits corresponds to the '10s' column of a decimal number and the bits represent the (decimal) values 10, 20, 40, 80.

BCD is an inefficient method of coding decimal numbers in binary. That's because when you have 4 bits, there are 16 possible combinations of 1 and 0, but BCD uses only ten of those combinations. Thus, a 16-bit word, using BCD, can only represent the (decimal) values zero through 9999, while the same 16-bit word, interpreted as a binary value can represent the (decimal) values zero through 65,535 (or -32,768 through +32,767).
 
BCD

I'm surprised you can't find this in your textbook, but here's a thumbnail:

BCD stands for Binary Coded Decimal. It's a halfway compromise between binary (powers of 2) and decimal (powers of 10).

Each decimal place (ones, tens, hundreds) is coded in binary (1,2,4,8). Thus 4 bits are used for each decimal place. A 3-digit number (base 10) (e.g., 000-999) uses 12 BCD bits to descibe it (even though 12 binary bits could count up to 4095).

The most common use of BCD these days (outside of the programming environment - some PLCs do integer math in BCD rather than straight binary), are thumbwheels.

Each wheel has 4 wires attached, corresponding to the binary pattern of the BCD/Binary value. This simplifes contruction of these input devices since the values for each wheel is independent of the value of the other wheel. If you went with straight binary, each wheel could affect whether power is on any wire. It would be simpler to program, but harder to manufacture the wheels.

But inside the PLC, all math is done in straight binary (4 + 4 = 0, carry the 1 to the 8's place), and the carrying rules for BCD wouldn't work (4 + 7 = 0100 + 0111 = 1011 (which is 1110, but an invalid number in BCD)

The that's the WHY of the FRD. The TOD is probably used to set an LED display. Again, the circuitry of these things is commonly BCD, again 4 wires for each digit. Again, it's easier for the display manufacturer to have each digit work independantly, than to decode an entire 16-bit (16-wire) feed.
 
Thanks for all of your responses. They were very helpful and made clear what BCD is.

I'd much rather use hex as well. As a computer support tech who is trying to make the jump into the controls world, BCD threw a wrench in my understanding.

Thanks again!

Ben
 
Here is some more BCD explanation

As explained, BCD is represented with groups of 4 bit (nibble). Each group (or nibble) can be a digit between 0 and 9.

The relation between Binary, Hex and BCD:

Code:
Binary  Hex   BCD

0000     0     0
0001     1     1
0010     2     2
0011     3     3
0100     4     4
0101     5     5
0110     6     6 
0111     7     7
1000     8     8
1001     9     9
1010     A
1011     B
1100     C
1101     D
1110     E
1111     F

Example of the difference between BCD and decimal:

1234 decimal = 10011010010 binary = 4D2 hex = invalid BCD

but

1234 BCD = 1234 hex = 0001001000110100 binary = 4660 decimal

(Because: 0001001000110100 binary = 0*2^15 + 0*2^14 + 0*2^13 + 1*2^12 + 0*2^11 + 0*2^10 + 1*2^9 + 0*2^8 + 0*2^7 + 0*2^6 + 1*2^5 + 1*2^4 + 0*2^3 + 1*2^2 + 0*2^1 + 0*2^0 = 4096 + 512 + 32 + 16 + 4 = 4660)


If you is going to roll-your-own BCDtoInteger-funksjon, here is an example of a BCD-to-Decimal routine:

Code:
(* BCDvalue is a word containing a BCD value *)

TempWord :=0;
DecimalWord:=0;
Power := 1000;

For i := 0 to 3 DO
  TempWord:= ShiftLeft(BCDvalue ,4);    (* TempWord = 4 MSB shifted out of BCDvalue *)
  DecimalWord:= DecimalWord + Power*TempWord;
  Power := Power/10;                    (* Power = 1000, 100, 10, 1 *)
End_for;

regards
 
Kalle,

Just to let you know, if you don't already, Phil just made some new Vb-tags for this exponent stuff.

It's a little extra work... but for those of us that are anal about the esthetics (you (WE) know who you (WE) are!), it makes for a nicer display.


0*2^3 + 1*2^2 + 0*2^1 + 0*2^0



0*23 + 1*22 + 0*21 + 0*20 = 4

You can checkout the Source to see how I did it.

 
Last edited:
I guess I am just lazy, Terry.

BTW. Now that I have read your code/source, I guess you have to hunt me down. But then, what is the chance that you are on this side of the pool... :D
 

Similar Topics

hi... i have an issue in s7 300 plc, while we run the machine(in idle there is no fault) , plc cpu goes in SF mode, after restart the power cycle...
Replies
2
Views
116
Hi. I’m doing a PV 1400e to PVP 7 migration and I don’t know how to convert the old 8digit BCD type from PB1400e into something that will work on...
Replies
2
Views
588
I'm working on converting an old PanelBuilder 1200 HMI application into FactoryTalk View ME. The PLC was an old SLC 5/02 with a scanner card for...
Replies
2
Views
1,104
I have a Cmore screen which is communicating to the DL06 in BCD and need to create a timer that works in real numbers for a test, I simply need to...
Replies
3
Views
2,129
Hello, I am stumped on how to tackle this issue. I am working on an Energy project on a solar / wind farm and I am trying to read in some data...
Replies
7
Views
2,473
Back
Top Bottom