[应用相关] STM32 之 EXTI

[复制链接]
954|2
 楼主| 史迪威将军 发表于 2015-9-29 22:09 | 显示全部楼层 |阅读模式
按键的硬件结构有一点一定要注意,要在GPIO段上拉电阻,否则GPIO设置成浮空输入后,会造成端口电平不稳定,中断效果不理想。
另外EXTI的映射关系可以看下图,是和管脚号对应的,比较好记

之 EXTI  (搬运)" title="STM32 之 EXTI  (搬运)">之 EXTI  (搬运)" title="STM32 之 EXTI  (搬运)" height="358" width="271">




1MainC语言:
  1. #include "includes.h"


  2. int main(void)
  3. {
  4. RCC_Configuration();    //配置系统时钟
  5. NVIC_Configuration();   //配置 NVIC 和 Vector Table
  6. SysTick_Config();       //配置SysTick的精确延时

  7. GPIO_Configuration();
  8. EXTI_Configuration();

  9. LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; // 初始化让灯全灭

  10. while (1)
  11. {
  12.   
  13.       if( KEY_UP == 0 )
  14.       { LED1_LOW ;}
  15.       if( KEY_DOWN == 0 )
  16.       { LED2_LOW ;}
  17.       if( KEY_LEFT == 0 )
  18.       { LED3_LOW ;}
  19.       if( KEY_RIGHT == 0 )
  20.       { LED4_LOW ;}

  21. }
  22. }

  23. (2)Unique_Device_ID.c

  24. C语言: Codee#14816

  25. #include "includes.h"


  26. vu32 TimingDelay;                  // 精确延时在SysTick中断里用的计数变量


  27. void RCC_Configuration(void)
  28. {
  29. ErrorStatus HSEStartUpStatus;

  30. //将外设 RCC寄存器重设为缺省值
  31. RCC_DeInit();

  32. //设置外部高速晶振(HSE)
  33. RCC_HSEConfig(RCC_HSE_ON);

  34. //等待 HSE 起振
  35. HSEStartUpStatus = RCC_WaitForHSEStartUp();

  36. if(HSEStartUpStatus == SUCCESS)
  37. {
  38.     //预取指缓存使能
  39.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  40.      //设置代码延时值
  41.     //FLASH_Latency_2 2 延时周期
  42.     FLASH_SetLatency(FLASH_Latency_2);

  43.     //设置 AHB 时钟(HCLK)
  44.     //RCC_SYSCLK_Div1 AHB 时钟 = 系统时钟
  45.     RCC_HCLKConfig(RCC_SYSCLK_Div1);

  46.      //设置高速 AHB 时钟(PCLK2)
  47.     //RCC_HCLK_Div2 APB1 时钟 = HCLK / 2
  48.     RCC_PCLK2Config(RCC_HCLK_Div2);

  49.     //设置低速 AHB 时钟(PCLK1)
  50.     //RCC_HCLK_Div2 APB1 时钟 = HCLK / 2
  51.     RCC_PCLK1Config(RCC_HCLK_Div2);

  52.     // PLLCLK = 8MHz * 9 = 72 MHz
  53.     //设置 PLL 时钟源及倍频系数
  54.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  55.     //使能或者失能 PLL
  56.     RCC_PLLCmd(ENABLE);

  57.     //等待指定的 RCC 标志位设置成功 等待PLL初始化成功
  58.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  59.     {
  60.     }


  61.     //设置系统时钟(SYSCLK) 设置PLL为系统时钟源
  62.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  63.     //等待PLL成功用作于系统时钟的时钟源
  64.     // 0x00:HSI 作为系统时钟
  65.     // 0x04:HSE作为系统时钟
  66.     // 0x08:PLL作为系统时钟
  67.     while(RCC_GetSYSCLKSource() != 0x08)
  68.     {
  69.     }
  70. }

  71. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);


  72. //使能或者失能 APB2 外设时钟
  73. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  74. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  75. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  76. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

  77. }


  78. void NVIC_Configuration(void)
  79. {
  80. NVIC_InitTypeDef NVIC_InitStructure_EXTI_LINE7;
  81.    
  82. #ifdef VECT_TAB_RAM

  83. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  84. #else

  85. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  86. #endif

  87. //===== NVIC_EXTI_PB7 ===================================================
  88.    
  89. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  90.    
  91. NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannel = EXTI9_5_IRQChannel;   // 配置使能指定的IRQ(Interrupt ReQuest中断请求)通道
  92. NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelPreemptionPriority = 0; // 配置IRQ的 组 优先级
  93. NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelSubPriority = 0;         // 配置IRQ的 从 优先级
  94. NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelCmd = ENABLE;            // 配置IRQ 使能
  95. NVIC_Init(&NVIC_InitStructure_EXTI_LINE7);                            // 初始化 UART1_IRQ

  96. }


  97. void GPIO_Configuration(void)
  98. {
  99. GPIO_InitTypeDef GPIO_InitStructure_LED_PORTB;
  100. GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTA;
  101. GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTB;
  102. GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTC;

  103. //==== LED =======================================================
  104. GPIO_InitStructure_LED_PORTB.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  105. GPIO_InitStructure_LED_PORTB.GPIO_Speed = GPIO_Speed_50MHz;
  106. GPIO_InitStructure_LED_PORTB.GPIO_Mode = GPIO_Mode_Out_PP;   //推挽输出
  107. GPIO_Init(GPIOB, &GPIO_InitStructure_LED_PORTB);

  108. //==== KEY =======================================================
  109. GPIO_InitStructure_KEY_PORTA.GPIO_Pin = GPIO_Pin_0 ;
  110. GPIO_InitStructure_KEY_PORTA.GPIO_Speed = GPIO_Speed_50MHz;
  111. GPIO_InitStructure_KEY_PORTA.GPIO_Mode = GPIO_Mode_IPU ;     //上拉输入
  112. GPIO_Init(GPIOA, &GPIO_InitStructure_KEY_PORTA);

  113. GPIO_InitStructure_KEY_PORTB.GPIO_Pin = GPIO_Pin_7 ;
  114. GPIO_InitStructure_KEY_PORTB.GPIO_Speed = GPIO_Speed_50MHz;
  115. GPIO_InitStructure_KEY_PORTB.GPIO_Mode = GPIO_Mode_IN_FLOATING;      //浮空输入 ,外部中断按键
  116. GPIO_Init(GPIOB, &GPIO_InitStructure_KEY_PORTB);

  117. GPIO_InitStructure_KEY_PORTC.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  118. GPIO_InitStructure_KEY_PORTC.GPIO_Speed = GPIO_Speed_50MHz;
  119. GPIO_InitStructure_KEY_PORTC.GPIO_Mode = GPIO_Mode_IPU;      //上拉输入
  120. GPIO_Init(GPIOC, &GPIO_InitStructure_KEY_PORTC);

  121. }


  122. void EXTI_Configuration(void)
  123. {
  124. EXTI_InitTypeDef EXTI_InitStructure_EXTI_LINE7;


  125. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource7);                   // 配置 管脚PB7用作外部中断线路


  126. EXTI_InitStructure_EXTI_LINE7.EXTI_Line = EXTI_Line7;                         //配置 使能或失能的外部线路
  127. EXTI_InitStructure_EXTI_LINE7.EXTI_Mode = EXTI_Mode_Interrupt;                //配置 EXTI线路为中断请求 (或者是事件请求)
  128. EXTI_InitStructure_EXTI_LINE7.EXTI_Trigger = EXTI_Trigger_Falling;            //配置 使能线路的触发边沿 -- 下降沿触发中断
  129. EXTI_InitStructure_EXTI_LINE7.EXTI_LineCmd = ENABLE;                          //配置 状态为使能
  130. EXTI_Init(&EXTI_InitStructure_EXTI_LINE7);                                    // 初始化外部中断线路7


  131. EXTI_GenerateSWInterrupt(EXTI_Line7);                                         //线路7产生一个软件中断
  132. }


  133. void SysTick_Config(void)
  134. {
  135.    
  136.     SysTick_CounterCmd(SysTick_Counter_Disable);

  137.    
  138.     SysTick_ITConfig(DISABLE);

  139.    
  140.     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

  141.    
  142.     SysTick_SetReload(9000);

  143.    
  144.     SysTick_ITConfig(ENABLE);

  145. }


  146. void Delay_Ms(u32 nTime)
  147. {

  148. SysTick_CounterCmd(SysTick_Counter_Enable);

  149. TimingDelay = nTime;

  150. while(TimingDelay != 0);


  151. SysTick_CounterCmd(SysTick_Counter_Disable);

  152. SysTick_CounterCmd(SysTick_Counter_Clear);
  153. }

  154. (4)stm32f10x_it.c

  155. C语言: Codee#14818


  156. #include "includes.h"
  157. //#include "stm32f10x_it.h"


  158. // ... ...



  159. // LINE : 407
  160. void EXTI9_5_IRQHandler(void)
  161. {         
  162.        if(EXTI_GetITStatus(EXTI_Line7) == SET)                  // 读取中断状态
  163.        {
  164.               
  165.               LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ;   // 灯全灭
  166.             
  167.               EXTI_ClearITPendingBit(EXTI_Line7);               // 清除标志位
  168.        }
  169.    
  170. }

  171. // ... ...




yiyigirl2014 发表于 2015-9-30 08:15 来自手机 | 显示全部楼层
是的,好多人说怎么样怎么样,实际上就是悬空了
IversonCar 发表于 2015-9-30 08:19 | 显示全部楼层
楼主,你的图片看不到了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

34

主题

291

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部