Search This Blog

Thursday, August 4, 2011

Binary Coded Decimal (BCD) Number System


BCD stands for Binary Coded Decimal.
Use Of BCD:
BCD means digits starting from 0 to 9.
In our day to day life we are not using hex values, so sometimes it is quite difficult to work with hex. Hence to reduce the complexity and for the ease of users, 8051 is provided with BCD.
Now while working with Embedded Systems, we encounters two terms in BCD number system:
  1. Unpacked BCD:
    Those numbers (in hex) whose upper nibble is zero, are considered as unpacked BCD numbers.
    This means  that in any number if the upper 4 bits are zero, then it is said to be in unpacked format.
    In unpacked BCD format, a single byte has only one BCD number in it.
    Ex:      ”0000 1001″ and “0000 0110″ are unpacked BCD for  9 and 6 respectively.
  2. Packed BCD:
    Those numbers whose upper nibble is not equal to zero, but posses some value is termed as Packed BCD.
    In packed BCD, a single byte has two BCD numbers in it, one in the lower 4 bits and one in upper 4 bits.
    Ex:      ”0011 1001″ and “0001 0001″ are packed BCD for 39 and 11 respectively.
Ok, now we understand what is a BCD number system.
  • Now the Q. is how to convert a hex number into decimal number..??
We can do this by two methods:
  1. In order to convert a hex value in BCD value, we have to add 6 under the number which is higher than 9.
    Consider the following examples.
    • Convert 2Ah in BCD  :
    2Ah
    +06h
    =30h
    ~ So, 30h is the BCD value of 2Ah .
    • Convert A2h in BCD  :
      A2h
      + 60h
      = 102h
      ~So, 102h is the BCD value of A2h.
    • Convert BCh in BCD  :
      BCh
      + 66h
      = 122h
      ~So, 122h is the BCD value of BCh.
  2. Another method is, by the use of DA Instruction:
    The DA (Decimal Adjust for Addition) instruction is provided in the 8051 to perform the above procedure itself.
    By the use of DA instruction, the controller will automatically add 6 under the value greater than 9 and will show us the BCD value.
    As simple as that.. :)
    Use of DA Instruction:
    MOV B,#25h        ; Move 21 hex in Register B
    MOV A,#49h       ; Move 8A hex in Accumulator
    ADD A,B                ; Add Register B with A
    DA A                       ; Convert hex value into BCD value
    After the execution of program we will get 74h in accumulator.
    NOTE:
    The DA Instruction works only with Accumulator, you can not use it with any other 8bit or 16bit register.
    The DA instruction is only used after ADD instruction; it will not work after INC operation.

No comments:

Post a Comment