[技术问答] 请教M0单片机下降沿中断多次触发的问题

[复制链接]
1698|5
 楼主| mcuatmel 发表于 2017-8-11 15:38 | 显示全部楼层 |阅读模式
在用NM200,EXINT0 ,设置为下降沿中断,用库函数,实测现象是:用两根导线,接触瞬间产生一次中断,停约1秒后断开,会产生至少三次的中断,请问是什么原因?
相关程序如下:
    GPIO_SetMode(P3, BIT2, GPIO_MODE_INPUT);
//    GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
        P3->INTTYPE &=0xfffffffb;
        P3->INTEN |=0x00000004;
    NVIC_EnableIRQ(EINT0_IRQn);


void EINT0_IRQHandler(void)
{
    /* For P3.2, clear the INT flag */
    P3->INTSRC = BIT2;
    printf("P3.2 EINT0 occurred. \n");
}


 楼主| mcuatmel 发表于 2017-8-11 16:18 | 显示全部楼层
我把去抖加上好了许多,去抖周期设为最大,但不知这样使用对单片机的效率是否有影响。
huangcunxiake 发表于 2017-8-13 21:28 | 显示全部楼层
进入中断后,要关闭中断,退出时候清理中断标志,然后恢复中断。
a_ziliu 发表于 2017-8-14 09:48 | 显示全部楼层
把彈跳功能打開。
    /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
    GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_1024);
    GPIO_ENABLE_DEBOUNCE(P3, BIT2 );
heisexingqisi 发表于 2017-8-14 20:20 | 显示全部楼层
看看,都是改例子也没注意这些,一会去去看看啥情况。
heisexingqisi 发表于 2017-8-14 20:29 | 显示全部楼层
  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: 5 $
  5. * $Date: 15/02/06 10:22a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the usage of GPIO interrupt function.
  7. * @note
  8. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M058S.h"


  12. #define PLL_CLOCK           50000000


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

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

  64. /**
  65. * @brief       Port5 IRQ
  66. *
  67. * @param       None
  68. *
  69. * @return      None
  70. *
  71. * @details     The Port5 default IRQ, declared in startup_M058S.s.
  72. */
  73. void GPIOP5_IRQHandler(void)
  74. {
  75.     /* To check if P5.2 interrupt occurred */
  76.     if(GPIO_GET_INT_FLAG(P5, BIT2))
  77.     {
  78.         GPIO_CLR_INT_FLAG(P5, BIT2);
  79.         printf("P5.2 INT occurred.\n");
  80.     }
  81.     else
  82.     {
  83.         /* Un-expected interrupt. Just clear all PORT5 interrupts */
  84.         P5->ISRC = P5->ISRC;
  85.         printf("Un-expected interrupts.\n");
  86.     }
  87. }

  88. /**
  89. * @brief       Port6/Port7 IRQ
  90. *
  91. * @param       None
  92. *
  93. * @return      None
  94. *
  95. * @details     The Port6/Port7 default IRQ, declared in startup_M058S.s.
  96. */
  97. void GPIOP6P7_IRQHandler(void)
  98. {
  99.     /* To check if P6.1 interrupt occurred */
  100.     if(GPIO_GET_INT_FLAG(P6, BIT1))
  101.     {
  102.         GPIO_CLR_INT_FLAG(P6, BIT1);
  103.         printf("P6.1 INT occurred.\n");
  104.     }
  105.     else
  106.     {
  107.         /* Un-expected interrupt. Just clear all PORT6 and PORT7 interrupts */
  108.         P6->ISRC = P6->ISRC;
  109.         P7->ISRC = P7->ISRC;
  110.         printf("Un-expected interrupts.\n");
  111.     }
  112. }

  113. void SYS_Init(void)
  114. {
  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Init System Clock                                                                                       */
  117.     /*---------------------------------------------------------------------------------------------------------*/
  118.     /* Enable Internal RC 22.1184MHz clock */
  119.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  120.     /* Waiting for Internal RC clock ready */
  121.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  124.     /* Enable external XTAL 12MHz clock */
  125.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  126.     /* Waiting for external XTAL clock ready */
  127.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  128.     /* Set core clock as PLL_CLOCK from PLL */
  129.     CLK_SetCoreClock(PLL_CLOCK);

  130.     /* Enable UART module clock */
  131.     CLK_EnableModuleClock(UART0_MODULE);

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

  134.     /*---------------------------------------------------------------------------------------------------------*/
  135.     /* Init I/O Multi-function                                                                                 */
  136.     /*---------------------------------------------------------------------------------------------------------*/

  137.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  138.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  139.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD);

  140. }

  141. void UART0_Init(void)
  142. {
  143.     /*---------------------------------------------------------------------------------------------------------*/
  144.     /* Init UART                                                                                               */
  145.     /*---------------------------------------------------------------------------------------------------------*/
  146.     /* Reset UART0 */
  147.     SYS_ResetModule(UART0_RST);

  148.     /* Configure UART0 and set UART0 Baudrate */
  149.     UART_Open(UART0, 115200);
  150. }

  151. /*---------------------------------------------------------------------------------------------------------*/
  152. /* MAIN function                                                                                           */
  153. /*---------------------------------------------------------------------------------------------------------*/
  154. int main(void)
  155. {
  156.     /* Unlock protected registers */
  157.     SYS_UnlockReg();

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

  160.     /* Lock protected registers */
  161.     SYS_LockReg();

  162.     /* Init UART0 for printf */
  163.     UART0_Init();

  164.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  165.     printf("+------------------------------------------------------+\n");
  166.     printf("|    GPIO P1.3/P4.5/P5.2/P6.1 Interrupt Sample Code    |\n");
  167.     printf("+------------------------------------------------------+\n\n");

  168.     /*-----------------------------------------------------------------------------------------------------*/
  169.     /* GPIO Interrupt Function Test                                                                        */
  170.     /*-----------------------------------------------------------------------------------------------------*/
  171.     printf("P1.3/P4.5/P5.2/P6.1 are used to test interrupt ......\n");

  172.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  173.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  174.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  175.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  176.     /*  Configure P4.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  177.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);
  178.     GPIO_EnableInt(P4, 5, GPIO_INT_FALLING);
  179.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

  180.     /*  Configure P5.2 as Input mode and enable interrupt by rising and falling edge trigger */
  181.     GPIO_SetMode(P5, BIT2, GPIO_PMD_INPUT);
  182.     GPIO_EnableInt(P5, 2, GPIO_INT_BOTH_EDGE);
  183.     NVIC_EnableIRQ(GPIO_P5_IRQn);

  184.     /*  Configure P6.1 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  185.     GPIO_SetMode(P6, BIT1, GPIO_PMD_QUASI);
  186.     GPIO_EnableInt(P6, 1, GPIO_INT_FALLING);
  187.     NVIC_EnableIRQ(GPIO_P6P7_IRQn);

  188.     /* Waiting for interrupts */
  189.     while(1);
  190. }

  191. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
您需要登录后才可以回帖 登录 | 注册

本版积分规则

512

主题

1456

帖子

2

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