⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8bitmath.c

📁 pic16c6x和pic16c7xxx都可以通用
💻 C
字号:
/******************************************************************************
* 8 bit Multiply and Scaling Routines 
*
* Written for "Digital Signal Processing with the PIC16C74" Application Note.
*
*
* This module provides a 8 bit signed multiply and scaling routine for the 
* PICTONE.C tone generation program. The routines are adapted from "Math
* Routines for the 16C5x" in  Microchip's Embedded Controller Handbook.
*
* All numbers are assumed to be signed 2's compliment format.
*
* D. Mostowfi 11/94
*******************************************************************************/
char multcnd;                /* 8 bit multiplicand */
char multplr;                /* 8 bit multiplier */
char result_h;               /* result - high byte */
char result_l;               /* result - low byte */
char sign;                   /* result sign */

#asm

;
; 8x8 signed multiply routine
; called from PICTONE.C module (assembly language routine)
;
 .MACRO mult_core bit           
        btfss   multplr,bit
        goto    \no_add
        movf    multcnd,W
        addwf   result_h,F

\no_add:
        rrf     result_h
        rrf     result_l
 .ENDM


_8x8smul:
        movf    multcnd,W       ; get multiplicand
        xorwf   multplr,W       ; and xor with multiplier
        movwf   sign            ; and save sign of result
        btfss   multcnd,7       ; check sign bit of multiplicand
        goto    chk_multplr     ; go and check multipier if positive
        comf    multcnd         ; negate if negative
        incf    multcnd         ;

chk_multplr:
        btfss   multplr,7       ; check sign bit of multiplier
        goto    multiply        ; go to multiply if positive
        comf    multplr         ; negate if negative
        incf    multplr         ;

multiply:
        movf    multcnd,W       ; set up multiply registers
        bcf     STATUS,C        ;
        clrf    result_h        ;
        clrf    result_l        ;
        mult_core 0             ; and do multiply core 8 times
        mult_core 1             ;
        mult_core 2             ;
        mult_core 3             ;
        mult_core 4             ;
        mult_core 5             ;
        mult_core 6             ;
        mult_core 7             ;
                                
set_sign:
        btfss  sign,7           ; test sign to see if result negative
        retlw  0                ; done if not! (clear W)
        comf   result_l         ; negate result if sign set
        incf   result_l         ;
        btfsc  STATUS,Z         ;
        decf   result_h         ;
        comf   result_h         ;
                                
        retlw  0                ; done (clear W)
                                

;
; Scaling Routine (used after a multiply to scale 16 bit result)
; Operates on result_h and result_l - final result is in result_l
; routine divides by 32 to restore Q7 result of 2*b*y(n-1) in tone
; generation algorithm
;
scale_16:                       
        btfss  sign,7           ; test if negative (sign set from mult)
        goto   start_shift      ; go to start shift if pos.
        comf   result_l         ; negate first if neg.
        incf   result_l         ;
        btfsc  STATUS,Z         ;
        decf   result_h         ;
        comf   result_h         ;
                                
start_shift:
        bcf    STATUS,C         ; clear status
        rrf    result_h         ; and shift result left 5x (/32)
        rrf    result_l         ;
        rrf    result_h         ;
        rrf    result_l         ;
        rrf    result_h         ;
        rrf    result_l         ;
        rrf    result_h         ;
        rrf    result_l         ;
        rrf    result_h         ;
        rrf    result_l         ;

        btfss  sign,7           ; test if result negative
        goto   scale_done       ; done if not negative 
        comf   result_l         ; negate result if negative
        incf   result_l         ;
        btfsc  STATUS,Z         ;
        decf   result_h         ;
        comf   result_h         ;

scale_done:                     ; 

        retlw  0                ; done (clear W)

#endasm

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -