PIC microcontroller realizes the conversion between binary code and compressed BCD code

Publisher:dandan666Latest update time:2019-05-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Write subroutines Bin2BCD and BCD2Bin to realize the conversion between binary numbers and compressed BCD codes. The binary number to be converted is stored in the w register, and the BCD code obtained after the subroutine call is completed is still stored in the w register and returned. For example:


movlw .45 ; w=45

call Bin2BCD ;

nop ; w=0x45

1. Convert binary numbers to compressed BCD codes

The method of converting binary numbers into compressed BCD code is to shift the binary code left 8 times. After each shift, check whether the lower four bits are greater than 4. If they are greater than 4, add 3 to the lower four bits, otherwise do not add; the same process is done for the upper 4 bits.



list p=16f877A ; Indicate the processor type used

#include ;Call header file  

__CONFIG _CP_OFF ​​& _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF ​​& _CPD_OFF

 

;*****************Variable definitions*******************************************************

 

BIN EQU 0x20 ; store binary numbers

BCD EQU 0x21 ; store compressed BCD code

BCDLO EQU 0x22 ; store the lower four bits of the BCD code

BCDHI EQU 0x23 ; store the high four bits of the BCD code

LOW3 EQU 0x24 ; used to add the lower four bits

HIGH3 EQU 0x25 ; used to add the upper four bits

COUNT EQU 0x26 ; Number of loops

 

;****************************************************** ******************************

    ORG 0x0000 ; Reset entry address

    nop ; compatible with ICD debugging tools, nop must be added

    goto Main; Jump to Main function

;*****************************Main function code**********************************

Main                                               

    MOVLW b'00101101' ; Send binary number 00101101 to BIN

    MOVWF BIN

    CLRF BCD ; BCD clear    

    MOVLW 0x03 ; 0x03 sends LOW3

    MOVWF LOW3

    MOVLW 0x30; 0x30 sends HIGH3

    MOVWF HIGH3

    MOVLW 0x07 ; Initialize loop count    

    MOVWF COUNT    

    CALL Bin2BCD ; Call the conversion program

    RLF BIN

    RLF BCD ; The eighth left shift should result in 0x45

    nop

    goto $ ; stop

;**************************Binary to compressed BCD code subroutine********************

    ORG 0X0100

Bin2BCD

    RLF BIN

    RLF BCD; Binary code is sent to BCD from high to low

    MOVLW b'00001111' ; Clear the upper four bits of W register

    ANDWF BCD,W ; Clear the high four bits of BCD and store the result in the W register

    MOVWF BCDLO ; Temporarily save here for judgment

    MOVLW 0x04

    BCF STATUS,C ; Flag cleared

    SUBWF BCDLO,F ; Check if the lower four bits are greater than 4

    BTFSC STATUS, C; If the C bit is 0, jump

    CALL ADDLO ; C=1, that is, the lower four bits are greater than 4, then add 3 to the lower four bits

    nop

    MOVLW b'11110000' ; Clear the lower four bits of the W register

    ANDWF BCD,W ; Clear the lower four bits of BCD and store the result in the W register

    MOVWF BCDHI; Temporarily save here for judgment

    MOVLW 0x40

    BCF STATUS,C ; Flag cleared

    SUBWF BCDHI,F ; Check whether the upper four bits are greater than 4

    BTFSC STATUS, C; If the C bit is 0, jump

    CALL ADDHI ; C=1, that is, the upper four bits are greater than 4, then add 3 to the upper four bits

    nop

    DECFSZ COUNT,F ; loop

    GOTO Bin2BCD

    RETURN

;****************************************************** *****************

ADDLO ; add 3 to the lower four bits

    MOVF LOW3,W

    ADDWF BCD,F

    RETURN

ADDHI ; add 3 to the upper four digits

    MOVF HIGH3,W 

    ADDWF BCD,F

    RETURN

;****************************************************** ******************************

END ; End of program

2. Realize the conversion of compressed BCD code to binary number

Because the compressed BCD code is a decimal number, we only need to take the upper four bits and the lower four bits, multiply the upper four bits by 10D and add the lower four bits. For the convenience of programming, we can convert the multiplication by 10D into accumulation 10 times.


list p=16f877A ; Indicate the processor type used

#include ;Call header file  

__CONFIG _CP_OFF ​​& _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF ​​& _CPD_OFF

 

;*****************Variable definitions*******************************************************

 

BIN EQU 0x20 ; store binary numbers

BCD EQU 0x21 ; store compressed BCD code

BCDLO EQU 0x22 ; store the lower four bits of the BCD code

BCDHI EQU 0x23 ; store the high four bits of the BCD code

COUNT EQU 0x24 ; Multiply the high bit by 10 (add itself 9 times) loop variable

;****************************************************** ******************************

    ORG 0x0000 ; Reset entry address

    nop ; compatible with ICD debugging tools, nop must be added

    goto Main; Jump to Main function

;*****************************Main function code**********************************

Main                                               

    MOVLW b'01000101' ; BCD code 01000101 sent to BCD

    MOVWF BCD  

    MOVLW .10 ; Initialize the number of loops    

    MOVWF COUNT

    CLRF BIN ; clear   

    CALL BCD2Bin ; Call the conversion program

    nop

    goto $ ; stop

;**************************Compress BCD code to binary subroutine********************

    ORG 0X0100

BCD2Bin

    MOVLW b'00001111'            

    ANDWF BCD,W ; Clear the high four bits of BCD and store the result in the W register

    MOVWF BCDLO ; Get the lower four bits of BCD

    MOVLW b'11110000'           

    ANDWF BCD,W ; Clear the lower four bits of BCD and store the result in the W register

    MOVWF BCDHI                 

    SWAPF BCDHI,F ; Get the high four bits of BCD

    MOVF BCDHI,W

    CALL MPY10 ; Call MPY10 and multiply the upper four bits by 10

    nop

    MOVF BCDLO,W

    ADDWF BIN,F ; add the lower four bits to complete the conversion

    RETURN

MPY10    

    ADDWF BIN,F

    DECFSZ COUNT,F ; The upper 4 bits are multiplied by 10 and stored in BIN, that is, add the upper 4 bits 10 times

    GOTO MPY10

    RETURN 

;****************************************************** ******************************

END ; End of program


Reference address:PIC microcontroller realizes the conversion between binary code and compressed BCD code

Previous article:PIC microcontroller lights up LED in various ways
Next article:PIC microcontroller I2C communication (slave mode)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号