Hey,
ik heb een keypadje met 9 toetsen waarvan elke toets aan een input hangt (via pull-up). Nu wil ik detecteren of er kort gedrukt of lang gedrukt is met m'n PIC. Hoe kan ik dit doen? Ruikt naar timers..
Hier is de code die ik al heb:
//******************************* KEYPAD SCAN ********************************//
//B9,B8,B7,B6,B5,B4,B3,B2,B1
if ( ENABLE_KEYPAD_SCAN )
{
Key = Key + (1 * !input (BUTTON_1) ); // Button 1 = 0 + 1 * 1
delay_us (75);
Key = Key + (2 * !input (BUTTON_2) ); // Button 2
delay_us (75);
Key = Key + (4 * !input (BUTTON_3) ); // Button 3
delay_us (75);
Key = Key + (8 * !input (BUTTON_4) ); // Button 4
delay_us (75);
Key = Key + (16 * !input (BUTTON_5) ); // Button 5
delay_us (75);
Key = Key + (32 * !input (BUTTON_6) ); // Button 6
delay_us (75);
temp_button = !input (BUTTON_7); // Button 7
Key = Key + (64 * temp_button);
delay_us (75);
temp_button = !input (BUTTON_8); // Button 8
Key = Key + (128 * temp_button);
delay_us (75);
temp_button = !input (BUTTON_9); // Button 9
Key = Key + (256 * temp_button);
delay_us (75);
if ( Key != 0 && // Alles losgelaten
input (BUTTON_1) && input (BUTTON_2) && input (BUTTON_3) && input (BUTTON_4) &&
input (BUTTON_5) && input (BUTTON_6) && input (BUTTON_7) && input (BUTTON_8) &&
input (BUTTON_9))
{
Key = 0; // Reset Keycode
Send_Command_String_RS232 ( Key , 2 , make8 (key,1) , make8 (key,0) , 0 );
Keypad_Debounce_Counter_2 = 2; // Start 2 * 20ms = 40 mS debouncing
ENABLE_KEYPAD_SCAN = FALSE; // Niet meer matrix lezen
}
}
Peter Claus
It is so simple to be happy, but it is so difficult to be simple
Met interupts op een timer. Ofwel een lus die doorlopen wordt bij indrukken van een toets, en een teller die optelt in die lus, en bij loslaten de lus weer verlaten.