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.

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.

With this, I complete my tutorial on BCD Number System.
Post your doubts in comments.

 

MicroController vs MicroProcessor

By microprocessor here I mean general purpose microprocessor like intels 8080,8085, 8086 80286, etc.
These microprocessors contains no RAM, no ROM and no I/O ports.


A system designer needs to add these items to microprocessor during its application. This makes the system bulkier and much more expensive. But provides the versatility such that designer can decide on amount of RAM, ROM and Ports to be added.


On the other hand microcintroller comes with a microprocessor plus a fixed amount of RAM, ROM and ports.  that cannot be changed at any point of time except during its manufacturing. These are ideal where cost and space are a critical thing.


Application like a simple TV remote do not need a computing power of a microprocessor. It just needs to read few signals and change few bits. COst is the major consideration here. Thats why micro controller is preffered over a microprocessor.

Assembly language program for 2 digit bcd to binary conversion

; GIVEN BCD NUMBER SAY 24
; IT IS STORED AS IT IS IN MEMORY
; EVEN THOUGH IT IS STORED AS HEXADECIMAL 24
; IT IS NOT HEXADECIMAL 24 IT IS BCD 24
; NOW FOR BINARY CONVERSION
; SEPARATE 24 TO 02 AND 04
; MULTIPLY 04 BY 0A 04 TIMES THEN ADD 02 TO GET BINARY NUMBER
; THIS IS IN SHORT A DECIMAL TO HEXADECIMAL CONVERSION




; MVI B, 24H ;
; MOV A, B ;
; ANI 0FH ;
; MOV C, A ;
; MOV A, B ;
; ANI F0H ;
; RRC ;
; RRC ;
; RRC ;
; RRC ;
; MOV D, A ;
; XRA A ;
; MVI E, 0AH ;


LOOP:


; ADD E ;
; DCR D ; MULTIPLY WITH 0A REGISTER D TIMES
; JNZ LOOP ;
; ADD C ;
; STA XXXXH ;
; HLT ; END OF PROGRAM

Absolute decoding VS Partial Decoding

ABSOLUTE DECODING


The decoding in which all available address line (16 lines in memory mapped and 8 lines in
peripheral mapping) are used for decoding to generate a unquie address is called absolute
decoding see fig.


ABSOLUTE DECODING





PARTIAL DECODING


The decoding in which all available address line(16 lines in memory mapping and 8 lines in
peripheral mapping) are not used for decoding resulting in multiple address for same port is
called partial decoding. see fig.


PARTIAL DECODING


IMPORTANT POINTS REGARDING INTERFACING

SOME IMPORTANT POINTS TO BE REMEMBERED WHILE DOING MAPPING
–> input and ouput port can have same address they can be differentiated by RD(bar) and
WR(bar) control signals.
–> In peripheral mapping it doesnot matter whether you use higher order address line or 
lower order address line. port address is duplicated on both segments of address 
lines.
–> To recongnize type of mapping just examine the control signal if its a MEMW/MEMR signal
then its a memory map if IOW/IOR signal its peripheral map.

Peripheral Mapped Input/Output



PERIPHERAL MAPPED I/O


–> has 8 bit device address
–> uses IOR(bar)/IOW(bar) as control signals for inout and output.
–> only IN and OUT instruction are available.
–> Data transfer occurs only between I/o and accumulator.
–> the I/O map is independent of memory map and 256 input and 256 ouput devices can be connected.
–> general execution speed of 10 T states.
–> less hardware is needed to decode 8 bit address line.
–> no arithmetic and logical operation are available.


PERIPHERAL MAPPED INPUT/OUTPUT