ARM floating-point display

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
1
33%
5
2
67%
 
Total votes: 3

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

ARM floating-point display

Post by Sean »

Image

Floating point variables
The introduction of Flowcode for ARMs has enabled Flowcode programs to be targeted at a range of powerful 32-bit devices. The features available in the ARM compiler prompted the introduction of floating-point variables to Flowcode. This has enabled Flowcode for ARMs to support an extensive range of additional mathematical functions.

Flowcode for ARM floating-point variables, and the functions that operate on them, are actually defined as double precision (64-bit). The 64 bits of each variable are split between a mantissa (53-bit number) and an exponent (11-bit multiplier). These can handle decimal values with up to 309 digits on either side of the decimal point, and with a precision of up to16 digits:

(+/-)10 e (+/-)308

The stored floating-point representation of variables is useful for the binary arithmetic operations that are carried out by the processor, but is extremely difficult to convert into a format that can be read by humans. Luckily there are some functions that are available to perform this task for us.

The new update to Flowcode for ARMs contains some additional files that make the display or transmission of floating-point values possible.

The gcvt() function
The C function gcvt() can be used to convert a floating-point variable into text characters and to write them to a Flowcode string variable. The value can then be displayed on any of the LCDs using their PrintString macros, or transmitted to other devices, as individual characters, using any of the Flowcode communications components.

The gcvt() function requires three parameters:
• The C version of the name of the Flowcode floating-point variable to convert.
• The number of significant digits to convert.
• The C version of the name of the Flowcode string to write the results to.

The following line, placed in a Flowcode C block, will convert the Flowcode variable 'Float1' to the Flowcode string 'PrntBuff'.

gcvt(FCV_FLOAT1, 8, FCV_PRNTBUFF);

Both variables must be created in Flowcode before being used, and the string variable must be long enough to contain all the generated characters. Up to six formatting and termination characters can be generated, in addition to the significant digits, and should be allowed for in the string length.

The example command generates a string containing eight significant digits and can be displayed on an LCD using the PrintString macro from the LCDDisplay component.

The result string can represent the value in either normal or scientific notation. The format will be automatically selected to provide the most appropriate representation of the value within the number of characters available:

0.001256
3.4512819e-05

When gcvt() converts a negative number the leading '-' character is treated as one of the significant digits. Decimal points and the characters used for the exponent in scientific notation are not treated as significant digits and are an addition to the result string.

Examples of the strings generated by gcvt() with the number of significant digits set to 8:

1
-1.234
8.1234567
-8.123457
9.2820413e-08
-9.282041e-08

Example program
The example program linked to this article calculates the Sine value of angles between 0 and 360 degrees, at 5 degree intervals:

Radians = Degrees * Pi / 180
SinVal = sin(Radians)

Note: Trigonometric calculations are based in radians. Angles must be converted if degrees are the preferred units of measurement.
Note: The value of Pi is not known by the compiler and has been defined in a floating-point variable.

The results are displayed on an LCD attached to Port A and, optionally, transmitted via an RS232 E-Block (connected to Port C) to a PC running a terminal application.

The example program uses ECIOARM as its target. This can be easily changed to any of the alternative ARM devices.
ECIOTrigPrint.fcf_arm
(8 KiB) Downloaded 481 times
Precision and rounding
Calculations using floating-point values can sometimes introduce small errors that would be rounded to the nearest whole number when using integers.
The calculation of sin(360) should produce the result 0, but in this case produces the result 0.0000000928. The gcvt() function is forced to use scientific notation to display this value within the number of characters available and generates the string: 9.2820413e-08.
For the strings transmitted via RS232, conversion to scientific notation is prevented in by using the floating-point rounding function, fround(), to limit values to 6 decimal places before they are passed to gcvt():

SinVal = fround(SinVal,6)

Post Reply