历史上的今天
今天是:2024年12月12日(星期四)
2019年12月12日 | PIC单片机的BCD码处理程序
2019-12-12 来源:elecfans
#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
史海拾趣
|
跪求高手 大致解释下每一行是干什么可以么? 万分感谢!! :( ------------------------------------------------- DELAY: ...… 查看全部问答> |
|
2010年由于电子产品逐渐进入需求高峰之后的淡季,PC、Notebook、小笔电等成长率趋缓,手机芯片、模拟IC、驱动IC、内存IC等需求处于回升。2010年台湾IC设计业产值突破新台币2300亿元,比去年同期成长35%。这显示2010年初景气复苏渐露曙光,终端 ...… 查看全部问答> |
|
wince 6.0 pxa270 再进行了初始化,和中断的设置后,按键一直有中断。而且是在没有按下按键的情况下。 这样的问题会是什么原因造成的呢? kpc设置问题?还是中断触发的设置问题,或是中断设置问题?一直没有搞明白,为什么还没有操作按键就一直 ...… 查看全部问答> |
|
个版本的tornado及其他软件的密码,经验证,在笔记本上安装Tornado成功。 转载: 共分三个类别:嵌入式系统开发软件,PCB、FPGA、IC开发软件,射频以及其他EDA软件 嵌入式系统开发软件 一、WINDRIVER.TORNADO.V2.2.FOR.COLDFIRE CD1 ...… 查看全部问答> |
|
头写了一个防火墙程序,现在本人手里有sys和两个inf,一个是过滤器的inf,一个是虚拟设备的驱动,跟passthru的差不多,手动安装没有问题,可是跟据bindview的例子写的程序自动安装就是不起作用,网络组件已经显示出来了,可是就是不起作用,弄了一个 ...… 查看全部问答> |
|
绿色照明应用热潮已经到来,LED照明则成为不可逆转的趋势。2012年,日本将全面禁用白炽灯,东芝照明技术更是决定在2012年之前停产普通白炽灯泡,关闭全部生产线。台湾地区相关部门也宣布,截至2012年底全面禁止高能耗的白炽灯泡的使用。而欧盟早在2 ...… 查看全部问答> |
|
这两天测试了STM32的速度。在72M的时钟下,我得到的数据大概如下:整数加减乘除 大概 4千万/S浮点运算就慢慢了 20万/S 左右。我是自己编写的测 ...… 查看全部问答> |




