Gast
#1481989
Hallo zusammen, ich mache gerade meine ersten Gehversuche in C und AVR und habe im Internet ein Beispielprogramm zur Tasterabfrage gefunden welches ich abwandeln wollte. Im Beispiel sollte immer nur eine Leuchte leuchten (if (keys &2,4,8,0x10,0x20usw) Ich möchte gerne zusätzlich dass beim gleichzeitigen Tastendrücken mehrere Leds leuchten Taster 1+2 zusammen: LED1und2 Taster 3+1: Led3+1 Taster 3+2+1: Led3+2+1 und so weiter. Ich schaffe es jedoch nicht dass mehrere Leds gleichzeitig leuchten. Meiner Meinung nach bedeutet if ( keys & 3 ) led = 3; im untenstehenden Beispiel dass wenn Taster 1 +2 gleichzeitig gedrückt ist, LED 1+2 leuchten müssen. Was mache ich falsch? Gruß Holger /*********************************************************************** ******* Title: Turn on LED when switch is pressed Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury Date: December 2002 Software: AVR-GCC 3.3 Hardware: AT90S8515 at 4 Mhz, STK200 compatible starter kit Description: This example demonstrates basic port input/output technic. If one of the switches on port B is pressed, the corresponding LED on port C is turned on. ************************************************************************ *******/ #include <inttypes.h> #include <avr/io.h> int main(void) { uint8_t led; uint8_t keys; DDRC = 0xff; // use all pins on PortC for output DDRB = 0x00; // use all pins on port B for input PORTB = 0xff; // activate internal pull-up PORTC = 0x00; // set output high -> turn all LEDs off for (;;) { // loop forever keys = ~PINB; // read input port with switches (active-low) if ( keys & 1 ) led = 1; else if ( keys & 2 ) led = 2; else if ( ( keys & 0x03 ) == 0x03 ) led = 3; else if ( keys & 4 ) led = 4; else if ( keys & 5 ) led = 5; else if ( keys & 6 ) led = 6; else if ( keys & 7 ) led = 7; else led = 0; if ( keys ) PORTC = ~led; // Set corresponding LED if switch pressed } }