[DemoCode下载] GPIO的双边沿中断

[复制链接]
827|8
 楼主| antusheng 发表于 2019-12-22 23:57 | 显示全部楼层 |阅读模式
GPIO, TE, ck, se, TI
  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: 4 $
  5. * $Date: 15/02/06 10:22a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the usage of GPIO external interrupt function and de-bounce 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       External INT0 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 External INT0(P3.2) default IRQ, declared in startup_M058S.s.
  21. */
  22. void EINT0_IRQHandler(void)
  23. {
  24.     /* For P3.2, clear the INT flag */
  25.     GPIO_CLR_INT_FLAG(P3, BIT2);

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

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

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

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

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

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

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

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

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

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

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

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

  67.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  68.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P32_Msk | SYS_MFP_P33_Msk);
  69.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD | SYS_MFP_P32_nINT0 | SYS_MFP_P33_nINT1);

  70. }

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

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

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

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

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

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

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

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

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

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

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

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

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


 楼主| antusheng 发表于 2019-12-22 23:57 | 显示全部楼层
有时候我们需要在电平跳变时候做出相应的响应这就需要用到双边沿中断了。
 楼主| antusheng 发表于 2019-12-22 23:58 | 显示全部楼层
不管是上升沿还是下降沿,都可以触发中断,我们可以在中断里判断是哪种类型。
 楼主| antusheng 发表于 2019-12-22 23:58 | 显示全部楼层
启用中断去跳功能,选择去跳采样周期为1024个LIRC时钟
 楼主| antusheng 发表于 2019-12-22 23:59 | 显示全部楼层
利用LIRC可以滤波,实现去抖动。
zhuotuzi 发表于 2019-12-23 00:00 | 显示全部楼层
是的,可以开启去抖动功能,这样IO对抖动就不敏感了。
天灵灵地灵灵 发表于 2019-12-23 21:30 | 显示全部楼层
防抖效果如何?
yiy 发表于 2019-12-23 23:23 | 显示全部楼层
越看这些基础东西越深奥。
21mengnan 发表于 2019-12-26 15:08 | 显示全部楼层
用低速内部振荡器进行防抖。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

86

主题

1521

帖子

5

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