[牛人杂谈] 新唐cortex-m0一个霸气的功能,48个引脚都可以产生中断

[复制链接]
 楼主| mintspring 发表于 2016-8-23 11:04 | 显示全部楼层 |阅读模式

笔者的项目虽然已经接近尾声,但是不断的被要求加入一些新的功能,从而体验到新唐Cortex-M0一个非常霸气的功能,那就是48个引脚都可以产生中断!

   外部中断一般用于按键,按键一多,外部中断就不够用了,如果用查询的方式的话,一定会非常影响效率的,所以这个功能无疑是给了我们一个非常便捷的方式来实现多个按键。

  在我做的分体式空调之遥控器部分里,在现场调试安装这个东西的时候,是不可能让上位机一个条命令一条命令的来给你传送的,那样太慢了,所以,加上按键,无疑是最好的选择(每个按键实现不同的功能),可是Cortex-M0这个只支持两个外部中断,我要加上四个按键,于是查阅资料下发现了这个功能。


 楼主| mintspring 发表于 2016-8-23 11:05 | 显示全部楼层
既然这样,为什么还要留两个外部中断?

  如果要捕获实时性很高的外部中断,建议用系统自带的外部中断功能,因为新唐的Cortex-M0,它是多P2P3P4用的是一个中断向量表,即24个引脚中断用的是一个中断向量表,你要正确判断中到底是哪个引脚产生了中断,必须用if进行判断,这样就降低了实时性。

  它的设置和外部中断的设置是一样的,唯一不同的是,就是在设置为引脚模式的时候,采用默认设置就可以,而外部中断则必须设置为外部中断模式!
lai832 发表于 2016-8-23 23:07 | 显示全部楼层
才发现?Mx的所有I/O都支持中断, INT0,INT1只是比较专业,     I/O用作中断是业余功能,
643757107 发表于 2016-8-24 17:40 | 显示全部楼层
INT0,INT1只是比较专业,     I/O用作中断性能不足是因为要进入后先判断是哪个管脚的中断,也就是一般8个IO端口共用一个中断入口。
dongnanxibei 发表于 2016-8-24 18:15 | 显示全部楼层
  外部中断一般用于按键,按键一多,外部中断就不够用了
zhuomuniao110 发表于 2016-8-24 20:44 | 显示全部楼层
IO中断是若干个IO共用一个入口实现中断,进入后需要先判断是哪个脚发生了中断。
 楼主| mintspring 发表于 2016-8-25 22:55 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 2 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. /**
  15. * @brief       Port0/Port1 IRQ
  16. *
  17. * @param       None
  18. *
  19. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  20. *
  21. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Port0/Port1 default IRQ, declared in startup_M051Series.s.
  22. */
  23. void GPIOP0P1_IRQHandler(void)
  24. {
  25.     /* To check if P1.3 interrupt occurred */
  26.     if(GPIO_GET_INT_FLAG(P1, BIT3))
  27.     {
  28.         GPIO_CLR_INT_FLAG(P1, BIT3);
  29.         printf("P1.3 INT occurred.\n");
  30.     }
  31.     else
  32.     {
  33.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  34.         P0->ISRC = P0->ISRC;
  35.         P1->ISRC = P1->ISRC;
  36.         printf("Un-expected interrupts.\n");
  37.     }
  38. }

  39. /**
  40. * @brief       Port2/Port3/Port4 IRQ
  41. *
  42. * @param       None
  43. *
  44. * @return      None
  45. *
  46. * @details     The Port2/Port3/Port4 default IRQ, declared in startup_M051Series.s.
  47. */
  48. void GPIOP2P3P4_IRQHandler(void)
  49. {
  50.     /* To check if P4.5 interrupt occurred */
  51.     if(GPIO_GET_INT_FLAG(P4, BIT5))
  52.     {
  53.         GPIO_CLR_INT_FLAG(P4, BIT5);
  54.         printf("P4.5 INT occurred.\n");
  55.     }
  56.     else
  57.     {
  58.         /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
  59.         P2->ISRC = P2->ISRC;
  60.         P3->ISRC = P3->ISRC;
  61.         P4->ISRC = P4->ISRC;
  62.         printf("Un-expected interrupts.\n");
  63.     }
  64. }

  65. void SYS_Init(void)
  66. {
  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init System Clock                                                                                       */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Enable Internal RC 22.1184MHz clock */
  71.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  72.     /* Waiting for Internal RC clock ready */
  73.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  74.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  75.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  76.     /* Enable external XTAL 12MHz clock */
  77.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  78.     /* Waiting for external XTAL clock ready */
  79.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  80.     /* Set core clock as PLL_CLOCK from PLL */
  81.     CLK_SetCoreClock(PLL_CLOCK);

  82.     /* Enable UART module clock */
  83.     CLK_EnableModuleClock(UART0_MODULE);

  84.     /* Select UART module clock source */
  85.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  86.     /*---------------------------------------------------------------------------------------------------------*/
  87.     /* Init I/O Multi-function                                                                                 */
  88.     /*---------------------------------------------------------------------------------------------------------*/

  89.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  90.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  91.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  92. }

  93. void UART0_Init(void)
  94. {
  95.     /*---------------------------------------------------------------------------------------------------------*/
  96.     /* Init UART                                                                                               */
  97.     /*---------------------------------------------------------------------------------------------------------*/
  98.     /* Reset UART */
  99.     SYS_ResetModule(UART0_RST);

  100.     /* Configure UART0 and set UART0 Baudrate */
  101.     UART_Open(UART0, 115200);
  102. }

  103. /*---------------------------------------------------------------------------------------------------------*/
  104. /* MAIN function                                                                                           */
  105. /*---------------------------------------------------------------------------------------------------------*/
  106. int main(void)
  107. {
  108.     /* Unlock protected registers */
  109.     SYS_UnlockReg();

  110.     /* Init System, peripheral clock and multi-function I/O */
  111.     SYS_Init();

  112.     /* Lock protected registers */
  113.     SYS_LockReg();

  114.     /* Init UART0 for printf */
  115.     UART0_Init();

  116.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  117.     printf("+------------------------------------------------+\n");
  118.     printf("|    GPIO P1.3 and P4.5 Interrupt Sample Code    |\n");
  119.     printf("+------------------------------------------------+\n\n");

  120.     /*-----------------------------------------------------------------------------------------------------*/
  121.     /* GPIO Interrupt Function Test                                                                        */
  122.     /*-----------------------------------------------------------------------------------------------------*/
  123.     printf("P1.3 and P4.5 are used to test interrupt ......\n");

  124.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  125.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  126.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  127.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  128.     /*  Configure P4.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  129.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);
  130.     GPIO_EnableInt(P4, 5, GPIO_INT_FALLING);
  131.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

  132.     /* Waiting for interrupts */
  133.     while(1);
  134. }

  135. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


dongnanxibei 发表于 2016-8-26 22:27 | 显示全部楼层
oid GPIOP0P1_IRQHandler(void)
{
    /* To check if P1.3 interrupt occurred */
    if(GPIO_GET_INT_FLAG(P1, BIT3))
    {
        GPIO_CLR_INT_FLAG(P1, BIT3);
        printf("P1.3 INT occurred.\n");
    }
    else
    {
        /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
        P0->ISRC = P0->ISRC;
        P1->ISRC = P1->ISRC;
        printf("Un-expected interrupts.\n");
    }
}
zhuomuniao110 发表于 2016-8-26 23:15 来自手机 | 显示全部楼层
为了节约寄存器,io中断共用一个中断入口
gejigeji521 发表于 2016-8-27 13:01 | 显示全部楼层
是不可能让上位机一个条命令一条命令的来给你传送的
玛尼玛尼哄 发表于 2016-8-27 14:09 | 显示全部楼层
  NVIC_EnableIRQ(GPIO_P0P1_IRQn);
最后都加入个这玩意儿是啥意思,这个是中断的总开关吗。
捉虫天师 发表于 2016-8-27 15:19 | 显示全部楼层
ARM都是这样的,每个IO可以实现中断,对于学过51的肯定感觉很神奇。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

302

主题

4962

帖子

24

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

302

主题

4962

帖子

24

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