Search code examples
cavr

DistinguishingFalling and rising Edges in XMEGA


I've got 3 buttons that should generate interrupts each time they've been pressed or released, the issue that I have is, that I need to distuiguish what really happens ( realsing or pressing the button ) , here is my code :

..................

 ISR(PORTA_INT0_vect){
    if(!(PORTA.IN & PIN1_bm)){  // Green LED  pressed
        printf(" Green button pressed\n");

    }

     if(!(PORTA.IN & PIN3_bm)){ // Blue LED pressed 
        printf(" Blue button pressed\n");

    }

    if (!(PORTA.IN & PIN5_bm)){ // Red LED
        printf(" Red button pressed  \n");

    }
}

 ISR(PORTA_INT1_vect){
  printf(" I'm in \n");
    if((PORTA.IN & PIN1_bm)){// Green LED  released
        printf("Green button  released \n");
    }
    else if((PORTA.IN & PIN3_bm)){// Blue  LED  released
        printf("Blue button released \n");
    }
    else if((PORTA.IN & PIN5_bm)){// Red LED  released
        printf("Red button released \n");
    }


 }

void buttonINT(){
    // PORTA 
    PORTA.PIN1CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_FALLING_gc ;
    PORTA.PIN3CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_FALLING_gc ;
    PORTA.PIN5CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_FALLING_gc ;
    PORTA.INT0MASK = PIN1_bm | PIN3_bm | PIN5_bm;
    PORTA.INTCTRL = PORT_INT0LVL0_bm;
    PMIC.CTRL |= PMIC_LOLVLEN_bm;
    sei();
    }
void buttonINT2(){
    // PORTA 
    PORTA.PIN1CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_RISING_gc ;
    PORTA.PIN3CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_RISING_gc ;
    PORTA.PIN5CTRL = PORT_OPC_PULLUP_gc|  PORT_ISC_RISING_gc ;
    PORTA.INT1MASK = PIN1_bm | PIN3_bm | PIN5_bm;
    PORTA.INTCTRL = PORT_INT1LVL1_bm;
    PMIC.CTRL |= PMIC_LOLVLEN_bm;
    sei();
    }

the problem that I get is the program doesn't generate an interrupt at all by rising edges ?

any idea what I'm doing wrong here ?


Solution

  • I recommend to use a timer generated interrupt to poll the keys every -say- 10 ms. Here you can

    • debounce the keys (i.e. accept a key change only if you have the same reading for 2/3/4 times) ... something nowadays often seems to be forgotten
    • check for rising and falling edges
    • raise a "key-status-change" flag that can be processed & cleared from a main()
    • bonus: check for "long keypresses"
    • bonus: check for "doubleclicks"