《電子技術(shù)應(yīng)用》
您所在的位置:首頁(yè) > 嵌入式技術(shù) > 解決方案 > PIC單片機(jī)的BIN碼轉(zhuǎn)BCD碼

PIC單片機(jī)的BIN碼轉(zhuǎn)BCD碼

2009-01-16
關(guān)鍵詞: PIC 源代碼

PIC單片機(jī)的BIN碼轉(zhuǎn)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

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點(diǎn)。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無(wú)法一一聯(lián)系確認(rèn)版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問(wèn)題,請(qǐng)及時(shí)通過(guò)電子郵件或電話(huà)通知我們,以便迅速采取適當(dāng)措施,避免給雙方造成不必要的經(jīng)濟(jì)損失。聯(lián)系電話(huà):010-82306118;郵箱:aet@chinaaet.com。