PIC microcontroller floating point number and its conversion to decimal number

Publisher:ShimmeringMoonLatest update time:2011-02-24 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Abstract This paper focuses on the format of floating point numbers, the conversion between decimal numbers and floating points, and program design.

In the instrument we designed, we used PIC series microcontrollers and encountered the problem of floating point operation. After consulting its relevant information, we found that the format of its floating point numbers and the conversion between it and decimal numbers are completely different from the three-byte and four-byte floating point numbers provided by our commonly used MCS-51 microcontrollers. This paper will explain the format of its floating point numbers and the conversion between it and decimal numbers and the program design steps.

1 Floating point format

The floating point format used by Microchip's microcontrollers is a variant of the IEEE-754 standard. The format of a 32-bit floating point number is:



Where: × represents a binary number 0 or 1; eb is the deviation of the exponent; S is the sign bit of the floating point number, S=0 is a positive number, S=1 is a negative number; the decimal point "·" is on the right of the sign bit S; BY0 BY1 BY2 is the decimal part of the mantissa.

Special attention should be paid to:

⑴ The floating point number implies that its integer part is 1.
⑵ The floating point number of the decimal number 0 is represented as 00H, 00H, 00H, 00H.

2 Conversion between floating point numbers and decimal numbers

2.1 Conversion of decimal numbers to floating point numbers

Let: the decimal number is A, then 2Z=A, Z= lnA/ln2, exponent P=int(z); mantissa part X: X=A/2P, its integer part is implicitly 1 (except zero), and its decimal part is formatted as a binary number according to the original code, that is, the decimal part of the mantissa BY0 BY1 BY2. The exponent deviation eb=P+7FH (where 7FH is the exponent offset). The sign bit S is determined by the positive or negative of the decimal number.

For example, the decimal number 50.265 is converted to a 32-bit normalized floating point number: A=50.265, then Z=ln50.265/ln2, P=int(Z), so P=5; X=A/2P=50.265/25=1.57078125, 0.57078125 is converted to a 23-bit binary decimal number, which is BY0 BY1 BY2, and the decimal sign bit S is added to the highest bit (because the decimal number is a positive number, so S=0); and eb=P+7FH, so the 32-bit normalized floating point number of the decimal number 50.265 is 84H, 49H, 0FH, 5CH.

2.2 Converting floating point numbers to decimal numbers

Let the floating point number be eb S.BY0 BY1 BY2. Since the integer of the implied mantissa of the floating point number is 1, the actual value of the mantissa X is:

BY0 BY1 BY2; exponent P = eb-7FH; therefore: decimal number

A = (-1) S × 2P × X

Example: 32-bit normalized floating point number 84H, 49H, 0FH, 5CH is converted to a decimal number.

The sign bit S = 0; exponent P = 84H-7FH, so P = 5; the decimal part of the mantissa is 49H, 0FH, 5CH shifted left by one bit, and the integer part of the mantissa is implicitly 1, so the actual value of the mantissa X is: 1.57078123; decimal number A = (-1) 0 × 25 × 1.57078123, that is, A = 50.265.

3 Program Design for Conversion between Floating-Point Numbers and Decimal Numbers

3.1 Program Design for Conversion between Floating-Point Numbers and Decimal Numbers

(1) Check whether the floating-point number is zero; if it is zero, the integer and fractional parts of the decimal number are both zero.

(2) Save the sign bit of the floating-point number, put the implicit 1 of the floating-point number in the sign bit of the floating-point number, add 1 to the exponent deviation eb, and move the decimal point before the sign bit of the original floating-point number.

(3) Determine whether the exponent deviation is greater than 7FH? If it is less than or equal to 7FH, the integer part of the base number is zero, and the mantissa of the floating-point number is shifted right n times (note: n=7FH-exponent deviation eb), that is, the binary fractional part is obtained. If it is greater than 7FH, the decimal point is shifted right n' times (note: n'=exponent deviation eb-7FH), that is, the binary integer and fractional parts are obtained.

(4) Convert the binary integer part to a decimal integer; convert the binary fractional part to a decimal fraction. At this point, the conversion from floating point to decimal is completed.

3.2 Program Design for Converting Decimal Numbers to Floating Point Numbers

(1) Check whether the decimal number is zero. If it is zero, the floating point number is set to 00H, 00H, 00H, 00H.

(2) Save the sign bit of the decimal number, convert the integer part of the decimal number to a binary integer, and convert the decimal part of the decimal number to a binary decimal (assuming that the binary integer is three bytes and the binary decimal is two bytes).

(3) Set the exponent deviation eb of the floating point number to 7FH+23, and check whether the highest bit of the binary integer is 1. If not, shift the binary integer and the binary decimal together to the left, shift left once, and reduce the exponent deviation by 1 until the highest bit of the binary integer is 1; the 1 of the mantissa integer is implied, and the highest bit of the binary integer is changed to the sign bit of the number. At this point, the contents of the exponent deviation eb unit and the three units of the original binary integer constitute a four-byte floating point number.

Finally, it should be pointed out that the 32-bit floating-point number described in this article has an accuracy equivalent to 7 decimal digits; the 32-bit floating-point number operation program is available in the relevant information provided by Microchip. However, when debugging its floating-point subroutine, it was found that the result of 0 minus 0 was 00H, 80H, 00H, 00H, rather than 00H, 00H, 00H, 00H. This should be noted when programming.

4 Conclusion

The program design for converting between decimal numbers and floating-point numbers in this article has practical value in the keyboard setting and data display of intelligent instruments. Here, a design idea is proposed. There is no complex algorithm, the program design is relatively simple, and the workload is relatively small. The ready-made subroutines provided by Microchip can be used to realize the conversion between decimal integers and binary integers, and the conversion between decimal decimals and binary decimals.

References

1 Application Notes in Microchip CD-ROM, 1997 (2)
2 Microcontroller Software Design Technology. Science and Technology Literature Press, 1988.7
Reference address:PIC microcontroller floating point number and its conversion to decimal number

Previous article:Software and hardware implementation of serial communication between PIC16 series microcontroller and PC
Next article:DS18B20 and PIC microcontroller communication source program

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号