Hi!
On this weekend I have decided to play wih debouncing software. I have chosen Danni`s code.
I took a paper and I started to work things out.
After spending few hours on the code this sofware is more or less clear but I have some questions about it :
Below pasting it once again:
I is changed a bit for newer interrupts vectors and I add some comments.
/************************************************************************ ;* * ;* Testing Read and debounce up to 8 keys * ;* Bulletproof: 4 equal samples needed * ;* * ;* Author: P. Dannegger * ;* danni@specs.de * ;* * ;***********************************************************************/ #include#include #define KEY_INPUT PINC //(PINC>>3)|(PIND>>4))&(0x03) //How can I set for debouncing PINC3 or PIND5 ??? //As far as I am concern When I am using only PINC I can below choose apropriate mask #define LED_OUTPUT PORTB #define LED_DIR DDRB char key_state; // debounced and inverted key state: // bit = 1: key pressed char key_press; // key press detect ISR(TIMER0_OVF_vect) { static char ct0, ct1; char i; i = key_state ^ ~KEY_INPUT; // key changed ? ct0 = ~( ct0 & i ); // reset or count ct0 ct1 = ct0 ^ (ct1 & i); // reset or count ct1 i &= ct0 & ct1; // count until roll over ? key_state ^= i; // then toggle debounced state // now debouncing finished key_press |= key_state & i; // 0->1: key press detect } char get_key_press( char key_mask ) { cli(); key_mask &= key_press; // read key(s) key_press ^= key_mask; // clear key(s) sei(); return key_mask; } int main( void ) { key_state = 0; key_press = 0; TCCR0 = 1<<CS01; //divide by 256 * 256 //changed value using 1000000 clock was TCCR0 = 1<<CS02; TIMSK = 1<<TOIE0; //enable timer interrupt LED_DIR = 0xFF; LED_OUTPUT = 0xFF; sei(); for(;;) { // main loop LED_OUTPUT^=get_key_press( 0xFF ); // toggle LEDs on key press } }
Questions:
1) How can I provide PINC3 and PIND5 to the software debouncer. I have tried my way but it is not working as I thought :/. All I want to do is provide pin states from different ports
2) What do you think about prescaler to the timer0??
When I am using 1000 000 clock I think by 256 is to much.
3) How would you make counter based on this code .
For example when we push one buttom it increases its value and when we press second switch it decrease its value.
Thanks in advance
Adam