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
Previous article:Software and hardware implementation of serial communication between PIC16 series microcontroller and PC
Next article:DS18B20 and PIC microcontroller communication source program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Analysis of the problem that the program cannot run after F28004x online debugging reset
- Is the STM32 library function HAL_UART_Receive blocking?
- 【DIY Creative LED V2】Complete program
- The Engineer's Way of Quanhui, the author of "FPGA Timing Constraints and Analysis"
- ALTERA cyclone V sockit development board for sale at low price and can be exchanged for E coins
- GD32L233C-START Review——04. Comparison between analog IIC and hardware IIC driving OLED
- What is the driving voltage in LCD segment code screen?
- [Smart Cup Holder] 04-Add hardware support for TouchGFX interface
- [RVB2601 Creative Application Development] RVB2601 Development Board - W800 Module Firmware Update Method
- Op amp positive and negative power supply problem