《匠人手记》推荐网上购书渠道:
互动出版网(china-pub)购书入口   >>>
当当网(dangdang)购书入口   >>>
卓越亚马逊网 购书入口   >>>
淘宝网(taobao)购书入口   >>>
更多购书渠道……   >>> 

设为首页加入收藏联系匠人管理入口21IC首页21IC博客21IC社区侃单片机回复的贴参与的贴

天气预报
百宝日历
载入中...

百宝专栏

载入中...
最新货色

载入中...

粉丝评论

载入中...

载入中...



百宝信息

载入中...

百宝流量

(2006-07-01开始)


匠人手记

 匠人观点: 好记性不如烂笔头  
 黑色幽默:三鹿门——后世畅想

PIC:BCD码处理程序
程序匠人 发表于 2005-6-2 20:57:00  阅读全文 | 回复(0) | 引用通告 | 编辑

#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

 

看《匠人手记》,与匠人同行!北航出版,正在热卖!

发表评论:
载入中...

芯片专题

器件专题

软件专题

硬件专题

综合专题

项目专题

原创专题

器件检测
LCD LED
按键 触摸键
E2PROM
电池 电机
电阻 电容 电感

指令系统
软件算法
编程规范
滤波算法
串行通讯

PCB设计
I2C PWM
红外遥控
充电技术
中断 ADC 

匠人手记
匠人夜话
网络心路
一周热点串烧
从零开始玩PIC
DIY旋转时钟

广告5号位 [投放]


学习板、开发板、编程器、下载器、仿真器(查看详情……)

广告3号位 [投放]

站内搜索


站外搜索


百度  google
mp3  歌词 
图片  FLASH 
知道  文档
新闻  词典 
地图  mp3 
软件  天网 
雅虎  爱问 
搜狗  讯雷 
网讯  华军 
天空 

21IC器件搜索
百宝箱分站
  • 《匠人的百宝箱》21IC站
  • 《匠人的百宝箱》21IC笔记团队
  • 《匠人手记》21IC书友会
  • 《匠人的百宝箱》MCUBLOG站
  • 《匠人的百宝箱》MCUBLOG笔记团队
  • 《匠人的百宝箱》EDN站
  • 《匠人手记》EDN书友会
  • 《匠人的百宝箱》与非网站
  • 《匠人的百宝箱》新浪站
  • 《匠人的百宝箱》百度站
  • 《匠人的百宝箱》网易126站
  • 《匠人的百宝箱》网易163站
  • 《匠人的百宝箱》互动出版网站
  • 广告4号位 [投放]

     
     

    匠人原创

    往日酷贴

     
     
     

    大千八卦

    友情连接

    新浪新闻:
    新浪财经:
    AK58新闻:
    新浪股票:
    新浪股票:
    证券之星:

     [更多酷站连接]

     

     

    [欢迎交换连接]

    [百宝箱之与非门分舵]

    [电脑圈圈的家当]

    [IC921的博客]

    [柔月阁]

    [八楼的呼吸]

    [hotpower 的水潭]

    [xwj的文君阁]

    [所长的BLOG]

    [阿摆手记]

    [电子伙伴]

    [unaided的笔记]

    [小飞的笔记]

    [单片机开发联盟]

    [网址之家]

    [好东西网址大全]

    [美萍中文精选]

    [数字电视之家]

    [SMARTCODE电子书斋]

    [软件开发之窗]

    [Armoric]

    [我爱研发网]

    [infernal的笔记]

    [雄鹰的空中加油站]

    [SunK]

    [逍遥电子]

    [ningpanda的博客]

    [C-Design]

    [一网见天下]

    [海边淘沙]

    [嵌入式365]

    [水牛的仓库]

    [股剩是怎样炼成的]

    [PIC论坛]

    [ICC AVR开发网]

    [中国高校自动化网]

     

     

     

    MCU博客-中国电子工程师博客网 

    大学生电子网 

     

     

     

     

     

    !!! 《匠人的百宝箱》 !!!