打印
[STM32F1]

STM32外部中断

[复制链接]
1054|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

主机:WIN7

开发环境:MDK4.23

MCU:STM32F103CBT6

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

说明:

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

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

源代码:

初始化:

//打开NTRX外部中断
void open_ntrx_irq(void)
{
        //定义中断结构体
        NVIC_InitTypeDef NVIC_InitStructure ;
        //定义外部中断结构体
        EXTI_InitTypeDef EXTI_InitStructure;
        //定义IO初始化结构体
        GPIO_InitTypeDef GPIO_InitStructure;
       
        //初始化NTRX中断脚PB1时钟
        RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                                               
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;                     //设置为输入
        GPIO_Init(GPIOB, &GPIO_InitStructure);                          //GPIOB初始化
        //配置中断源为PB1
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);
        // 配置EXTI_Line1下降沿触发
        EXTI_ClearITPendingBit(EXTI_Line1);
        EXTI_InitStructure.EXTI_Line = EXTI_Line1;
        EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
        EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
        EXTI_InitStructure.EXTI_LineCmd = ENABLE;
        EXTI_Init(&EXTI_InitStructure);
        //打开NTRX中断
        NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;                                //通道设置为外部中断线0
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;         //中断占先等级0
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0f;           //中断响应优先级0
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                           //打开中断
        NVIC_Init(&NVIC_InitStructure);                                 //初始化
       
        //EXTI_Line1软件中断允许
        //EXTI_GenerateSWInterrupt(EXTI_Line1);
}

中断函数:

//NTRX中断
void EXTI1_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line1) != RESET)
          {
            //清中断
            EXTI_ClearITPendingBit(EXTI_Line1);

                //中断标志置位
                Flag_IRQ.ntrx = 1;
          }
}


沙发
天灵灵地灵灵| | 2016-3-23 13:16 | 只看该作者
//打开NTRX中断
        NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;                                //通道设置为外部中断线0
这个NTRX中断没有在百度找的,谁知道是不是写错了?

使用特权

评论回复
板凳
mintspring| | 2016-3-23 17:14 | 只看该作者
官方也有这种
/**
  ******************************************************************************
  * [url=home.php?mod=space&uid=288409]@file[/url]    EXTI/main.c
  * [url=home.php?mod=space&uid=187600]@author[/url]  MCD Application Team
  * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.1
  * [url=home.php?mod=space&uid=212281]@date[/url]    11-October-2013
  * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>© COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "stm32f0308_discovery.h"

/** @addtogroup STM32F0308_Discovery_Peripheral_Examples
  * @{
  */

/** @addtogroup EXTI_Example
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void EXTI_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
     */

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

  /* Configure PA0 in interrupt mode */
  EXTI_Config();

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

  /* Infinite loop */
  while (1)
  {
  }
}

/**
  * @brief  Configure PA0 in interrupt mode
  * @param  None
  * @retval None
  */
static void EXTI_Config(void)
{
  EXTI_InitTypeDef   EXTI_InitStructure;
  GPIO_InitTypeDef   GPIO_InitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;
  
  /* Enable GPIOA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

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

  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  
  /* Connect EXTI0 Line to PA0 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

  /* Configure EXTI0 line */
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI0 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


#ifdef  USE_FULL_ASSERT

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

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

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


使用特权

评论回复
地板
mmuuss586| | 2016-3-23 17:52 | 只看该作者
谢谢分享;

使用特权

评论回复
5
734774645|  楼主 | 2016-4-12 12:30 | 只看该作者
HAL库应该不是这么实现了。

使用特权

评论回复
6
joketinnle| | 2016-4-12 14:10 | 只看该作者
库函数

使用特权

评论回复
7
734774645|  楼主 | 2016-4-13 22:35 | 只看该作者
这是用之前的标准库函数做的,而现在流行的HAL库函数,我还没有搞懂。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

197

主题

3451

帖子

14

粉丝