How to Use Binary Numbers to Convert BCD Codes

Write subroutines Bin2BCD, BCD2Bin to achieve the conversion of binary to compressed BCD code. The binary number to be converted is stored in the w register. The BCD code obtained after the subroutine call is completed is still stored in the w register. E.g:

Movlw .45 ; w=45

Call Bin2BCD ;

Nop ; w=0x45

First, the conversion from binary to compressed BCD code

The method of converting binary code to BCD code is to shift the binary code to the left 8 times. After each shift, check whether the lower four bits are greater than 4. If the number is greater than 4 then the lower four bits are added to 3; otherwise, they are not added; the higher 4 bits are the same. deal with.

Block diagram:

How to Use Binary Numbers to Convert BCD Codes

How to Use Binary Numbers to Convert BCD Codes

How to Use Binary Numbers to Convert BCD Codes

Second, to achieve the conversion of compressed BCD code to binary number

Since the compressed BCD code is a decimal number, so long as it takes the upper four bits and the lower four bits, the upper four bits are multiplied by 10D plus the lower four bits. For programming convenience, multiply 10D into cumulative 10 times.

Block diagram:

How to Use Binary Numbers to Convert BCD Codes

How to Use Binary Numbers to Convert BCD Codes

How to Use Binary Numbers to Convert BCD Codes

Sensor

The sensor includes linear encoder and rotary encoder, which is used for the position measurement of speed, displacement and angle. Yuheng optics can provide rotary encoders based on optical, magnetic and gear principles, linear encoders based on optical principles and supporting products.

ZWD-24M-1

Custom Sensor,Clintegrity Encoder,Absolute Angle Encoder,Small Rotary Encoders

Yuheng Optics Co., Ltd.(Changchun) , https://www.yhencoder.com