转载地址:https://blog.csdn.net/ydogg/article/details/78971835
STM8L的中断,有两种模式,分别为Pin模式和Port模式,也就是所谓线中断和端口中断。
简而言之,线中断是指所有寄存器组的同一管脚Pin的中断都使用同一中断号,
端口中断是指同一个或多个寄存器组的所有管径Pin的中断使用同一中断号。
举例而言,
所有Port A/B/C/D/E的Pin1的中断都是EXTI1,
Port B 和Port G的所有Pin的中断都是EXTIB/G
实现方面,因为关联寄存器很多,直接操作比较太繁琐,容易出错。
比较好的选择是利用ST的库函数,代码清晰,易于维护,可以参照以下步骤.
简而言之,线中断比较简单,需要依次调用:
1. GPIO_Init() 端口初始化、上拉输入、浮空输入皆可
2. EXTI_SetPinSensitivity() 触发方式设定
3. enableInterrupts() 开中断
端口中断复杂一些,需要依次调用(缺一不可):
1. GPIO_Init() 端口初始化、上拉输入、浮空输入皆可
2. EXTI_SetPortSensitivity() 触发方式设定
3. EXTI_SelectPort() 选择触发端口
4. EXTI_SetHalfPortSelection() 选择端口高字节或低字节
3. enableInterrupts() 开中断
此外,设定中断优先级,两者皆用ITC_SetSoftwarePriority()函数,
在中断处理中,中断状态的清理也都使用EXTI_ClearITPendingBit()函数。
需要注意的是,
两种中断模式下,函数名类似,但参数类型定义并不一致,容易混淆,需要注意检查
比如:
EXTI_SetPinSensitivity的参数为EXTI_Pin_TypeDef,如EXTI_Pin_7
EXTI_SetPortSensitivity的参数为EXTI_Port, 如EXTI_Port_G
ITC_SetSoftwarePriority()和EXTI_ClearITPendingBit()函数中
如果是端口中断模式,要使用EXTIB_G_IRQn等代表端口的参数,而非GPIO的管脚号
根据MCU型号不同,不是所有的GPIO都能同时支持线中断和端口中断
比如STM8L052R8,其PG管脚只支持端口中断,使用前一定要查看手册。以下摘自STM8L手册:
● EXTIB/G - 8 lines on Port B or G: PB[7:0] or PG[7:0]
● EXTID/H - 8 lines on Port D or H: PD[7:0] or PH[7:0]
● EXTIE/F - 8 lines on Port E or F: PE[7:0] or PF[7:0]
● EXTI0 - 6 lines on Port A/B/C/D/E/F, bit 0: Px[0]
● EXTI1 - 5 lines on Port A/B/C/D/E, bit 1: Px[1]
● EXTI2 - 5 lines on Port A/B/C/D/E, bit 2: Px[2]
● EXTI3 - 5 lines on Port A/B/C/D/E, bit 3: Px[3]
● EXTI4 - 5 lines on Port A/B/C/D/E, bit 4: Px[4]
● EXTI5 - 5 lines on Port A/B/C/D/E, bit 5: Px[5]
● EXTI6 - 5 lines on Port A/B/C/D/E, bit 6: Px[6]
● EXTI7 - 5 lines on Port A/B/C/D/E, bit 7: Px[7]
线中断初始化:
GPIO_DeInit(GPIOC); //PC0
GPIO_Init(GPIOC, GPIO_Pin_0, GPIO_Mode_In_PU_IT); // 上拉中断输入
EXTI_SetPinSensitivity(EXTI_Pin_0, EXTI_Trigger_Falling); // 下降沿触发
ITC_SetSoftwarePriority(EXTI0_IRQn, ITC_PriorityLevel_2); // 软件优先级2
线中断的中断处理:
INTERRUPT_HANDLER(EXTI0_IRQHandler, 8)
{
EXTI_ClearITPendingBit(EXTI_IT_Pin0); // 清中断
val = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);
if( val == RESET ) {
// 你的处理
}
}
端口中断的初始化:
// GPIO初始化 (PG5)
GPIO_DeInit(GPIOG);
GPIO_Init(GPIOG, GPIO_Pin_5, GPIO_Mode_In_PU_IT); // 上拉输入带中断
disableInterrupts();
EXTI_DeInit();
EXTI_SetPortSensitivity(EXTI_Port_G, EXTI_Trigger_Rising); // 上升沿触发
EXTI_SelectPort(EXTI_Port_G); // PG端口选择
EXTI_SetHalfPortSelection(EXTI_HalfPort_G_MSB ,ENABLE); // 允许PG的[4:7]pin触发
EXTI_SetHalfPortSelection(EXTI_HalfPort_G_LSB ,DISABLE); // 禁止PG的[0:3]pin触发
ITC_SetSoftwarePriority(EXTIB_G_IRQn, ITC_PriorityLevel_1);// 设定优先级
enableInterrupts(); // 开中断
端口中断的中断处理:
INTERRUPT_HANDLER(EXTIB_G_IRQHandler, 6)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
// 清PG中断状态
EXTI_ClearITPendingBit(EXTI_IT_PortG);
val = GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_5);
if( val ) {
count++; // 你的处理
}
}
最后,ST官方的代码注释是最好的说明:
To use a GPIO pin as an interrupt source, follow theses steps:
1. Configure the GPIO pin in input mode with interrupt enabled using
GPIO_Init()
2. Configure the GPIO pin sensitivity (falling, rising...) using
EXTI_SetPinSensitivity()
3. Enable global interrupts using enableInterrupts()
4. In the IRQ handler corresponding to the GPIO pin, clear the interrupt
pending bit using EXTI_ClearITPendingBit()
To use a GPIO port as an interrupt source, follows theses steps:
1. Configure the GPIO pins of the same port in input mode with interrupt
enabled using GPIO_Init()
2. Configure the GPIO port sensitivity (falling, rising...) using
EXTI_SetPortSensitivity()
3. Select the GPIO port and the corresponding half port using
EXTI_SelectPort() then EXTI_SetHalfPortSelection()
4. Enable global interrupts using enableInterrupts()
5. In the IRQ handler corresponding to the GPIO port, clear the interrupt
pending bit using EXTI_ClearITPendingBit()
---------------------
作者:ydogg
来源:CSDN
原文:https://blog.csdn.net/ydogg/article/details/78971835
版权声明:本文为博主原创文章,转载请附上博文链接!