问题
例如,配置PA1和PB1同时为外部中断模式,并且模式都是上升下降沿触发,上升下降沿又分别对应不同的功能,但是硬件中断只有EXTI1,如何在EXTI1中区分是PA1还是PB1引起的中断?
例如我要设置所有的端口都为外部中断,需要分别判断中断响应是哪个引脚的那种边沿触发的?
方法思考
判断引脚电平的方式不可取,改硬件?不可能,绝对不可能!还有其它引脚需要用其它的gpio口中断呢,我的一个项目中需要用到N个端口,这里的PA1/PB1只是举个例子。有没有通解?
1.配置PA1和PB1同时为外部中断模式,模式都是上升下降沿触发;
2.硬件中断事件有了,接下来考虑怎么判断;
3.全局变量记录中断事件发生前的引脚状态,再与中断发生后的状态比较,由此判断到底是哪个引脚发生的何种类型的中断。
实现
全局变量定义
u8 PA1_last_state=1;//set as gpio state
引脚状态判断函数
//function : get gpio edge
//input :NA
//return:-1:falling edge ; 0:no change; 1:rising edge
int get_PA1_edge_state(void)
{
if(PAin(1)){ //gpio now state is high
if(PA1_last_state){ //last state is high
PA1_last_state=PAin(1); //refresh last state
return 0; //return no change
}else{ //last state is low
PA1_last_state=PAin(1); //refresh last state
return 1; //return rising edge
}
}else{ //gpio now state is low
if(PA1_last_state){ //last state is high
PA1_last_state=PAin(1); //refresh last state
return -1; //return falling edge
}else{ //last state is low
PA1_last_state=PAin(1); //refresh last state
return 0; //return no change
}
}
}
中断响应函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
int edge_state_temp;
if(GPIO_Pin == GPIO_PIN_1) //judge EXTI is gpio 1
{
edge_state_temp=get_PA1_edge_state(); //get PA1 edge state
if(edge_state_temp==1) //is PA1 rising edge
{
//todo
}
if(edge_state_temp==-1) //is PA1 falling edge
{
//todo
}
//...get other EXTI state to deal...
}
}