304/317
10 - Second Application: a Sailing Computer
The
Polarity
variable is also toggled here, so that the drive voltage will have a frequency of
50 Hz since the function is called 100 times per second.
10.5.3 Polling the push-buttons
The push-buttons are wired to a resistor arrangement that produces different voltages at one
of the inputs of the Analog to Digital Converter. A different voltage is also provided when no
button is pressed. The following function reads the voltage input, with the required de-
bouncing, both to avoid noise making it appear that a button is pressed, and also to be sure
the voltage is stable and not en route to a final value when we sampled it.
The result is written into a global variable
ButtonState
that is zero when no button is pressed,
otherwise 1, 2 or 3 depending on the button pressed. This variable is used by the main pro-
gram, which puts it back to zero. The debouncing mechanism is such that, to produce the
same value again, or another value, the user must first release the button, then press it again,
or press another. There is no auto repeat function, unlike on a computer keyboard.
void GetPushButtons ( void )
{
static Byte OldState, Count = 0 ;
Byte NewState, a ;
a = ADCDR ;
/* Get last conversion */
if ( a < 80 )
/* about 1.57 V */
NewState = 1 ;
/* Key 1 is pressed */
else
if ( a < 120 )
/* about 2.35 V */
NewState = 2 ;
/* Key 2 is pressed */
else
if ( a < 150 )
/* about 2.94 V */
NewState = 3 ;
/* Key 3 is pressed */
else
NewState = 0 ;
/* No key */
if ( NewState == OldState )
{
Count++ ;
if ( Count == DEBOUNCE_TIME )
{
ButtonState = NewState ;
/* Signal that a key is pressed. */
if ( Count > DEBOUNCE_TIME )
Count = DEBOUNCE_TIME ;
/* As long as button pressed,
do nothing */
}
}
else
Count = 0 ;
/* Unstable state : wait. */
Содержание 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 ...