Chinaunix首页 | 论坛 | 博客
  • 博客访问: 567400
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: 嵌入式

2017-03-02 14:14:55

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:
  1. /* MAIN.C file
  2.  *
  3.  * Copyright (c) 2002-2005 STMicroelectronics
  4.  */
  5. #include "stm8l15x.h"

  6. //定义LED、按键端口
  7. #define LED_PORT GPIOD
  8. #define LED_PINS GPIO_Pin_0
  9. #define KEY_PORT GPIOB
  10. #define KEY_PINS GPIO_Pin_1
  11. /*******************************************************************************
  12. ****入口参数:无
  13. ****出口参数:无
  14. ****函数备注:不精确延时函数
  15. *******************************************************************************/
  16. void Delay(__IO uint16_t nCount)
  17. {
  18.     /* Decrement nCount value */
  19.     while (nCount != 0)
  20.     {
  21.         nCount--;
  22.     }
  23. }
  24. /*******************************************************************************
  25. ****函数说明:主函数
  26. ****入口参数:
  27. ****出口参数:
  28. ****函数备注:按键按下后在外部中断1中将LED亮灭变换
  29. ********************************************************************************/
  30. void main(void)
  31. {
  32.   GPIO_Init(LED_PORT,LED_PINS,GPIO_Mode_Out_PP_Low_Slow);//初始化LED端口
  33.   GPIO_Init(KEY_PORT, KEY_PINS, GPIO_Mode_In_PU_IT);//初始化按键,上拉输入,带中断
  34.   
  35.   
  36.   EXTI_DeInit (); //恢复中断的所有设置
  37.   EXTI_SetPinSensitivity (EXTI_Pin_1,EXTI_Trigger_Falling);//外部中断1,下降沿触发,向量号9
  38.   enableInterrupts();//使能中断
  39.   
  40.   while (1)//等待中断
  41.   {
  42.       
  43.   }
  44. }
stm8l15x.h:
打开芯片型号宏定义
  1. /* Uncomment the line below according to the target STM8L15x device used in your
  2.    application
  3.   */
  4. /* #define STM8L15X_LD */ /*!< STM8L15X_LD: STM8L15x Low density devices */
  5. /* #define STM8L15X_MD */ /*!< STM8L15X_MD: STM8L15x Medium density devices */
  6. /* #define STM8L15X_MDP */ /*!< STM8L15X_MDP: STM8L15x Medium density plus devices */
  7. /* #define STM8L15X_HD */ /*!< STM8L15X_HD: STM8L15x/16x High density devices */
  8.  #define STM8L05X_LD_VL */ /*!< STM8L05X_LD_VL: STM8L051xx3 Low density value line devices */
  9. /* #define STM8L05X_MD_VL */ /*!< STM8L05X_MD_VL: STM8L052xx6 Medium density value line devices */
  10. /* #define STM8L05X_HD_VL */ /*!< STM8L05X_HD_VL: STM8L052xx8 High density value line devices */
  11. /* #define STM8AL31_L_MD */ /*!< STM8AL31_L_MD: STM8AL3x Medium density devices */
stm8l15x_conf.h:
关闭USE_FULL_ASSERT宏
  1. /* Exported types ------------------------------------------------------------*/
  2. /* Exported constants --------------------------------------------------------*/
  3. /* Uncomment the line below to expanse the "assert_param" macro in the
  4.    Standard Peripheral Library drivers code */
  5. /* #define USE_FULL_ASSERT (1) */

  6. /* Exported macro ------------------------------------------------------------*/
stm8l15x_it.c:
修改中断向量9响应函数INTERRUPT_HANDLER(EXTI1_IRQHandler,9),在该函数中添加自定义中断处理函数
  1. INTERRUPT_HANDLER(EXTI1_IRQHandler,9)
  2. {
  3.     /* In order to detect unexpected events during development,
  4.        it is recommended to set a breakpoint on the following instruction.
  5.     */
  6.         
  7.     EXTI_ClearITPendingBit (EXTI_IT_Pin1);//清除中断标志
  8.     GPIO_ToggleBits(GPIOD, GPIO_Pin_0);//翻转LED端口电平
  9. }
stm8_interrupt_vector.c:
注释@far @interrupt void NonHandledInterrupt (void)函数,添加#include "stm8l15x_it.h",解决重定义拨错问题,
_vectab中irq9的中断处理函数由NonHandledInterrupt修改为EXTI1_IRQHandler
  1. /*    BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
  2.  *    Copyright (c) 2007 STMicroelectronics
  3.  */

  4. /* 添加stm8l15x_it.h头文件 */
  5. #include "stm8l15x_it.h"

  6. typedef void @far (*interrupt_handler_t)(void);

  7. struct interrupt_vector {
  8.     unsigned char interrupt_instruction;
  9.     interrupt_handler_t interrupt_handler;
  10. };

  11. /* 注释掉NonHandledInterrupt */
  12. #if 0
  13. @far @interrupt void NonHandledInterrupt (void)
  14. {
  15.     /* in order to detect unexpected events during development,
  16.      it is recommended to set a breakpoint on the following instruction
  17.     */
  18.     return;
  19. }
  20. #endif

  21. extern void _stext(); /* startup routine */

  22. struct interrupt_vector const _vectab[] = {
  23.     {0x82, (interrupt_handler_t)_stext}, /* reset */
  24.     {0x82, NonHandledInterrupt}, /* trap */
  25.     {0x82, NonHandledInterrupt}, /* irq0 */
  26.     {0x82, NonHandledInterrupt}, /* irq1 */
  27.     {0x82, NonHandledInterrupt}, /* irq2 */
  28.     {0x82, NonHandledInterrupt}, /* irq3 */
  29.     {0x82, NonHandledInterrupt}, /* irq4 */
  30.     {0x82, NonHandledInterrupt}, /* irq5 */
  31.     {0x82, NonHandledInterrupt}, /* irq6 */
  32.     {0x82, NonHandledInterrupt}, /* irq7 */
  33.     {0x82, NonHandledInterrupt}, /* irq8 */
  34.     {0x82, EXTI1_IRQHandler}, /* irq9 */
  35.     {0x82, NonHandledInterrupt}, /* irq10 */
  36.     {0x82, NonHandledInterrupt}, /* irq11 */
  37.     {0x82, NonHandledInterrupt}, /* irq12 */
  38.     {0x82, NonHandledInterrupt}, /* irq13 */
  39.     {0x82, NonHandledInterrupt}, /* irq14 */
  40.     {0x82, NonHandledInterrupt}, /* irq15 */
  41.     {0x82, NonHandledInterrupt}, /* irq16 */
  42.     {0x82, NonHandledInterrupt}, /* irq17 */
  43.     {0x82, NonHandledInterrupt}, /* irq18 */
  44.     {0x82, NonHandledInterrupt}, /* irq19 */
  45.     {0x82, NonHandledInterrupt}, /* irq20 */
  46.     {0x82, NonHandledInterrupt}, /* irq21 */
  47.     {0x82, NonHandledInterrupt}, /* irq22 */
  48.     {0x82, NonHandledInterrupt}, /* irq23 */
  49.     {0x82, NonHandledInterrupt}, /* irq24 */
  50.     {0x82, NonHandledInterrupt}, /* irq25 */
  51.     {0x82, NonHandledInterrupt}, /* irq26 */
  52.     {0x82, NonHandledInterrupt}, /* irq27 */
  53.     {0x82, NonHandledInterrupt}, /* irq28 */
  54.     {0x82, NonHandledInterrupt}, /* irq29 */
  55. };
编译项目通过

4、测试
设置断点单步调试,按下按键led点亮,再按led熄灭。
阅读(3814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~