[STM32F1] STM32外部中断

[复制链接]
 楼主| 734774645 发表于 2016-3-23 10:49 | 显示全部楼层 |阅读模式

主机:WIN7

开发环境:MDK4.23

MCU:STM32F103CBT6

————————————————————————————————————————————————————

说明:

STM32有20个外部中断线,其中EXTI0-EXTI15给I/O端口使用

EXTI线16连接到PVD输出
EXTI线17连接到RTC闹钟事件
EXTI线18连接到USB唤醒事件
EXTI线19连接到以太网唤醒事件(只适用于互联型产品)

1338273627_7101.jpg

源代码:

初始化:

  1. //打开NTRX外部中断
  2. void open_ntrx_irq(void)
  3. {
  4.         //定义中断结构体
  5.         NVIC_InitTypeDef NVIC_InitStructure ;
  6.         //定义外部中断结构体
  7.         EXTI_InitTypeDef EXTI_InitStructure;
  8.         //定义IO初始化结构体
  9.         GPIO_InitTypeDef GPIO_InitStructure;
  10.        
  11.         //初始化NTRX中断脚PB1时钟
  12.         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  13.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                                               
  14.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                     //设置为输入
  15.         GPIO_Init(GPIOB, &GPIO_InitStructure);                          //GPIOB初始化
  16.         //配置中断源为PB1
  17.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
  18.         // 配置EXTI_Line1下降沿触发
  19.         EXTI_ClearITPendingBit(EXTI_Line1);
  20.         EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  21.         EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  22.         EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  23.         EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  24.         EXTI_Init(&EXTI_InitStructure);
  25.         //打开NTRX中断
  26.         NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;                                //通道设置为外部中断线0
  27.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;         //中断占先等级0
  28.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;           //中断响应优先级0
  29.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                           //打开中断
  30.         NVIC_Init(&NVIC_InitStructure);                                 //初始化
  31.        
  32.         //EXTI_Line1软件中断允许
  33.         //EXTI_GenerateSWInterrupt(EXTI_Line1);
  34. }

中断函数:

  1. //NTRX中断
  2. void EXTI1_IRQHandler(void)
  3. {
  4.         if(EXTI_GetITStatus(EXTI_Line1) != RESET)
  5.           {
  6.             //清中断
  7.             EXTI_ClearITPendingBit(EXTI_Line1);

  8.                 //中断标志置位
  9.                 Flag_IRQ.ntrx = 1;
  10.           }
  11. }


天灵灵地灵灵 发表于 2016-3-23 13:16 | 显示全部楼层
//打开NTRX中断
        NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;                                //通道设置为外部中断线0
这个NTRX中断没有在百度找的,谁知道是不是写错了?
mintspring 发表于 2016-3-23 17:14 | 显示全部楼层
官方也有这种
  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=288409]@file[/url]    EXTI/main.c
  4.   * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.1
  6.   * [url=home.php?mod=space&uid=212281]@date[/url]    11-October-2013
  7.   * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f0xx.h"
  29. #include "stm32f0308_discovery.h"

  30. /** @addtogroup STM32F0308_Discovery_Peripheral_Examples
  31.   * @{
  32.   */

  33. /** @addtogroup EXTI_Example
  34.   * @{
  35.   */

  36. /* Private typedef -----------------------------------------------------------*/
  37. /* Private define ------------------------------------------------------------*/
  38. /* Private macro -------------------------------------------------------------*/
  39. /* Private variables ---------------------------------------------------------*/
  40. /* Private function prototypes -----------------------------------------------*/
  41. static void EXTI_Config(void);

  42. /* Private functions ---------------------------------------------------------*/

  43. /**
  44.   * @brief  Main program.
  45.   * @param  None
  46.   * @retval None
  47.   */
  48. int main(void)
  49. {
  50.   /*!< At this stage the microcontroller clock setting is already configured,
  51.        this is done through SystemInit() function which is called from startup
  52.        file (startup_stm32f0xx.s) before to branch to application main.
  53.        To reconfigure the default setting of SystemInit() function, refer to
  54.        system_stm32f0xx.c file
  55.      */

  56.   /* Initialize LEDs mounted on STM32F0-Discovery kit */
  57.   STM_EVAL_LEDInit(LED3);
  58.   STM_EVAL_LEDInit(LED4);

  59.   /* Configure PA0 in interrupt mode */
  60.   EXTI_Config();

  61.   /* Generate software interrupt: simulate a falling edge applied on EXTI0 line */
  62.   EXTI_GenerateSWInterrupt(EXTI_Line0);

  63.   /* Infinite loop */
  64.   while (1)
  65.   {
  66.   }
  67. }

  68. /**
  69.   * @brief  Configure PA0 in interrupt mode
  70.   * @param  None
  71.   * @retval None
  72.   */
  73. static void EXTI_Config(void)
  74. {
  75.   EXTI_InitTypeDef   EXTI_InitStructure;
  76.   GPIO_InitTypeDef   GPIO_InitStructure;
  77.   NVIC_InitTypeDef   NVIC_InitStructure;
  78.   
  79.   /* Enable GPIOA clock */
  80.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  81.   /* Configure PA0 pin as input floating */
  82.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  83.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  84.   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  85.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  86.   /* Enable SYSCFG clock */
  87.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  88.   
  89.   /* Connect EXTI0 Line to PA0 pin */
  90.   SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

  91.   /* Configure EXTI0 line */
  92.   EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  93.   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  94.   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  95.   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  96.   EXTI_Init(&EXTI_InitStructure);

  97.   /* Enable and set EXTI0 Interrupt */
  98.   NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
  99.   NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  100.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  101.   NVIC_Init(&NVIC_InitStructure);
  102. }


  103. #ifdef  USE_FULL_ASSERT

  104. /**
  105.   * @brief  Reports the name of the source file and the source line number
  106.   *         where the assert_param error has occurred.
  107.   * @param  file: pointer to the source file name
  108.   * @param  line: assert_param error line source number
  109.   * @retval None
  110.   */
  111. void assert_failed(uint8_t* file, uint32_t line)
  112. {
  113.   /* User can add his own implementation to report the file name and line number,
  114.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  115.   /* Infinite loop */
  116.   while (1)
  117.   {
  118.   }
  119. }
  120. #endif

  121. /**
  122.   * @}
  123.   */

  124. /**
  125.   * @}
  126.   */

  127. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


mmuuss586 发表于 2016-3-23 17:52 | 显示全部楼层
谢谢分享;
 楼主| 734774645 发表于 2016-4-12 12:30 | 显示全部楼层
HAL库应该不是这么实现了。
joketinnle 发表于 2016-4-12 14:10 来自手机 | 显示全部楼层
库函数
 楼主| 734774645 发表于 2016-4-13 22:35 | 显示全部楼层
这是用之前的标准库函数做的,而现在流行的HAL库函数,我还没有搞懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

210

主题

3585

帖子

15

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