中断优先级
stm8的中断优先级不是固定不变的,stm8的中断分为硬件优先级与软件优先级;当多个中断发生时,cpu会先响应软件优先级高的中断,若软件优先级相同会先响应硬件优先级高的;
其中软件优先级有四个
/*** @brief ITC Priority Levels selection*/
typedef enum {ITC_PRIORITYLEVEL_0 = (uint8_t)0x02, /*!< Software priority level 0 (cannot be written) */ITC_PRIORITYLEVEL_1 = (uint8_t)0x01, /*!< Software priority level 1 */ITC_PRIORITYLEVEL_2 = (uint8_t)0x00, /*!< Software priority level 2 */ITC_PRIORITYLEVEL_3 = (uint8_t)0x03 /*!< Software priority level 3 */
} ITC_PriorityLevel_TypeDef;
例子:
ITC_SetSoftwarePriority(ITC_IRQ_PORTE,ITC_PRIORITYLEVEL_2 );
外部中断
stm8的所有io引脚都有外部中断能力;
IAR的中断处理声明
在固件库stm8s.h中可以找到这样一段代码
#ifdef _IAR_#define STRINGVECTOR(x) #x#define VECTOR_ID(x) STRINGVECTOR( vector = (x) )#define INTERRUPT_HANDLER( a, b ) \_Pragma( VECTOR_ID( (b)+2 ) ) \__interrupt void (a)( void )#define INTERRUPT_HANDLER_TRAP(a) \_Pragma( VECTOR_ID( 1 ) ) \__interrupt void (a) (void)
#endif /* _IAR_ */
例子:
INTERRUPT void EXTI_PORTE_IRQHandler(void);INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{if(KEY1==0){delay_ms(20);while(KEY1==0);LED1_TOGGLE;}if(KEY2==0){delay_ms(20);while(KEY2==0);LED2_TOGGLE; }
}
外部中断灵敏性(触发方式)
/*** @brief EXTI Sensitivity values for PORTA to PORTE*/
typedef enum {EXTI_SENSITIVITY_FALL_LOW = (uint8_t)0x00, /*!< Interrupt on Falling edge and Low level */EXTI_SENSITIVITY_RISE_ONLY = (uint8_t)0x01, /*!< Interrupt on Rising edge only */EXTI_SENSITIVITY_FALL_ONLY = (uint8_t)0x02, /*!< Interrupt on Falling edge only */EXTI_SENSITIVITY_RISE_FALL = (uint8_t)0x03 /*!< Interrupt on Rising and Falling edges */
} EXTI_Sensitivity_TypeDef;
Falling edge 是下降沿
Rising edge 是上升沿
使用例子:
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOE, EXTI_SENSITIVITY_FALL_ONLY);
实验程序
使用一个GPIO来作外部中断有这几个步骤
- GPIO_Init
- EXTI_SetExtIntSensitivity
- ITC_SetSoftwarePriority
- INTERRUPT_HANDLER 定义中断处理
- enableInterrupts
#include "stm8s_gpio.h"
#include "stm8s_itc.h"
#include "stm8s_exti.h"bool pinn = FALSE;
void delay(uint32_t delayTime);int main(void)
{disableInterrupts();GPIO_Init(GPIOA, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);GPIO_Init(GPIOE, GPIO_PIN_6, GPIO_MODE_IN_PU_IT);EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOE, EXTI_SENSITIVITY_FALL_ONLY);ITC_SetSoftwarePriority(ITC_IRQ_PORTE, ITC_PRIORITYLEVEL_3);if (pinn == TRUE){GPIO_WriteLow(GPIOA, GPIO_PIN_4);}else{GPIO_WriteHigh(GPIOA, GPIO_PIN_4);}enableInterrupts();while (1){}
}void delay(uint32_t delayTime)
{while (delayTime--);
}INTERRUPT void EXTI_PORTE_IRQHandler(void);INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{BitStatus status;status = GPIO_ReadInputPin(GPIOE, GPIO_PIN_6);if (status == RESET){pinn = !pinn;if (pinn == TRUE){GPIO_WriteLow(GPIOA, GPIO_PIN_4);}else{GPIO_WriteHigh(GPIOA, GPIO_PIN_4);}}
}