#define PAGE EJECT
TITLE "BCD ArithmeTIc RouTInes : Ver 1.0"
;*******************************************************************
; BCD ArithmeTIc Routines
;*******************************************************************
LIST columns=120, WRAP, L=0
include "17c42.h"
CBLOCK 0x20
Lbyte, Hbyte
R2, R1, R0 ;must maintain R2, R1, R0 sequence
count
Num1, Num2
ENDC
;
BCD equ Num1
Htemp equ Num1
Ltemp equ Num2
;
PAGE
ORG 0x0000
;*******************************************************************
; BCD Arithmetic Test Program
;*******************************************************************
;
main
setf Hbyte
setf Lbyte
; ; 16 bit binary num = 0xffff
call B2_BCD_Looped ; after conversion the Decimal Num
; ; in R0, R1, R2 = 06,55,35
setf Hbyte
setf Lbyte
call B2_BCD_Straight ; same as above, but straight line code
;
movlw 0x06
movwf R0
movlw 0x55
movwf R1
movlw 0x35
movwf R2 ; setf R0R1R2 = 65535
;
call BCDtoB ; after conversion Hbyte = 0xff
; ; and Lbyte = 0xff
movlw 0x99
movwf Num1
movlw 0x99
movwf Num2 ; setf Num1 = Num2 = 0x99 (max BCD)
;
call BCDAdd ; after addition, Num2 = 98
; ; and Num1 = 01 ( 99+99 = 198)
;
movlw 0x63 ; setf Wreg = 63 hex
call BinBCD ; after conversion, BCD = 99
; ; 63 hex = 99 decimal.
;
self goto self
;
PAGE
;*******************************************************************;
; Binary To BCD Conversion Routine (8 bit)
;
; This routine converts the 8 bit binary number in the W Reg
; to a 2 digit BCD number in location BCD( compacted BCD Code)
; The least significant digit is returned in location LSD and
; the most significant digit is returned in location MSD.
;
; Performance :
; Program Memory : 10
; Clock Cycles : 62 (worst case when W = 63 Hex )
; ( i.e max Decimal number 99 )
;*******************************************************************
;
BinBCD
clrf BCD
again
addlw -10
btfss _carry
goto swapBCD
incf BCD
goto again
swapBCD
addlw 10
swapf BCD
iorwf BCD
return
;
PAGE
;********************************************************************
; Binary To BCD Conversion Routine (16 Bit)
; (LOOPED Version)
;
; This routine converts a 16 Bit binary Number to a 5 Digit
; BCD Number.
;
; The 16 bit binary number is input in locations Hbyte and
; Lbyte with the high byte in Hbyte.
; 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 : 32
; Clock Cycles : 750
;
;*******************************************************************;
;
B2_BCD_Looped
bsf _fs0
bsf _fs1 ; set fsr0 for no auto increment
;
bcf _carry
clrf count
bsf count,4 ; set count = 16
clrf R0
clrf R1
clrf R2
loop16a
rlcf Lbyte
rlcf Hbyte
rlcf R2
rlcf R1
rlcf R0
;
dcfsnz count
return
adjDEC
movlw R2 ; load R2 as indirect address ptr
movwf fsr0
call adjBCD
;
incf fsr0
call adjBCD
;
incf fsr0
call adjBCD
;
goto loop16a
;
adjBCD
movfp indf0,wreg
addlw 0x03
btfsc wreg,3 ; test if result > 7
movwf indf0
movfp indf0,wreg
addlw 0x30
btfsc wreg,7 ; test if result > 7
movwf indf0 ; save as MSD
return
;
;********************************************************************
; Binary To BCD Conversion Routine (16 Bit)
; (Partial Straight Line Version)
;
; This routine converts a 16 Bit binary Number to a 5 Digit
; BCD Number.
;
; The 16 bit binary number is input in locations Hbyte and
; Lbyte with the high byte in Hbyte.
; 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 : 44
; Clock Cycles : 572
;
;*******************************************************************;
;
B2_BCD_Straight
bsf _fs0
bsf _fs1 ; set fsr0 for no auto increment
;
bcf _carry
clrf count
bsf count,4 ; set count = 16
clrf R0
clrf R1
clrf R2
loop16b
rlcf Lbyte
rlcf Hbyte
rlcf R2
rlcf R1
rlcf R0
;
dcfsnz count
return ; DONE
movlw R2 ; load R2 as indirect address ptr
movwf fsr0
; adjustBCD
movfp indf0,wreg
addlw 0x03
btfsc wreg,3 ; test if result > 7
movwf indf0
movfp indf0,wreg
addlw 0x30
btfsc wreg,7 ; test if result > 7
movwf indf0 ; save as MSD
;
incf fsr0
; adjustBCD
movfp indf0,wreg
addlw 0x03
btfsc wreg,3 ; test if result > 7
movwf indf0
movfp indf0,wreg
addlw 0x30
btfsc wreg,7 ; test if result > 7
movwf indf0 ; save as MSD
;
incf fsr0
; adjustBCD
movfp indf0,wreg
addlw 0x03
btfsc wreg,3 ; test if result > 7
movwf indf0
movfp indf0,wreg
addlw 0x30
btfsc wreg,7 ; test if result > 7
movwf indf0 ; save as MSD
;
goto loop16b
;
PAGE
;*********************************************************
; BCD To Binary Conversion
;
; This routine converts a 5 digit BCD number to a 16 bit binary
; number.
; The input 5 digit BCD numbers are asumed to be in locations
; R0, R1 & R2 with R0 containing the MSD in its right most nibble.
;
; The 16 bit binary number is output in registers Hbyte & Lbyte
; ( high byte & low byte repectively ).
;
; The method used for conversion is :
; input number X = abcde ( the 5 digit BCD number )
; X = (R0,R1,R2) = abcde = 10[10[10[10a+b]+c]+d]+e
;
; Performance :
; Program Memory : 30
; Clock Cycles : 112
;
;***********************************************;
;
mpy10b
andlw 0x0f
addwf Lbyte
btfsc _carry
incf Hbyte
mpy10a
bcf _carry ; multiply by 2
rlcf Lbyte,w
movwf Ltemp
rlcf Hbyte,w ; (Htemp,Ltemp) = 2*N
movwf Htemp
;
bcf _carry ; multiply by 2
rlcf Lbyte
rlcf Hbyte
bcf _carry ; multiply by 2
rlcf Lbyte
rlcf Hbyte
bcf _carry ; multiply by 2
rlcf Lbyte
rlcf Hbyte ; (Hbyte,Lbyte) = 8*N
;
movfp Ltemp,wreg
addwf Lbyte
movfp Htemp,wreg
addwfc Hbyte
return ; (Hbyte,Lbyte) = 10*N
;
;
BCDtoB
clrf Hbyte
movfp R0,wreg
andlw 0x0f
movwf Lbyte
call mpy10a ; result = 10a+b
;
swapf R1,w
call mpy10b ; result = 10[10a+b]
;
movfp R1,wreg
call mpy10b ; result = 10[10[10a+b]+c]
;
swapf R2,w
call mpy10b ; result = 10[10[10[10a+b]+c]+d]
;
movfp R2,wreg
andlw 0x0f
addwf Lbyte
btfsc _carry
incf Hbyte ; result = 10[10[10[10a+b]+c]+d]+e
return ; BCD to binary conversion done
;
PAGE
;***********************************************;
;
; Unsigned BCD Addition
;
; This routine performs a 2 Digit Unsigned BCD Addition
; It is assumed that the two BCD numbers to be added are in
; locations Num1 & Num2. The result is the sum of Num1+Num2
; and is stored in location Num2 and the overflow carry is returned
; in location Num1
;
; Performance :
; Program Memory : 5
; Clock Cycles : 5
;
;*****************************************;
;
BCDAdd
movfp Num1,wreg
addwf Num2,w ; perform binary addition
daw Num2 ; adjust for BCD addition
clrf Num1
rlcf Num1 ; set Num1 = carry bit
return
;
;******************************************************
;
END
Previous article:How to solve the problem of hardware deadlock of PIC microcontroller
Next article:What are the application designs of PIC8-bit microcontrollers? What are the techniques?
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- MOS transistor driving voltage and current
- [ESP32-S2-Kaluga-1 Review] 3. ADC button test
- This week's highlights
- For the STM32 MCU, is it generally better to choose a high frequency or a low frequency external main crystal oscillator? Is there any technical principle?
- Long-lasting and durable electric motorcycle 16S-17S lithium-ion battery pack solution BQ76940
- MSP430F249—SPI Master-Slave Communication
- MicroPython Hands-on (03) - Learn to boot up the computer with MaixPy from scratch
- [RVB2601 Creative Application Development] How to use font tools
- Why do we need LoRa and NB-IoT when we have GPRS?
- Recommend a network disk that supports automatic synchronization - TeraCLOUD, supports WebDav