MCS-51 MCU Practical Subroutine Library 7

Publisher:书香门第Latest update time:2015-12-22 Source: eefocusKeywords:MCS-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
25) Label: FTOD Function: Converts a floating point number into a double-byte fixed point number


Entry condition: Formatted floating-point operand in [R0].
Exit information: OV=1 overflow, OV=0 conversion success: The absolute value of the fixed-point number is in [R0] (double byte), the sign
is in bit 1FH, F0=1 is an integer, CY=1 is a one-byte integer and a one-byte decimal, otherwise it is a pure decimal.
Affected resources: PSW, A, B, R2, R3, R4, bit 1FH Stack requirements: 6 bytes
FTOD: LCALL MVR0; transfer [R0] to the first work area
MOV A, R2
JZ FTD4; the exponent is zero, pure decimal
JB ACC.7, FTD4; the exponent is negative, pure decimal
SETB C
SUBB A, #10H
JC FTD1
SETB OV; the exponent is greater than 16, overflow
RET
FTD1: SETB C
MOV A, R2
SUBB A, #8; is the exponent greater than 8?
JC FTD3
FTD2: MOV B,#10H ; If the exponent is greater than 8, convert it as a double-byte integer
LCALL FTD8
SETB F0 ; Set up a double-byte integer flag
CLR C
CLR OV
RET
FTD3: MOV B,#8 ; Convert it as a one-byte integer to a one-byte decimal
LCALL FTD8
SETB C ; Set up a one-byte integer to a one-byte decimal flag
CLR F0
CLR OV
RET
FTD4: MOV B,#0 ; Convert it as a pure decimal
LCALL FTD8
CLR OV ; Set up a pure decimal flag
CLR F0
CLR C
RET
FTD8: MOV A,R2 ; Right-normalize according to the specified number of integer digits
CJNE A,B,FTD9
MOV A,R3 ; Transfer the double-byte result to [R0]
MOV @R0,A
INC R0
MOV A,R4
MOV @R0,A
DEC R0
RET
FTD9: CLR C
LCALL RR1 ; Right-normalize once
SJMP FTD8

(26) Label: BTOF Function: Convert floating-point BCD code to formatted floating-point number

Entry condition: The floating-point BCD code operand is in [R0].
Exit information: The converted formatted floating-point number is still in [R0].
Affected resources: PSW, A, B, R2~R7, bits 1DH~1FH Stack requirement: 6 bytes
BTOF: INC R0; Check whether it is zero.
INC R0
MOV A,@R0
MOV R7,A
DEC R0
MOV A,@R0
MOV R6,A
DEC R0
ORL A,R7
JNZ BTF0
MOV @R0,#41H; If it is zero, the conversion ends.
RET
BTF0: MOV A,@R0
MOV C,ACC.7
MOV 1DH,C; Save the number sign.
CLR 1FH; Convert with absolute value.
MOV C,ACC.6; Expand the exponent to eight bits.
MOV ACC.7,C
MOV @R0,A
JNC BTF1
ADD A,#19 ;Is it less than 1E-19?
JC BTF2
MOV @R0,#41H ;When it is less than 1E-19, it is counted as 0.
INC R0
MOV @R0,#0
INC R0
MOV @R0,#0
DEC R0
DEC R0
RET
BTF1: SUBB A,#19
JC BTF2
MOV A,#3FH ;When it is greater than 1E19, it is capped.
MOV C,1DH
MOV ACC.7,C
MOV @R0,A
INC R0
MOV @R0,#0FFH
INC R0
MOV @R0,#0FFH
DEC R0
DEC R0
RET
BTF2: CLR A ;Prepare to convert the mantissa of the BCD code into a hexadecimal floating point number.
MOV R4,A
MOV R3,A
MOV R2,#10H ;At least two bytes.
BTF3: MOV A,R7
ADD A,R7
DA A
MOV R7,A
MOV A,R6
ADDC A,R6
DA A
MOV R6,A
MOV A,R4
RLC A
MOV R4,A
MOV A,R3
RLC A
MOV R3,A
DEC R2
JNB ACC.7,BTF3; until the mantissa is normalized.
MOV A,R6; rounding.
ADD A,#0B0H
CLR A
ADDC A,R4
MOV R4,A
CLR A
ADDC A,R3
MOV R3,A
JNC BTF4
MOV R3,#80H
INC R2
BTF4: MOV DPTR,#BTFL; prepare to look up the table to get the floating point number corresponding to the decimal exponent.
MOV A,@R0
ADD A,#19; calculate the table offset.
MOV B,#3
MUL AB
ADD A,DPL
MOV DPL,A
JNC BTF5
INC DPH
BTF5: CLR A; look up the table.
MOVC A,@A+DPTR
MOV C,ACC.6
MOV ACC.7,C
MOV R5,A
MOV A,#1
MOVC A,@A+DPTR
MOV R6,A
MOV A,#2
MOVC A,@A+DPTR
MOV R7,A
LCALL MUL1 ;Multiply the floating-point number corresponding to the exponent and the floating-point number corresponding to the mantissa.
MOV C,1DH ;Extract the sign.
MOV 1FH,C
LJMP MOV0 ;Transfer the conversion result. [page]

(27) Label: FTOB Function: Convert formatted floating-point numbers into floating-point BCD codes

Entry condition: The formatted floating-point operand is in [R0].
Exit information: The converted floating-point BCD code is still in [R0].
Affected resources: PSW, A, B, R2~R7, bits 1DH~1FH Stack requirement: 6 bytes
FTOB: INC R0
MOV A,@R0
INC R0
ORL A,@R0
DEC R0
DEC R0
JNZ FTB0
MOV @R0,#41H
RET
FTB0: MOV A,@R0
MOV C,ACC.7
MOV 1DH,C
CLR ACC.7
MOV @R0,A
LCALL MVR0
MOV DPTR,#BFL0; Start of table lookup when the absolute value is greater than or equal to 1.
MOV B,#0; Ten to the power of 0.
MOV A,R2
JNB ACC.7,FTB1
MOV DPTR,#BTFL; Start of table lookup when the absolute value is less than 1E-6.
MOV B,#0EDH; Ten to the power of -19.
ADD A,#16
JNC FTB1
MOV DPTR,#BFLN; The starting point of the table lookup when the absolute value is greater than or equal to 1E-6.
MOV B,#0FAH; Ten to the power of -6.
FTB1: CLR A; Look up the table and find an integer power greater than the floating point number to be converted.
MOVC A,@A+DPTR
MOV C,ACC.6
MOV ACC.7,C
MOV R5,A
MOV A,#1
MOVC A,@A+DPTR
MOV R6,A
MOV A,#2
MOVC A,@A+DPTR
MOV R7,A
MOV A,R5; Compare with the floating point number to be converted.
CLR C
SUBB A,R2
JB ACC.7,FTB2; The difference is negative.
JNZ FTB3
MOV A,R6
CLR C
SUBB A,R3
JC FTB2
JNZ FTB3
MOV A,R7
CLR C
SUBB A,R4
JC FTB2
JNZ FTB3
MOV R5,B ;It is exactly the number in the table.
INC R5 ;Add one to the power.
MOV R6,#10H ;The mantissa is 0.1000.
MOV R7,#0
SJMP FTB6 ;Transfer the conversion result.
FTB2: INC DPTR ;Prepare the next item in the table.
INC DPTR
INC DPTR
INC B ;Add one to the power.
SJMP FTB1 ;
FTB3: PUSH B ;Save the power value.
LCALL DIV3 ;Divide and get a pure decimal of a binary floating point number.
FTB4: MOV A,R2 ;Get the exponent.
JZ FTB5 ;Is it zero?
CLR C
LCALL RR1 ;Right normalize.
SJMP FTB4
FTB5: POP ACC; Get the power value.
MOV R5,A; As the exponent of the decimal floating point number.
LCALL HB2 ;Convert the tenth and hundredth digits of the mantissa.
MOV R6,A
LCALL HB2 ;Convert the thousandth and ten-thousandth digits of the mantissa.
MOV R7,A
MOV A,R3 ;Round.
RLC A
CLR A
ADDC A,R7
DA A
MOV R7,A
CLR A
ADDC A,R6
DA A
MOV R6,A
JNC FTB6
MOV R6,#10H
INC R5
FTB6: INC R0 ;Store the conversion result.
INC R0
MOV A,R7
MOV @R0,A
DEC R0
MOV A,R6
MOV @R0,A
DEC R0
MOV A,R5
MOV C,1DH ;Take out the sign.
MOV ACC.7,C
MOV @R0,A
RET
HB2: MOV A,R4 ;Enlarge the mantissa by 100 times.
MOV B,#100
MUL AB
MOV R4,A
MOV A,B
XCH A,R3
MOV B,#100
MUL AB
ADD A,R3
MOV R3,A
JNC HB21
INC B
HB21: MOV A,B; Convert the integer part into BCD code.
MOV B,#10
DIV AB
SWAP A
ORL A,B
RET
BTFL: DB 41H,0ECH,1EH ;1.0000E-19
DB 45H,93H,93H ;1.0000E-18
DB 48H,0B8H,78H ;1.0000E-17
DB 4BH,0E6H,96H ;1.0000E-16
DB 4FH,90H,1DH ;1.0000E-15
DB 52H,0B4H,25H ;1.0000E-14
DB 55H,0E1H,2EH ;1.0000E-13
DB 59H,8CH,0BDH ;1.0000E-12
DB 5CH,0AFH,0ECH ;1.0000E-11
DB 5FH,0DBH,0E7H ;1.0000E-10
DB 63H,89H,70H ;1.0000E-9
DB 66H,0ABH,0CCH ;1.0000E-8
DB 69H,0D6H,0C0H ;1.0000E-7
BFLN: DB 6DH,86H,38H; 1.0000E-6
DB 70H,0A7H,0C6H; 1.0000E-5
DB 73H,0D1H,0B7H; 1.0000E-4
DB 77H,83H,12H; 1.0000E-3
DB 7AH,0A3H,0D7H; 1.0000E
-2DB 7DH,0CCH,0CDH; 1.0000E-1
BFL0: DB 1,80H,00H; 1.0000
DB 4,0A0H,00H; 1.0000E1
DB 7,0C8H,00H; 1.0000E2
DB 0AH,0FAH,00H; 1.0000E3
DB 0EH,9 CH,40H; 1.0000E4
DB 11H,0C3H,50H; 1.0000E5
DB 14H,0F4H,24H; 1.0000E6
DB 18H,98H,97H; 1.0000E7
DB 1BH,0BEH,0BCH; 1.0000E8
DB 1EH,0EEH, 6BH ;1.0000E9
DB 22H,95H,03H ;1.0000E10
DB 25H,0BAH,44H ;1.0000E11
DB 28H,0E8H,0D5H ;1.0000E12
DB 2CH,91H,85H ;1.0000E13
DB 2FH,0B5H, 0E6H; 1.0000E14
DB 32H, 0E3H, 60H; 1.0000E15
DB 36H, 8EH, 1CH; 1.0000E16
DB 39H, 31H, 0A3H; 1.0000E17
DB 3CH, 0DEH, 0BH; 1.0000E18
DB 40H, 8AH ,0C7H ;1.0000E19

 (28) Label: FCOS Function: Floating point cosine function

Entry condition: Operand is in [R0].
Exit information: Result is still in [R0].
Affected resources: DPTR, PSW, A, B, R2~R7, bits 1DH~1FH Stack requirement: 6 bytes
FCOS: LCALL FABS ;COS(-X) = COS X
MOV R5,#1 ;Constant 1.5708 (π/2)
MOV R6,#0C9H
MOV R7,#10H
CLR 1EH
LCALL MVR0
CLR F0
LCALL AS1 ; x + (π/2)
LCALL MOV0 ; Save the result and then run the following FSIN program

(29) Label: FSIN Function: Floating-point sine function

Entry condition: The operand is in [R0].
Exit information: The result is still in [R0].
Affected resources: DPTR, PSW, A, B, R2-R7, bits 1DH-1FH Stack requirement: 6 bytes
FSIN: MOV A,@R0
MOV C,ACC.7
MOV 1DH,C ;Save the sign of the independent variable
CLR ACC.7 ;Calculate uniformly as a positive number
MOV @R0,A
LCALL MVR0 ;Transfer [R0] to the first work area
MOV R5,#0 ;Coefficient 0.636627 (2/π)
MOV R6,#0A2H
MOV R7,#0FAH
CLR 1EH
LCALL MUL1 ;Multiply, the independent variable is normalized by (π/2)
MOV A,R2 ;Copy the result to the second area
MOV R5,A
MOV A,R3
MOV R6,A
MOV A,R4
MOV R7,A
LCALL INT ;Round the first area to obtain quadrant information
MOV A,R2
JZ SIN2
SIN1: CLR C ;Convert floating point number to fixed point number
LCALL RR1
CJNE R2,#10H,SIN1
MOV A,R4
JNB ACC.1,SIN2
CPL 1DH ;For the third and fourth quadrants, negate the result
SIN2: JB ACC.0,SIN3
CPL 1FH ;For the first and third quadrants, directly find the normalized decimal
SJMP SIN4
SIN3: MOV A,R4 ;For the second and fourth quadrants, prepare to find its complement
INC A
MOV R4,A
JNZ SIN4
INC R3
SIN4: LCALL RLN ;Normalization
SETB F0
LCALL AS1 ;Find the normalized equivalent value of the independent variable
LCALL MOV0 ;Return
LCALL FPLN ;Use polynomial to calculate the sine value
DB 7DH,93H,28H ;0.07185
DB 41H,0,0 ;0
DB 80H,0A4H,64H;-0.64215
DB 41H,0,0;0
DB 1,0C9H,2;1.5704
DB 41H,0,0;0
DB 40H;End
MOV A,@R0;Does the absolute value of the result exceed 1.00?
JZ SIN5
JB ACC.6,SIN5
INC R0;The absolute value is capped at 1.00
MOV @R0,#80H
INC R0
MOV @R0,# 0
DEC R0
DEC R0
MOV A,#1
SIN5: MOV C,1DH;Insert the number sign into the result
MOV ACC.7,C
MOV @R0,A
RET

(30) Label: FATN Function: Floating point inverse tangent function

Entry condition: The operand is in [R0].
Exit information: The result is still in [R0].
Affected resources: DPTR, PSW, A, B, R2~R7, bits 1CH~1FH Stack requirement: 7 bytes
FATN: MOV A,@R0
MOV C,ACC.7
MOV 1DH,C; Save the sign of the independent variable
CLR ACC.7; Take the absolute value of the independent variable
MOV @R0,A
CLR 1CH; Clear the remainder operation flag
JB ACC.6,ATN1; Is the independent variable a pure decimal?
JZ ATN1
SETB 1CH ;Set the remainder operation flag
LCALL FRCP ;Convert to pure decimal
ATN1 through reciprocal operation: LCALL FPLN ;Calculate the inverse tangent function value through polynomial operation
DB 0FCH,0E4H,91H;-0.055802
DB 7FH,8FH,37H;0.27922
DB 0FFH,0EDH,0E0H;-0.46460
DB 7BH,0E8H,77H;0.028377
DB 0,0FFH,68H;0.9977
DB 72H,85H,0ECH;3.1930×10-5
DB 40H ;End
JNB 1CH,ATN2;Is remainder operation required?
CPL 1FH ; prepare operation flag
MOV C,1FH
MOV F0,C ; constant 1.5708 (π/2)
MOV R5,#1
MOV R6,#0C9H
MOV R7,#10H
LCALL AS1 ; remainder operation
LCALL MOV0 ; return
ATN2: MOV A,@R0 ; insert the result sign
MOV C,1DH
MOV ACC.7,C
MOV @R0,A
RET

(31) Label: RTOD Function: Convert floating-point radians to floating-point degrees

Entry condition: The floating-point radians is in [R0].
Exit information: The converted floating-point degrees are still in [R0].
Affected resources: PSW, A, B, R2~R7, bits 1EH, 1FH Stack requirements: 6 bytes
RTOD: MOV R5,#6; The coefficient (180/π) is transferred to the second work area
MOV R6,#0E5H
MOV R7,#2FH
SJMP DR; Convert by multiplication

(32) Label: DTOR Function: Convert floating-point degrees to floating-point radians

Entry condition: The floating-point degrees are in [R0].
Exit information: The converted floating-point radians are still in [R0].
Affected resources: PSW, A, B, R2~R7, bits 1EH, 1FH Stack requirement: 6 bytes
DTOR: MOV R5,#0FBH; coefficient (π/180) is transferred to the second work area
MOV R6,#8EH
MOV R7,#0FAH
DR: LCALL MVR0; transfer [R0] to the first work area
CLR 1EH; coefficient is positive
LCALL MUL1; conversion by multiplication
LJMP MOV0; result is transferred to [R0]
END

Keywords:MCS-51 Reference address:MCS-51 MCU Practical Subroutine Library 7

Previous article:MCS-51 MCU Practical Subroutine Library 6
Next article:C51 MCU serial communication points

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号