[DemoCode下载] NUC029的IO中断开启去抖动功能

[复制链接]
731|3
 楼主| huahuagg 发表于 2020-6-26 20:19 | 显示全部楼层 |阅读模式
  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: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    NUC029 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 "NUC029xAN.h"


  13. #define PLL_CLOCK           50000000


  14. /**
  15. * @brief       External INT0 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 External INT0(P3.2) default IRQ, declared in startup_NUC029xAN.s.
  22. */
  23. void EINT0_IRQHandler(void)
  24. {
  25.     /* For P3.2, clear the INT flag */
  26.     GPIO_CLR_INT_FLAG(P3, BIT2);

  27.     printf("P3.2 EINT0 occurred.\n");
  28. }

  29. /**
  30. * @brief       External INT1 IRQ
  31. *
  32. * @param       None
  33. *
  34. * @return      None
  35. *
  36. * @details     The External INT1(P3.3) default IRQ, declared in startup_NUC029xAN.s.
  37. */
  38. void EINT1_IRQHandler(void)
  39. {
  40.     /* For P3.3, clear the INT flag */
  41.     GPIO_CLR_INT_FLAG(P3, BIT3);

  42.     printf("P3.3 EINT1 occurred.\n");
  43. }

  44. void SYS_Init(void)
  45. {
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Init System Clock                                                                                       */
  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Enable Internal RC 22.1184MHz clock */
  50.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  51.     /* Waiting for Internal RC clock ready */
  52.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  55.     /* Enable external XTAL 12MHz clock */
  56.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  57.     /* Waiting for external XTAL clock ready */
  58.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  59.     /* Set core clock as PLL_CLOCK from PLL */
  60.     CLK_SetCoreClock(PLL_CLOCK);

  61.     /* Enable UART module clock */
  62.     CLK_EnableModuleClock(UART0_MODULE);

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

  65.     /*---------------------------------------------------------------------------------------------------------*/
  66.     /* Init I/O Multi-function                                                                                 */
  67.     /*---------------------------------------------------------------------------------------------------------*/

  68.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  69.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P32_Msk | SYS_MFP_P33_Msk);
  70.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P32_INT0 | SYS_MFP_P33_INT1);

  71. }

  72. void UART0_Init(void)
  73. {
  74.     /*---------------------------------------------------------------------------------------------------------*/
  75.     /* Init UART                                                                                               */
  76.     /*---------------------------------------------------------------------------------------------------------*/
  77.     /* Reset UART */
  78.     SYS_ResetModule(UART0_RST);

  79.     /* Configure UART0 and set UART0 Baudrate */
  80.     UART_Open(UART0, 115200);
  81. }

  82. /*---------------------------------------------------------------------------------------------------------*/
  83. /* MAIN function                                                                                           */
  84. /*---------------------------------------------------------------------------------------------------------*/
  85. int main(void)
  86. {
  87.     /* Unlock protected registers */
  88.     SYS_UnlockReg();

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

  91.     /* Lock protected registers */
  92.     SYS_LockReg();

  93.     /* Init UART0 for printf */
  94.     UART0_Init();

  95.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  96.     printf("+------------------------------------------------------------+\n");
  97.     printf("|    GPIO EINT0/EINT1 Interrupt and De-bounce Sample Code    |\n");
  98.     printf("+------------------------------------------------------------+\n\n");

  99.     /*-----------------------------------------------------------------------------------------------------*/
  100.     /* GPIO External Interrupt Function Test                                                               */
  101.     /*-----------------------------------------------------------------------------------------------------*/
  102.     printf("EINT0(P3.2) and EINT1(P3.3) are used to test interrupt \n");

  103.     /* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
  104.     GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);
  105.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  106.     NVIC_EnableIRQ(EINT0_IRQn);

  107.     /* Configure P3.3 as EINT1 pin and enable interrupt by rising and falling edge trigger */
  108.     GPIO_SetMode(P3, BIT3, GPIO_PMD_INPUT);
  109.     GPIO_EnableEINT1(P3, 3, GPIO_INT_BOTH_EDGE);
  110.     NVIC_EnableIRQ(EINT1_IRQn);

  111.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 * 10 KHz clock */
  112.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_LIRC, GPIO_DBCLKSEL_1024);
  113.     GPIO_ENABLE_DEBOUNCE(P3, BIT2 | BIT3);

  114.     /* Waiting for interrupts */
  115.     while(1);
  116. }

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


詹求实 发表于 2020-6-27 16:09 | 显示全部楼层
中断去抖动可以达到什么目的呢,或者说有什么效果呢?
 楼主| huahuagg 发表于 2020-6-29 18:04 | 显示全部楼层
詹求实 发表于 2020-6-27 16:09
中断去抖动可以达到什么目的呢,或者说有什么效果呢?

目的很明确啊,防止抖动造成多次重复进入中断重复执行中断啊。
mintspring 发表于 2020-6-29 22:44 | 显示全部楼层
设置完了就等中断。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1430

帖子

2

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