EXIT中断详解

EXIT中断详解:

EXIT结构体:

typedef struct
{
  uint32_t EXTI_Line;               /*!< Specifies the EXTI lines to be enabled or disabled.
                                         This parameter can be any combination of @ref EXTI_Lines */
   
  EXTIMode_TypeDef EXTI_Mode;       /*!< Specifies the mode for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines.
                                         This parameter can be a value of @ref EXTIMode_TypeDef */

  FunctionalState EXTI_LineCmd;     /*!< Specifies the new state of the selected EXTI lines.
                                         This parameter can be set either to ENABLE or DISABLE */ 
}EXTI_InitTypeDef;

1.EXTI_Line:为中断/事件线选择,总共有20个中断线在( stm32f10x_exit.h )头文件中可以查看。(EXIT0—EXIT19)

2.EXIT_Mode: 中断模式选择,可选为产生中断模式/产生事件

3.EXIT_Trigger :边沿触发事件,可选上升沿触发,下降沿触发,或者上升沿和下降沿都触发

4.EXIT_LineCmd :控制是否使能EXIT线

NVIC结构体:

(misc.h) 头文件中

typedef struct
{
  uint8_t NVIC_IRQChannel;                    /*!< Specifies the IRQ channel to be enabled or disabled.
                                                   This parameter can be a value of @ref IRQn_Type 
                                                   (For the complete STM32 Devices IRQ Channels list, please
                                                    refer to stm32f10x.h file) */

  uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< Specifies the pre-emption priority for the IRQ channel
                                                   specified in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  uint8_t NVIC_IRQChannelSubPriority;         /*!< Specifies the subpriority level for the IRQ channel specified
                                                   in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  FunctionalState NVIC_IRQChannelCmd;         /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel
                                                   will be enabled or disabled. 
                                                   This parameter can be set either to ENABLE or DISABLE */   
} NVIC_InitTypeDef;

1.NVIC_IRQChannel :设置中断通道

2.NVIC_IRQChannelPreemptionnPriority :设置抢占优先级

3.NVIC_IRQChannelSubPriority :设置子优先级

4.NVIC_IRQChannelCmd: 控制是否使能 中断控制器

下表给出了根据 NVIC_PriorityGroupConfig 函数进行的优先权分组配置所允许的优先权和次优先权的值

NVIC_PriorityGroupNVIC_IRQChannelPreemptionPriorityNVIC_IRQChannelSubPriorityDescription
NVIC_PriorityGroup_000-150 bits for pre-emption priority
4 bits for subpriority
NVIC_PriorityGroup_10-10-71 bits for pre-emption priority
3 bits for subpriority
NVIC_PriorityGroup_20-30-32 bits for pre-emption priority
2 bits for subpriority
NVIC_PriorityGroup_30-70-13 bits for pre-emption priority
1 bits for subpriority
NVIC_PriorityGroup_40-1504 bits for pre-emption priority
0 bits for subpriority

表格中可以看出先比较抢占优先级 NVIC_IRQChannelPreemptionPriority 数字越小表示越优先,再去比较子优先级 NVIC_IRQChannelSubPriority 也是同理。

如何配置中断函数:

1.初始化用来中断的GPIO

2.初始化EXIT (边沿检测电路---->上升沿/下降沿触发中断)

3.配置NVIC(中断优先级)

4.编写服务函数 (可以在启动文件 startup_stm32f10x_hd.s 中找到)

中断程序及IO配置:

#include "exit.h"
void Exit_config_Iinit(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//中断位复用所以打开复用时钟
    //配置GPIO
    GPIO_InitTypeDef Gpio_Init;
    Gpio_Init.GPIO_Mode = GPIO_Mode_IPD;
    Gpio_Init.GPIO_Pin = GPIO_Pin_1;
    Gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOA,&Gpio_Init);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);//GPIO中断线的配置
    //配置EXTI外部中断
    EXTI_InitTypeDef Exit_Init;
    Exit_Init.EXTI_Line = EXTI_Line1;
    Exit_Init.EXTI_Mode = EXTI_Mode_Interrupt;
    Exit_Init.EXTI_Trigger = EXTI_Trigger_Falling;
    Exit_Init.EXTI_LineCmd = ENABLE;
    EXTI_Init(&Exit_Init);
    //配置中断控制器
    NVIC_InitTypeDef Nvic_Init;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    Nvic_Init.NVIC_IRQChannel = EXTI1_IRQn;
    Nvic_Init.NVIC_IRQChannelPreemptionPriority = 1;
    Nvic_Init.NVIC_IRQChannelSubPriority = 1;
    Nvic_Init.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&Nvic_Init);
}

中断服务函数配置:

void EXTI1_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line1) != RESET)//判断是否发生了中断
    {
        GPIO_ResetBits(GPIOC,GPIO_Pin_13);
        delay(1000);
        GPIO_SetBits(GPIOC,GPIO_Pin_13);
    }
    EXTI_ClearFlag(EXTI_Line1);//清除中断标志位
}

以上实现中断上升沿触发点亮LED暂停1s后熄灭。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux内核驱动中的定时器是一种常见的机制,可以在指定的时间间隔内触发中断。它的主要作用是进行定时操作,如周期性地刷新屏幕、进行数据采集等。下面是一个简单的Linux内核驱动定时器中断的代码示例: ``` #include <linux/timer.h> #include <linux/init.h> #include <linux/module.h> struct timer_list my_timer; void my_timer_callback(unsigned long data) { printk(KERN_INFO "my_timer_callback called (%ld).\n", jiffies); } static int __init timer_init(void) { int ret; printk(KERN_INFO "timer_init() called\n"); // 设置定时器 setup_timer(&my_timer, my_timer_callback, 0); ret = mod_timer(&my_timer, jiffies + msecs_to_jiffies(1000)); // 定时1秒 if (ret) printk(KERN_ERR "Error in mod_timer\n"); return 0; } static void __exit timer_exit(void) { int ret; ret = del_timer(&my_timer); if (ret) printk(KERN_ERR "The timer is still in use...\n"); printk(KERN_INFO "timer_exit() called\n"); } module_init(timer_init); module_exit(timer_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Linux Timer Driver"); ``` 在上面的示例中,我们定义了一个名为`my_timer`的定时器,并将其设置为1秒钟后触发中断。当定时器到期时,它会调用`my_timer_callback()`函数来处理中断。这个函数会输出一条信息,表明它已被调用。 上述示例只是一个简单的示例,实际上在Linux内核驱动中使用定时器有许多不同的方法和实现方式。如果你需要更深入地了解Linux内核驱动定时器中断代码,你可以查阅相关的文档或书籍,例如《Linux设备驱动开发详解》等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ryan菲特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值