Close

Debouncing

A project log for AVeRCADE

Customisable USB adapter for arcade controls.

danjovicdanjovic 03/25/2017 at 19:260 Comments
Added timed sample of buttons (16ms) as a countermeasure for contact bouncing.Used Timer 0 to overflow at each 16ms
	TCCR0 |= (1 << CS00) | (1<<CS02); // timer 0 prescaler 1024, overflow at FOSC/1024/256

Then wait for Timer 0 overflow before sampling the buttons

// Sample controllers each 16ms for 16MHz crystal (22ms for 12MHz)
if (TIFR & (1<<TOV0)) {
	Get_digital_controller_data();
	TIFR |= (1<<TOV0); // reset overflow flag		
}

This prevents bouncing at later part of the code which sends a packed each time it detects any change in the controllers.
...
	else
	{// or if data has changed 
		if (memcmp(&gamepad_report_1, &gamepad_report_1_old, sizeof(gamepad_report_t)) != 0)
		{
			to_send = 1;
		}
	}
...

Discussions