我有3个按钮,它们在每次按下或释放时均会生成中断,而我的问题是,我需要区分实际发生的情况(实现或按下按钮),这是我的代码:
..................
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();
}
我得到的问题是程序根本不会通过上升沿生成中断吗?
知道我在做什么错吗?
最佳答案
我建议使用计时器生成的中断,每隔10毫秒轮询一次键。在这里你可以
取消按键的抖动(即,只有当您具有相同读数的2/3/4倍时才接受按键更改)...如今似乎经常被遗忘
检查上升沿和下降沿
引发可以从main()
处理和清除的“密钥状态更改”标志
奖金:检查“长按”
奖励:检查“ doubleclicks”