[DemoCode下载] NUC029的IO中断还是挺特别的

[复制链接]
965|17
 楼主| mintspring 发表于 2020-6-18 23:38 | 显示全部楼层 |阅读模式
  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: 16/10/25 4:29p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the usage of GPIO interrupt function.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLLCTL_SETTING  CLK_PLLCTL_72MHz_HXT
  13. #define PLL_CLOCK       72000000

  14. /**
  15. * @brief       PortA/PortB 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 PortA/PortB default IRQ, declared in startup_NUC029xGE.s.
  22. */
  23. void GPAB_IRQHandler(void)
  24. {
  25.     /* To check if PB.3 interrupt occurred */
  26.     if(GPIO_GET_INT_FLAG(PB, BIT3))
  27.     {
  28.         GPIO_CLR_INT_FLAG(PB, BIT3);
  29.         printf("PB.3 INT occurred.\n");
  30.     }
  31.     else
  32.     {
  33.         /* Un-expected interrupt. Just clear all PORTA, PORTB interrupts */
  34.         PA->INTSRC = PA->INTSRC;
  35.         PB->INTSRC = PB->INTSRC;
  36.         printf("Un-expected interrupts.\n");
  37.     }
  38. }

  39. /**
  40. * @brief       PortC/PortD/PortE/PortF IRQ
  41. *
  42. * @param       None
  43. *
  44. * @return      None
  45. *
  46. * @details     The PortC/PortD/PortE/PortF default IRQ, declared in startup_NUC029xGE.s.
  47. */
  48. void GPCDEF_IRQHandler(void)
  49. {
  50.     /* To check if PC.4 interrupt occurred */
  51.     if(GPIO_GET_INT_FLAG(PC, BIT4))
  52.     {
  53.         GPIO_CLR_INT_FLAG(PC, BIT4);
  54.         printf("PC.4 INT occurred.\n");
  55.     }
  56.     else
  57.     {
  58.         /* Un-expected interrupt. Just clear all PORTC, PORTD, PORTE and PORTF interrupts */
  59.         PC->INTSRC = PC->INTSRC;
  60.         PD->INTSRC = PD->INTSRC;
  61.         PE->INTSRC = PE->INTSRC;
  62.         PF->INTSRC = PF->INTSRC;
  63.         printf("Un-expected interrupts.\n");
  64.     }
  65. }

  66. void SYS_Init(void)
  67. {

  68.     /*---------------------------------------------------------------------------------------------------------*/
  69.     /* Init System Clock                                                                                       */
  70.     /*---------------------------------------------------------------------------------------------------------*/

  71.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  72.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  73.     /* Wait for HIRC clock ready */
  74.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  75.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  76.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  77.     /* Enable HXT clock (external XTAL 12MHz) */
  78.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  79.     /* Wait for HXT clock ready */
  80.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

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

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

  85.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  86.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

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

  90.     /* Set multi-function pins for UART0 RXD and TXD */
  91.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  92.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

  93. }

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

  101.     /* Configure UART0 and set UART0 baud rate */
  102.     UART_Open(UART0, 115200);
  103. }

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

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

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

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

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

  121.     /*-----------------------------------------------------------------------------------------------------*/
  122.     /* GPIO Interrupt Function Test                                                                        */
  123.     /*-----------------------------------------------------------------------------------------------------*/
  124.     printf("PB.3 and PC.4 are used to test interrupt ......\n");

  125.     /* Configure PB.3 as Input mode and enable interrupt by rising edge trigger */
  126.     GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
  127.     GPIO_EnableInt(PB, 3, GPIO_INT_RISING);
  128.     NVIC_EnableIRQ(GPAB_IRQn);

  129.     /*  Configure PC.4 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  130.     GPIO_SetMode(PC, BIT4, GPIO_MODE_QUASI);
  131.     GPIO_EnableInt(PC, 4, GPIO_INT_FALLING);
  132.     NVIC_EnableIRQ(GPCDEF_IRQn);

  133.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
  134.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
  135.     GPIO_ENABLE_DEBOUNCE(PB, BIT3);
  136.     GPIO_ENABLE_DEBOUNCE(PC, BIT4);

  137.     /* Waiting for interrupts */
  138.     while(1);
  139. }

  140. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| mintspring 发表于 2020-6-18 23:39 | 显示全部楼层
void GPAB_IRQHandler(void)

void GPCDEF_IRQHandler(void)
 楼主| mintspring 发表于 2020-6-18 23:40 | 显示全部楼层
    NVIC_EnableIRQ(GPCDEF_IRQn);
用的是GPABCDEF。。。
 楼主| mintspring 发表于 2020-6-18 23:41 | 显示全部楼层
        GPIO_CLR_INT_FLAG(PC, BIT4);
进入中断后也要先清理标志。
 楼主| mintspring 发表于 2020-6-18 23:41 | 显示全部楼层
如果不是自己想要的那个中断就要清理所有中断标志了。
 楼主| mintspring 发表于 2020-6-18 23:42 | 显示全部楼层
那么会不会有一种情况,同时触发了多个IO中断,如果判断完,只清理了自己的那个,其他的会不会有问题。
 楼主| mintspring 发表于 2020-6-18 23:43 | 显示全部楼层
其实我们明白,因为我们配置了哪个开启IO中断。
    /* Configure PB.3 as Input mode and enable interrupt by rising edge trigger */
    GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
    GPIO_EnableInt(PB, 3, GPIO_INT_RISING);
    NVIC_EnableIRQ(GPAB_IRQn);

    /*  Configure PC.4 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
    GPIO_SetMode(PC, BIT4, GPIO_MODE_QUASI);
    GPIO_EnableInt(PC, 4, GPIO_INT_FALLING);
    NVIC_EnableIRQ(GPCDEF_IRQn);

    /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
    GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
    GPIO_ENABLE_DEBOUNCE(PB, BIT3);
    GPIO_ENABLE_DEBOUNCE(PC, BIT4);
 楼主| mintspring 发表于 2020-6-18 23:43 | 显示全部楼层
所以我们不用担心其他的情况,因为没有设置的,不可能发生。
所以那个else都是很难进来的。
zhuomuniao110 发表于 2020-6-19 16:43 | 显示全部楼层
被你彻底看穿了。
kxsi 发表于 2020-7-6 17:16 | 显示全部楼层
非常感谢楼主分享
nawu 发表于 2020-7-6 17:16 | 显示全部楼层
看的很是透彻
qcliu 发表于 2020-7-6 17:17 | 显示全部楼层
呗你这样一说  确实很特别
tfqi 发表于 2020-7-6 17:17 | 显示全部楼层
呵呵角度很好啊
wiba 发表于 2020-7-6 17:17 | 显示全部楼层
楼主辛苦了
Harvard 发表于 2020-7-6 22:12 | 显示全部楼层
029xE系列好买吗
huangcunxiake 发表于 2020-7-6 22:50 | 显示全部楼层
如何特别啊。
huangcunxiake 发表于 2020-7-6 22:51 | 显示全部楼层

官方淘宝店好想有卖。
Harvard 发表于 2020-7-12 22:50 | 显示全部楼层
huangcunxiake 发表于 2020-7-6 22:51
官方淘宝店好想有卖。

嗯 还没有见到 民间有那么大的批量..

新唐的M0 竞争 同质化太严重了 .感觉抓不到重点啊 ... 用了半天就029LAN最常用 实惠  .
还有没有其他的 5元左右跑量的  求知道..
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

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