[牛人杂谈] M058S的GPIO中断学习笔记

[复制链接]
 楼主| 发表于 2025-1-8 14:31 | 显示全部楼层 |阅读模式
学习GPIO中断要先知道跟GPIO中断设置的函数有哪些
第一步查看例程
  1. printf("P1.3/P4.5/P5.2/P6.1 are used to test interrupt ......\n");

  2.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  3.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  4.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  5.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  6.     /*  Configure P4.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  7.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);
  8.     GPIO_EnableInt(P4, 5, GPIO_INT_FALLING);
  9.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

  10.     /*  Configure P5.2 as Input mode and enable interrupt by rising and falling edge trigger */
  11.     GPIO_SetMode(P5, BIT2, GPIO_PMD_INPUT);
  12.     GPIO_EnableInt(P5, 2, GPIO_INT_BOTH_EDGE);
  13.     NVIC_EnableIRQ(GPIO_P5_IRQn);

  14.     /*  Configure P6.1 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  15.     GPIO_SetMode(P6, BIT1, GPIO_PMD_QUASI);
  16.     GPIO_EnableInt(P6, 1, GPIO_INT_FALLING);
  17.     NVIC_EnableIRQ(GPIO_P6P7_IRQn);
原来设置一个引脚为中断工作模式需要三步:
1、设置引脚的模式,模块有2种,准双向模式和输入模式
2、使能引脚的中断功能,设置边沿触发模式,有5种模式,一般都是用下降沿,上升沿,双边沿,另外2种不常用的是高电平,低电平
3、在嵌套中断向量控制器力使能对应的中断号入口
3767677e19e374ac3.png
跟GPIO相关的一共4个参数。

然后就是讨论中断处理函数如何起作用了
先看示例
  1. void GPIOP0P1_IRQHandler(void)
  2. {
  3.     /* To check if P1.3 interrupt occurred */
  4.     if(GPIO_GET_INT_FLAG(P1, BIT3))
  5.     {
  6.         GPIO_CLR_INT_FLAG(P1, BIT3);
  7.         printf("P1.3 INT occurred.\n");
  8.     }
  9.     else
  10.     {
  11.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  12.         P0->ISRC = P0->ISRC;
  13.         P1->ISRC = P1->ISRC;
  14.         printf("Un-expected interrupts.\n");
  15.     }
  16. }

  17. /**
  18. * [url=home.php?mod=space&uid=247401]@brief[/url]       Port2/Port3/Port4 IRQ
  19. *
  20. * @param       None
  21. *
  22. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  23. *
  24. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Port2/Port3/Port4 default IRQ, declared in startup_M058S.s.
  25. */
  26. void GPIOP2P3P4_IRQHandler(void)
  27. {
  28.     /* To check if P4.5 interrupt occurred */
  29.     if(GPIO_GET_INT_FLAG(P4, BIT5))
  30.     {
  31.         GPIO_CLR_INT_FLAG(P4, BIT5);
  32.         printf("P4.5 INT occurred.\n");
  33.     }
  34.     else
  35.     {
  36.         /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
  37.         P2->ISRC = P2->ISRC;
  38.         P3->ISRC = P3->ISRC;
  39.         P4->ISRC = P4->ISRC;
  40.         printf("Un-expected interrupts.\n");
  41.     }
  42. }
示例中直接定义了这几个函数就完成了中断的调用处理,那么这些函数名字是有什么讲究吗
13868677e1b705a148.png
原来在这个.s文件中定义了他们的函数名字,只需要按照这个列表中的名字定义函数,就可以在中断事件发生时候被调用了。


 楼主| 发表于 2025-1-8 14:38 | 显示全部楼层
知道了如何定义中断处理函数,就要知道中断处理函数内必须要做的事情了。
因为很多IO共用了中断处理函数,所以进来要判断,通过获取中断标志位的函数读取是否该引脚发生的中断,并对中断标志进行清零。
之后执行你的中断要处理的私人工作。
根据
        /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
        P0->ISRC = P0->ISRC;
        P1->ISRC = P1->ISRC;
操作,可以知道写1是清标志。
发表于 2025-1-8 18:00 | 显示全部楼层
大佬分析的可真是对啊。
发表于 2025-1-8 19:40 | 显示全部楼层
中断分析的是真是好啊,头文件怎么弄,都说了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

229

主题

3673

帖子

10

粉丝
快速回复 返回顶部 返回列表