Na, dann will ich dich mal nicht im Regen stehen lassen.
Ich kann's nicht testen, weil ich die Hardware nicht besitze, aber das hier könnte klappen:
Code:// prg: check buttons for codes #define F_CPU 9000000UL #include <util/delay.h> #include <avr/io.h> typedef uint8_t byte; // button pins // PINB3 is pin of button 1 // PINB4 is pin of button 2 // 0 means end of buttons byte buttons[] = {PINB3, PINB4, 0}; // first 0 is for counting in function // 1,1,2,1 is code // last 0 is end of code byte code1[] = {0, 1, 2, 2, 1, 0}; byte code2[] = {0, 2, 1, 2, 2, 0}; byte code3[] = {0, 2, 1, 1, 2, 2, 0}; byte* codes[] = {code1, code2, code3, 0}; // prototypes byte isButtonPressed(byte pinNr); void resetInput(); byte checkButtons(byte* button, byte* codes[], byte buttons[]); // main int main (void) { DDRB = 0x06; // I/O ports PB1+PB2; PORTB = 0x18; // activate pull up (PB3+PB4) byte btn; // if a button is pressed this variable will know while(1) { byte code_nr = checkButtons(&btn, codes, buttons); // codes switch LEDs if (code_nr == 1) { PORTB |= (1 << PINB2); // PB2 on PORTB &= ~(1 << PINB1); // PB1 off } else if (code_nr == 2) { PORTB |= (1<<PINB1); // PB1 on PORTB &= ~(1<<PINB2); // PB2 off } else if (code_nr == 3) { PORTB &= ~(1<<PINB1); // PB1 off PORTB &= ~(1<<PINB2); // PB2 off } } } // functions byte isButtonPressed(byte pinNr) { byte pin = PINB; return (pin & (1 << pinNr)) || (pin & (1 << pinNr)) || (pin & (1 << pinNr)); } void resetInput() { byte i = 0; while(codes[i]) { *codes[i++] = 0; } } byte checkButtons(byte* button, byte* codes[], byte buttons[]) { static byte last_pressed_button = 0; if (last_pressed_button) { // was button pressed last time? if(isButtonPressed(buttons[last_pressed_button-1])) // is button still pressed? return 0; // -> return else { last_pressed_button = 0; // else: not pressed anymore -> go on } } *button = 0; byte button_iterator = 0; // iterate over all buttons byte code_iterator = 0; // iterate over all codes while(buttons[button_iterator]) { if(isButtonPressed(buttons[button_iterator])) { // check buttons one after another *button = button_iterator + 1; // write pressed button number to external variable pressed_btn last_pressed_button = *button; // store pressed button number while(codes[code_iterator]) { // check all codes one after another byte* code_pos_ptr = codes[code_iterator]; // first byte of codes array is for current position if(codes[code_iterator][(*code_pos_ptr)+1] == button_iterator + 1) { // is the pressed button nr equal to next button nr in code? (*code_pos_ptr)++; // -> next position if(!codes[code_iterator][(*code_pos_ptr)+1]) { // code veryfied resetInput(); // reset all counters return code_iterator + 1; // returns number of detected code } } else { *code_pos_ptr = 0; } code_iterator++; } code_iterator = 0; // prepare for next button check } button_iterator++; } return 0; // 0 means no code veryfied }






Zitieren


Lesezeichen