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

📄 bin2bcd.s51

📁 Analog 公司 ADE7169 SOC 电表方案DEMO程序
💻 S51
字号:
/*
-2006/09/21: Petre M.
  This function computes 8 digits of a 32 bit number.
  The largest positive number that can be represented is
    99,999,999=0x05f5e0ff
  The smallest negative number that can be represented is
    -9,999,999=0xff676981
  The function uses Temporary[8] as the input folder:
    -The input 32 bit number has to be introduced in the following manner:
      Temporary[0]=Byte0
      Temporary[1]=Byte1
      Temporary[2]=Byte2
      Temporary[3]=Byte3
    -The output is obtained in the following form in Temporary1[0]:
      Temporary1[0]=Digit0 (1s)
      Temporary1[1]=Digit1 (10s)
      Temporary1[2]=Digit2 (100s)
      Temporary1[3]=Digit3 (1,000s)
      Temporary1[4]=Digit4 (10,000s)
      Temporary1[5]=Digit5 (100,000s)
      Temporary1[6]=Digit6 (1,000,000s)
      Temporary1[7]=Digit7 (10,000,000s or - negative sign)

  If there is an error during computations, the entire Temporary1 buffer
    is written with 0x0A because minus sign LCD display is the 10th
    element in Digit_Table[11] table

*/



#include "iar_common.h"
;--------------------------------------
  MODULE    bin2bcd
  PUBLIC    Bin2bcd
  FUNCTION  Bin2bcd,0203H
  EXTERN    Temporary, Temporary1

  RSEG NEAR_CODE:CODE:NOROOT(0)
Bin2bcd:

    MOV   A,Temporary+3
    JNB   ACC.7,POSITIVE
                              ;If the number is negative, compute its complement first
    CLR   C                    ; negative argument
    CLR   A
    SUBB  A,Temporary
    MOV   Temporary,A
    CLR   A
    SUBB  A,Temporary+1
    MOV   Temporary+1,A
    CLR   A
    SUBB  A,Temporary+2
    MOV   Temporary+2,A
    CLR   A
    SUBB  A,Temporary+3
    JNZ   ERROR                ; should be ZERO

    MOV   R3,#7                ;"Digit" Counter
    MOV   R2,#6                ;"Nibble" Counter

    MOV   Temporary1+7,#0AH     ; 0x0A as '-' Character
                              ; (will be converted for
                              ;  8-seg. LCD)
    CLR   A
    MOV   Temporary1, A           ;Temporary1[0] = 0x00

    MOV   R1, #Temporary+2        ;pointer to highest byte that needs to be swaped
    MOV   R0, #Temporary1+1       ;pointer to the place where the highest nibble should be placed

    SJMP  Begin_process


POSITIVE:

    MOV   A,Temporary+3           ; the most significant 5 bits of the number should be 0
    ANL   A, #0F8H                 ; should be ZERO
    JNZ   ERROR

    MOV   R3,#8             ;"Digit" Counter
    MOV   R2,#7             ;"Nibble" Counter

    MOV   R1, #Temporary+3        ;pointer to highest byte that needs to be swaped
    MOV   R0, #Temporary1       ;pointer to the place where the highest nibble should be placed

Begin_process:
    CALL  Comp_Nibbles

    MOV   R1,#Temporary1           ;"Digit" Pointer
    MOV   R0,#Temporary1+1         ;"Nibble" Pointer

    CLR   A
    SJMP  LOOP_DIV    ; everything is in place now

LOOP_DIGIT:

    MOV   0x00,0x01
    MOV   0x02,0x03
    CLR   A

LOOP_DIV:
    XCHD  A,@R0
    MOV   B,#10       ; DIVISOR
    DIV   AB
    XCHD  A,@R0
    MOV   A,B
    SWAP  A           ; Remainder in AKKU HIGH NIBBLE
    INC   R0
    DJNZ  R2,LOOP_DIV

    MOV   A,@R1        ; should be ZERO
    JNZ   ERROR
    MOV   @R1,B
    INC   R1
    DJNZ  R3,LOOP_DIGIT

END1:
    RET

ERROR:
    MOV A,#0AH         ; write 0x0A as '-' Character all over Temporary1 buffer
    MOV R0,#Temporary1+7
Write_again:
    MOV @R0, A
    DEC R0
    CJNE R0, #Temporary1-1, Write_again

END2:
    RET

//The function Comp_Nibbles takes bytes from Temporary buffer and
//puts the corresponding nibbles in a buffer indicated by R0
//Therefore, the inputs to this routine have to be:
//    R0 = #Temporary to move only the nibbles from LS byte B0
//    R0 = #Temporary+1 to move nibbles from B1 and B0
//    R0 = #Temporary+2 to move nibbles from B2 to B0
//    R0 = #Temporary+3 to move nibbles from B3 to B0

//    R1 = #Temporary1 = the place where the highest nibble should be placed


  PUBLIC    Comp_Nibbles
  FUNCTION  Comp_Nibbles,0203H
  EXTERN Temporary, Temporary1

  RSEG NEAR_CODE:CODE:NOROOT(0)


Comp_Nibbles:
    MOV   A,@R1
    SWAP  A
    ANL   A,#0FH
    MOV   @R0,A                   ; LSB2 HN
    MOV   A,@R1
    ANL   A,#0FH
    INC   R0
    MOV   @R0,A                   ; LSB2 LN
    INC   R0
    DEC   R1
    CJNE  R1,#Temporary-1,Comp_Nibbles

    RET

//The function Comp_Nibbles2 takes bytes from Temporary buffer and
//puts the corresponding nibbles in a buffer indicated by R0
//Therefore, the inputs to this routine have to be:
//    R1 = #Temporary to move only the nibbles from LS byte B0
//    R0 = #Temporary1+1 = the place where the highest nibble should be placed
//
//    R1 = #Temporary+1 to move nibbles from B1 and B0
//    R0 = #Temporary1+3 = the place where the highest nibble should be placed
//
//    R1 = #Temporary+2 to move nibbles from B2 to B0
//    R0 = #Temporary1+5 = the place where the highest nibble should be placed
//
//    R1 = #Temporary+3 to move nibbles from B3 to B0
//    R0 = #Temporary1+7 = the place where the highest nibble should be placed

  PUBLIC    Comp_Nibbles2
  FUNCTION  Comp_Nibbles2,0203H
  EXTERN Temporary, Temporary1

  RSEG NEAR_CODE:CODE:NOROOT(0)


Comp_Nibbles2:
    MOV   A,@R1
    SWAP  A
    ANL   A,#0FH
    MOV   @R0,A                   ; LSB2 HN
    MOV   A,@R1
    ANL   A,#0FH
    DEC   R0
    MOV   @R0,A                   ; LSB2 LN
    DEC   R0
    DEC   R1
    CJNE  R1,#Temporary-1,Comp_Nibbles2

    RET




  END

⌨️ 快捷键说明

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