Question:
Is it possible to convert a 4 digit BCD (binary-coded decimal) register into an integer value?
Solution:
A binary coded decimal is where each digit is represented by a fixed number of bits (typically 4). BCD takes advantage of the fact that any one decimal number can be represented by a 4-bit pattern. The most common way of encoding a decimal digit is by its corresponding four-bit binary value.
Most BCD values will typically encode 2 digits within a byte (8 bits). This is sometimes called a packed BCD.
Now a 16-bit SCADAPack register in TelePACE can store an unsigned integer between 0-65535. The same 16-bit register can store a BCD value between 0-9999.
The following TelePACE code takes a single register with a BCD value and converts it to an unsigned integer in the SCADAPack. It uses modular math to extract the value for each digit. It then multiplies and adds the pieces back together as an unsigned integer.
A hexadecimal BCD value is input into register 41000 in the decimal range of 0-9999. Let's call it WXYZ. The SCADAPack will interpret the BCD value as an incorrect unsigned integer.
- Register 41001 MODU 4096 gives the remainder XYZ and puts it in register 43005
- Register 41001 DIVU 4096 gives the number of thousands W
- Register 43005 MODU 256 gives the remainder YZ and stores it in register 43003
- Register 43005 DIVU 256 gives the number of hundreds X
- Register 43003 MODU 16 gives the remainder Z and stores it in register 43001
- Register 43003 DIVU 16 gives the number of tens Y
Once the values are determined for W, X, Y and Z, they can be combined together to form an unsigned integer.
- This logic calculates (W*1000) + (X*100) + (Y*10) + Z and stores the result in register 42001
Released for: Schneider Electric Philippines

