308/317
10 - Second Application: a Sailing Computer
The results of the computation are word values that must be converted into a string of charac-
ters. This is done by the following function which is a very classical number-to-string conver-
sion. The leftmost digit is processed separately: if the result is between 1000 and 1999, the 1
is displayed; if it exceeds 1999, an arrow is displayed to show that the value overflows the dis-
play capacity. Finally, the minus sign is shown if the number is negative.
In the process of number-to-string conversion, please note the use of the
div
function that
takes two values and produces at once both the quotient and the remainder in a structure.
This is a very efficient way to do it.
One might question why the standard C function
sprintf
is not used here. There are two rea-
sons: the first one is that this function requires a large amount of code, that would not fit the
available ROM. The other reason is that this function yields an ASCII string, which is not what
our display needs anyway.
void NumberToString ( int Value, char * S )
{
Byte i ;
div_t Step ;
Step.quot = abs ( Value )
;
/* convert absolute value */
for ( i = 3 ; i > 0 ; i-- )
{
Step = div ( Step.quot, 10 ) ;
/* determine next digit */
S[i] = Step.rem ;
/* this is the digit */
}
if ( Step.quot == 0 )
S[0] = 0 ;
else
{
/* Case of an overflow */
if ( Step.quot == 1 )
S[0] = 0x10 ;
/* supplementary digit is 1 : use left-
hand 1 */
else
S[0] = 0x04 ;
/* greater than 1 : put overflow sign (<-) */
}
if ( Value < 0 )
S[0] |= 0x08 ;
/* add minus sign if necessary */
}
The execution of the computations is a mere translation in C language of the mathematical for-
mulae, thanks to the use of floating point arithmetic. This needs no further explanation.
Содержание ST7 Series
Страница 1: ...ST7 8 BIT MCU FAMILY USER GUIDE JANUARY 1999 1 ...
Страница 238: ...238 317 8 C Language and the C Compiler 08 Burn bmp Then use the EPROMer programmer software as described in Chapter 7 ...
Страница 289: ...289 317 10 Second Application a Sailing Computer 10 befor Bs Rw Vw VMG AlphaR AlphaV Before the wind ...