Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4397156
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-06-21 10:32:00

程序描述:
         外部按键 KEY1 KEY2 控制 LED 显示
整个工程代码:
 4_key_irq_led.rar  

  1. /*
  2. *    Author :     yuweixian
  3. *    Date:         2011.6.21
  4. *
  5. * Description:
  6. * 开发板上: PA15 - KEY1
  7. * PA13 - KEY2
  8. * key.h 包含了 按键输入初始化
  9.                          stm32 内核NVIC 配置中断线
  10.                          stm32 中断使能 EXTI
  11. */

  12. #ifndef __KEY_H
  13. #define __KEY_H

  14. #include "stm32f10x_gpio.h"
  15. #include "stm32f10x_type.h"
  16. #include "stm32f10x_exti.h"
  17. #include "stm32f10x_nvic.h"
  18. #include "stm32f10x_flash.h"
  19. #include "stm32f10x_map.h"

  20. void key_init(void);    //按键初始化
  21. void Key_EXTI_Configuration(void);    // 中断使能
  22. void Key_NVIC_Configuration(void);// 内核选择 中断线

  23. #endif

  1. #include "key.h"

  2. void key_init(void)
  3. {
  4.     GPIO_InitTypeDef GPIO_InitStructure;

  5.     //初始化 KEY2 PA13
  6.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //KEY2
  7.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;    //上拉
  9.     GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 设置PA13

  10.     //初始化 KEY1 PA15
  11.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  12.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  13.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;    //上拉
  14.     GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化 设置PA15
  15. }

  16. void Key_EXTI_Configuration(void)
  17. {
  18.     EXTI_InitTypeDef EXTI_InitStructure; //EXTI初始化结构体定义

  19.     EXTI_ClearITPendingBit(EXTI_Line13); //清除中断13标志
  20.     EXTI_ClearITPendingBit(EXTI_Line15); //清除中断15标志

  21.     //初始化 PA13
  22.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource13);//选择中断管脚PA13 KEY2
  23.  
  24.     EXTI_InitStructure.EXTI_Line = EXTI_Line13; //线路选择 中断通道13
  25.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置为中断请求,非事件选择
  26.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
  27.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断线使能                
  28.     EXTI_Init(&EXTI_InitStructure); //初始化中断

  29.     //初始化 PA15
  30.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource15);//选择中断管脚PA15 KEY1
  31.  
  32.     EXTI_InitStructure.EXTI_Line = EXTI_Line15; //线路选择 中断通道13
  33.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置为中断请求,非事件选择
  34.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发
  35.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;//中断线使能                
  36.     EXTI_Init(&EXTI_InitStructure); //初始化中断
  37. }


  38. void Key_NVIC_Configuration(void)
  39. {
  40.     NVIC_InitTypeDef NVIC_InitStructure;

  41. #ifdef VECT_TAB_RAM
  42.   /* Set the Vector Table base location at 0x20000000 */
  43.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);

  44. #else /* VECT_TAB_FLASH */
  45.   /* Set the Vector Table base location at 0x08000000 */
  46.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);

  47. #endif

  48.     //选择中断分组2
  49.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  50.     //允许 EXTI15_10中断
  51.     NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel;//选择10-15中断通道
  52.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占式中断优先级设置为 1
  53.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式中断优先级设置为 0
  54.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    //使能中断
  55.     NVIC_Init(&NVIC_InitStructure);     //使能中断
  56. }


阅读(773) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~