305/317
10 - Second Application: a Sailing Computer
OldState = NewState ;
/* Remember for next time */
}
The button number is determined by checking that the voltage is within a certain range. The
debouncing consists of checking that the voltage remains in the same range for a certain
number (here 3) of consecutive conversions. If the voltage falls within another range, the
Count
variable is reset and the process restarts.
10.5.4 Reading and filtering the wind direction
The weather vane is attached to the shaft of a potentiometer that can turn indefinitely. It pro-
duces a DC voltage that is a fraction of the supply voltage, thus proportional to the angle. By
connecting the wiper to PD1, the Analog to Digital Converter will give a value between 0 and
255. This value corresponds to the wind direction but with the following differences:
The resolution is only 256 points, while the reading is expected to have a one-degree
resolution.
Because the potentiometer is installed without any precise alignment, the value represents
the angle shifted by a certain number of points.
To correct the shift, a simple addition of a constant, then a truncation modulo 256 is needed.
To increase the resolution, it is necessary to take the mean of a certain number of successive
readings. This also has the advantage of filtering the signal, thus damping the oscillations that
the weather vane undergoes because of the pitch and roll. This is performed by building an
array of the successive values of the direction, and shifting these values each time a new
value is read; the oldest value is lost, and replaced by the newest one. Then taking the mean
of all these values yields the required filtering as well as the resolution increase, if we divide
the sum of the value by half the number of values to be averaged: the result stands now be-
tween 0 and 511, which allows us to display the direction to the degree. This operation implies
periodic readings; they are performed once every two interrupts (i.e. 50 times per second) by
the display refresh routine, avoiding using a timer for this purpose.
The code is the following:
The Analog to Digital Converter is initialized with the proper channel number, starting the con-
version. A loop waits until the conversion is complete, and the value is read, with the addition
of the
VaneAdjust
variable that corrects the alignment error. Then the converter is configured
back for the channel that corresponds to the push-buttons.
void ReadDirection ( void )
{
Byte i, a ;
int Angle ;
Содержание 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 ...