Debounce direction as well as step

This commit is contained in:
Michael Moon 2010-01-15 13:39:00 +11:00
parent 4463e2b945
commit 1af9eacefb
1 changed files with 25 additions and 12 deletions

View File

@ -156,6 +156,7 @@ int main (void)
uint16_t spinner = 0; uint16_t spinner = 0;
int r; int r;
int32_t rv; int32_t rv;
uint8_t input_lastreading;
// setup inputs // setup inputs
DDR_STEP &= ~MASK(PIN_STEP); DDR_STEP &= ~MASK(PIN_STEP);
@ -210,21 +211,33 @@ int main (void)
for (;;) for (;;)
{ {
// check logic inputs // check logic inputs
if ((READ_STEP & MASK(PIN_STEP)) == 0) { if (1) {
if (stepdebounce >= 128) { uint8_t input_thisreading = (READ_STEP & (MASK(PIN_STEP) | MASK(PIN_DIR)));
if (stepdebounce == 128) { // if same as last time
if (READ_DIR & MASK(PIN_DIR)) if (input_thisreading == input_lastreading) {
// if we're near debounce threshold
if (stepdebounce >= 32) {
// if we're exactly on the threshold
if (stepdebounce == 32) {
// adjust target position
if (input_thisreading & MASK(PIN_DIR))
npos++; npos++;
else else
npos--; npos--;
// go over threshold so inputs must change before next move
stepdebounce++; stepdebounce++;
} }
} }
else // approach threshold
stepdebounce++; else {
stepdebounce++
} }
else }
// if input changed, reset debouncer
else {
stepdebounce = 0; stepdebounce = 0;
}
}
// check serial input // check serial input
if (serial_rxchars()) { if (serial_rxchars()) {