PIC microcontroller implements double-byte unsigned number division

Publisher:乐呵的挑Latest update time:2018-07-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Write a subroutine Div_16 to implement double-byte unsigned number division. Define variables by yourself, where:

  • ACCALO; store the lower 8 bits of the dividend

  • ACCAHI; store the high 8 bits of the dividend

  • ACCBLO; store the divisor 8 bits

  • ACCCLO; store remainder 8 bits

  • ACCCHI; Depositor 8 digits

Using the shift method, let the dividend be ACCA, the divisor be ACCBLO, the quotient be ACCCHI (initial value is 0), and the remainder be ACCCLO (initial value is 0). ACCA is shifted left from high to low and enters the ACCCLO unit. After each shift, the values ​​of the ACCCLO unit and ACCBLO are compared. If ACCCLO ≥ ACCBLO, the value of the quotient unit of this bit is set to 1, and then ACCBLO is subtracted from ACCCLO, otherwise no operation is performed. Until all ACCA enters the ACCCLO unit. At this time, the quotient is stored in ACCCHI and the remainder is stored in ACCCLO.

Program flowchart:


list p=16f877A ; Indicate the processor type used

#include ;Call header file 

__CONFIG    _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_OFF & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF

 

;*****************Variable definitions*******************************************************

 

ACCALO EQU 0x20 ; store the lower 8 bits of the dividend

ACCAHI EQU 0x21 ; store the high 8 bits of the dividend

ACCBLO EQU 0x22 ; store the divisor 8 bits

ACCCLO EQU 0x23 ; Store the remainder 8 bits

ACCCHI EQU 0x24 ; Stores 8 bits

COUNT EQU 0x25 ; store loop variables

 

;*******************************************************************************

    ORG 0x0000 ; Reset entry address

    nop ; compatible with ICD debugging tools, nop must be added

    goto Main; Jump to Main function

;*****************************Main function code**********************************

Main                                               

    MOVLW 0x2A ; send the high 8 bits of the dividend

    MOVWF  ACCAHI                

    MOVLW 0x3B ; send the lower 8 bits of the dividend 

    MOVWF  ACCALO

    MOVLW 0x4C ; send divisor

    MOVWF  ACCBLO   

    MOVLW .16 ; Initialize loop variables    

    MOVWF  COUNT

    CLRF ACCCLO ; Clear result unit

    CLRF ACCCHI   

    CALL Div_16 ; Call the subroutine to solve, the result should be quotient 0x8E, remainder 0x13

    nop

    goto $ ; stop

;**************************Compress BCD code to binary subroutine********************

    ORG    0X0100

Div_16  

    RLF    ACCALO

    RLF    ACCAHI

    RLF ACCCLO ; The dividend is shifted left from high to low into the remainder unit

    MOVF ACCBLO,W

    BSF STATUS, C ; C position 1

    SUBWF ACCCLO,W ; Compare remainder unit with divisor unit

    BTFSC  STATUS,C              ; 若ACCCLO>=ACCBLO   

    CALL SUB_C_B ; then the bit quotient unit is set to 1, and the remainder unit is subtracted from the divisor, otherwise the bit quotient unit is set to 0  

    RLF    ACCCHI                      

    DECFSZ COUNT,F               

    GOTO   Div_16

    RETURN

SUB_C_B   

    MOVF ACCBLO,W ; Subtract the divisor from the remainder unit

    SUBWF  ACCCLO,F     

    RETURN 

;*****************************************************************************

END ; End of program


Reference address:PIC microcontroller implements double-byte unsigned number division

Previous article:PIC Microcontroller Introduction_Instruction System
Next article:Teach you how to use PIC microcontroller to drive relay

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号