stvd+stm8l051F3(一): 外部中断
stvd: ST Visual Develop Version 4.3.9
ic : stm8l051F3
1、原理图
按键接在stm8l051F3的PB1脚,LED接在 stm8l051F3的PD0脚
2、建立stvd key项目
打开stvd新建 stm8l051_key项目,并将stm8l15x.h,stm8l15x_it.h,stm8l15x_it.c,stm8l15x_conf.h,stm8l15x_exti.c,stm8l15x_gpio.c文件加入项目;
stm8l15x_gpio.c:io相关源文件;
stm8l15x_exti.c:中断相关源文件;
3、修改代码
main.c:
stm8l15x.h:
打开芯片型号宏定义
stm8l15x_conf.h:
关闭 USE_FULL_ASSERT宏
stm8l15x_it.c:
修改中断向量9响应函数 INTERRUPT_HANDLER(EXTI1_IRQHandler,9) ,在该函数中添加自定义中断处理函数
stm8_interrupt_vector.c:
注释 @far @interrupt void NonHandledInterrupt (void)函数,添加 #include "stm8l15x_it.h" ,解决重定义拨错问题,
将 _vectab中irq9的中断处理函数由 NonHandledInterrupt修改为 EXTI1_IRQHandler
编译项目通过
4、测试
设置断点单步调试,按下按键led点亮,再按led熄灭。
stvd: ST Visual Develop Version 4.3.9
ic : stm8l051F3
1、原理图
按键接在stm8l051F3的PB1脚,LED接在 stm8l051F3的PD0脚
2、建立stvd key项目
打开stvd新建 stm8l051_key项目,并将stm8l15x.h,stm8l15x_it.h,stm8l15x_it.c,stm8l15x_conf.h,stm8l15x_exti.c,stm8l15x_gpio.c文件加入项目;
stm8l15x_gpio.c:io相关源文件;
stm8l15x_exti.c:中断相关源文件;
3、修改代码
main.c:
- /* MAIN.C file
- *
- * Copyright (c) 2002-2005 STMicroelectronics
- */
- #include "stm8l15x.h"
-
- //定义LED、按键端口
- #define LED_PORT GPIOD
- #define LED_PINS GPIO_Pin_0
- #define KEY_PORT GPIOB
- #define KEY_PINS GPIO_Pin_1
- /*******************************************************************************
- ****入口参数:无
- ****出口参数:无
- ****函数备注:不精确延时函数
- *******************************************************************************/
- void Delay(__IO uint16_t nCount)
- {
- /* Decrement nCount value */
- while (nCount != 0)
- {
- nCount--;
- }
- }
- /*******************************************************************************
- ****函数说明:主函数
- ****入口参数:无
- ****出口参数:无
- ****函数备注:按键按下后在外部中断1中将LED亮灭变换
- ********************************************************************************/
- void main(void)
- {
- GPIO_Init(LED_PORT,LED_PINS,GPIO_Mode_Out_PP_Low_Slow);//初始化LED端口
- GPIO_Init(KEY_PORT, KEY_PINS, GPIO_Mode_In_PU_IT);//初始化按键,上拉输入,带中断
-
-
- EXTI_DeInit (); //恢复中断的所有设置
- EXTI_SetPinSensitivity (EXTI_Pin_1,EXTI_Trigger_Falling);//外部中断1,下降沿触发,向量号9
- enableInterrupts();//使能中断
-
- while (1)//等待中断
- {
-
- }
- }
打开芯片型号宏定义
- /* Uncomment the line below according to the target STM8L15x device used in your
- application
- */
- /* #define STM8L15X_LD */ /*!< STM8L15X_LD: STM8L15x Low density devices */
- /* #define STM8L15X_MD */ /*!< STM8L15X_MD: STM8L15x Medium density devices */
- /* #define STM8L15X_MDP */ /*!< STM8L15X_MDP: STM8L15x Medium density plus devices */
- /* #define STM8L15X_HD */ /*!< STM8L15X_HD: STM8L15x/16x High density devices */
- #define STM8L05X_LD_VL */ /*!< STM8L05X_LD_VL: STM8L051xx3 Low density value line devices */
- /* #define STM8L05X_MD_VL */ /*!< STM8L05X_MD_VL: STM8L052xx6 Medium density value line devices */
- /* #define STM8L05X_HD_VL */ /*!< STM8L05X_HD_VL: STM8L052xx8 High density value line devices */
- /* #define STM8AL31_L_MD */ /*!< STM8AL31_L_MD: STM8AL3x Medium density devices */
关闭 USE_FULL_ASSERT宏
- /* Exported types ------------------------------------------------------------*/
- /* Exported constants --------------------------------------------------------*/
- /* Uncomment the line below to expanse the "assert_param" macro in the
- Standard Peripheral Library drivers code */
- /* #define USE_FULL_ASSERT (1) */
-
- /* Exported macro ------------------------------------------------------------*/
修改中断向量9响应函数 INTERRUPT_HANDLER(EXTI1_IRQHandler,9) ,在该函数中添加自定义中断处理函数
- INTERRUPT_HANDLER(EXTI1_IRQHandler,9)
- {
- /* In order to detect unexpected events during development,
- it is recommended to set a breakpoint on the following instruction.
- */
-
- EXTI_ClearITPendingBit (EXTI_IT_Pin1);//清除中断标志
- GPIO_ToggleBits(GPIOD, GPIO_Pin_0);//翻转LED端口电平
- }
注释 @far @interrupt void NonHandledInterrupt (void)函数,添加 #include "stm8l15x_it.h" ,解决重定义拨错问题,
将 _vectab中irq9的中断处理函数由 NonHandledInterrupt修改为 EXTI1_IRQHandler
- /* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
- * Copyright (c) 2007 STMicroelectronics
- */
-
- /* 添加stm8l15x_it.h头文件 */
- #include "stm8l15x_it.h"
-
- typedef void @far (*interrupt_handler_t)(void);
-
- struct interrupt_vector {
- unsigned char interrupt_instruction;
- interrupt_handler_t interrupt_handler;
- };
-
- /* 注释掉NonHandledInterrupt */
- #if 0
- @far @interrupt void NonHandledInterrupt (void)
- {
- /* in order to detect unexpected events during development,
- it is recommended to set a breakpoint on the following instruction
- */
- return;
- }
- #endif
-
- extern void _stext(); /* startup routine */
-
- struct interrupt_vector const _vectab[] = {
- {0x82, (interrupt_handler_t)_stext}, /* reset */
- {0x82, NonHandledInterrupt}, /* trap */
- {0x82, NonHandledInterrupt}, /* irq0 */
- {0x82, NonHandledInterrupt}, /* irq1 */
- {0x82, NonHandledInterrupt}, /* irq2 */
- {0x82, NonHandledInterrupt}, /* irq3 */
- {0x82, NonHandledInterrupt}, /* irq4 */
- {0x82, NonHandledInterrupt}, /* irq5 */
- {0x82, NonHandledInterrupt}, /* irq6 */
- {0x82, NonHandledInterrupt}, /* irq7 */
- {0x82, NonHandledInterrupt}, /* irq8 */
- {0x82, EXTI1_IRQHandler}, /* irq9 */
- {0x82, NonHandledInterrupt}, /* irq10 */
- {0x82, NonHandledInterrupt}, /* irq11 */
- {0x82, NonHandledInterrupt}, /* irq12 */
- {0x82, NonHandledInterrupt}, /* irq13 */
- {0x82, NonHandledInterrupt}, /* irq14 */
- {0x82, NonHandledInterrupt}, /* irq15 */
- {0x82, NonHandledInterrupt}, /* irq16 */
- {0x82, NonHandledInterrupt}, /* irq17 */
- {0x82, NonHandledInterrupt}, /* irq18 */
- {0x82, NonHandledInterrupt}, /* irq19 */
- {0x82, NonHandledInterrupt}, /* irq20 */
- {0x82, NonHandledInterrupt}, /* irq21 */
- {0x82, NonHandledInterrupt}, /* irq22 */
- {0x82, NonHandledInterrupt}, /* irq23 */
- {0x82, NonHandledInterrupt}, /* irq24 */
- {0x82, NonHandledInterrupt}, /* irq25 */
- {0x82, NonHandledInterrupt}, /* irq26 */
- {0x82, NonHandledInterrupt}, /* irq27 */
- {0x82, NonHandledInterrupt}, /* irq28 */
- {0x82, NonHandledInterrupt}, /* irq29 */
- };
4、测试
设置断点单步调试,按下按键led点亮,再按led熄灭。