单片机c语言bin码与bcd码,PIC单片机的BIN码转BCD码

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

;                  Binary To BCD Conversion RouTIne

;      This rouTIne converts a 16 Bit binary Number to a 5 Digit

; BCD Number. This rouTIne is useful since PIC16C55 & PIC16C57

; have  two 8 bit ports and one 4 bit port ( total of 5 BCD digits)

;

;       The 16 bit binary number is input in locaTIons H_byte and

; L_byte with the high byte in H_byte.

;       The 5 digit BCD number is returned in R0, R1 and R2 with R0

; containing the MSD in its right most nibble.

;

;   Performance :

;               Program Memory  :       35

;               Clock Cycles    :       885

;

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

;

count  equ      16

temp   equ      17

;

H_byte  equ     10

L_byte  equ     11

R0      equ     12              ; RAM Assignments

R1      equ     13

R2      equ     14

;

include         "picreg.h"

;

B2_BCD  bcf     STATUS,0                ; clear the carry bit

movlw   .16

movwf   count

clrf    R0

clrf    R1

clrf    R2

loop16  rlf     L_byte

rlf     H_byte

rlf     R2

rlf     R1

rlf     R0

;

decfsz  count

goto    adjDEC

RETLW   0

;

adjDEC  movlw   R2

movwf   FSR

call    adjBCD

;

movlw   R1

movwf   FSR

call    adjBCD

;

movlw   R0

movwf   FSR

call    adjBCD

;

goto    loop16

;

adjBCD  movlw   3

addwf   0,W

movwf   temp

btfsc   temp,3          ; test if result > 7

movwf   0

movlw   30

addwf   0,W

movwf   temp

btfsc   temp,7          ; test if result > 7

movwf   0               ; save as MSD

RETLW   0

;

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

;               Test Program

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

main    movlw   0FF

movwf   H_byte

movwf   L_byte          ; The 16 bit binary number = FFFF

call    B2_BCD          ; After conversion the Decimal Number

;                               ; in R0,R1,R2 = 06,55,35

;

self    goto    self

;

org     1FF

goto    main

;

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值