[DemoCode下载] MINI51系列的GPIO中断

[复制链接]
 楼主| huangcunxiake 发表于 2017-8-14 22:23 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.10
  4. * $Date: 15/10/12 8:06p $
  5. * [url=home.php?mod=space&uid=247401]@brief[/url]    Shows the usage of GPIO interrupt function.
  6. *
  7. * @note
  8. * Copyright (C) 2012 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "mini51series.h"
  13. #include "GPIO.h"

  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_Mini51.s.
  22. */
  23. void GPIO01_IRQHandler(void)
  24. {
  25.     /* To check if P1.5 interrupt occurred */
  26.     if (P1->ISRC & BIT5) {
  27.         P1->ISRC = BIT5;
  28.         printf("P1.5 INT occurred. \n");

  29.     } else {
  30.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  31.         P0->ISRC = P0->ISRC;
  32.         P1->ISRC = P1->ISRC;
  33.         printf("Un-expected interrupts. \n");
  34.     }
  35. }


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


  59. void SYS_Init(void)
  60. {
  61.     /* Unlock protected registers */
  62.     SYS_UnlockReg();

  63.     /*---------------------------------------------------------------------------------------------------------*/
  64.     /* Init System Clock                                                                                       */
  65.     /*---------------------------------------------------------------------------------------------------------*/

  66.     /* Set P5.0 and P5.1 -> XTAL  */
  67.     SYS->P5_MFP &= ~(SYS_MFP_P50_Msk | SYS_MFP_P51_Msk);
  68.     SYS->P5_MFP |= (SYS_MFP_P50_XTAL1 | SYS_MFP_P51_XTAL2);

  69.     /* Enable external 12MHz XTAL, internal 22.1184MHz */
  70.     CLK->PWRCON |= CLK_PWRCON_XTL12M | CLK_PWRCON_IRC22M_EN_Msk;

  71.     /* Waiting for clock ready */
  72.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL_STB_Msk | CLK_CLKSTATUS_IRC22M_STB_Msk);

  73.     /* Switch HCLK clock source to XTL */
  74.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_XTAL,CLK_CLKDIV_HCLK(1));

  75.     /* STCLK to XTL STCLK to XTL */
  76.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLK_S_XTAL);

  77.     /* Enable IP clock */
  78.     CLK_EnableModuleClock(UART_MODULE);

  79.     /* Select IP clock source */
  80.     CLK_SetModuleClock(UART_MODULE,CLK_CLKSEL1_UART_S_XTAL,CLK_CLKDIV_UART(1));

  81.     /*---------------------------------------------------------------------------------------------------------*/
  82.     /* Init I/O Multi-function                                                                                 */
  83.     /*---------------------------------------------------------------------------------------------------------*/
  84.     /* Set P0 multi-function pins for UART RXD and TXD */
  85.     SYS->P0_MFP &= ~(SYS_MFP_P01_Msk | SYS_MFP_P00_Msk);
  86.     SYS->P0_MFP |= (SYS_MFP_P01_RXD | SYS_MFP_P00_TXD);

  87.     /* Set P3 multi-function pins for Clock Output */
  88.     SYS->P3_MFP = SYS_MFP_P36_CKO;

  89.     /* To update the variable SystemCoreClock */
  90.     SystemCoreClockUpdate();

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

  94. void UART_Init(void)
  95. {
  96.     /*---------------------------------------------------------------------------------------------------------*/
  97.     /* Init UART                                                                                               */
  98.     /*---------------------------------------------------------------------------------------------------------*/
  99.     /* Reset IP */
  100.     SYS_ResetModule(SYS_IPRSTC2_UART_RST_Msk);

  101.     /* Configure UART and set UART Baudrate */
  102.     UART_Open(UART, 115200);

  103. }

  104. /*---------------------------------------------------------------------------------------------------------*/
  105. /* MAIN function                                                                                           */
  106. /*---------------------------------------------------------------------------------------------------------*/
  107. int main (void)
  108. {
  109.     /* Init System, IP clock and multi-function I/O */
  110.     SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.

  111.     /* Init UART for printf */
  112.     UART_Init();

  113.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  114.     printf("+----------------------------------------+ \n");
  115.     printf("|    MINI51 GPIO Interrupt Sample Code   | \n");
  116.     printf("+----------------------------------------+ \n");

  117.     /*-----------------------------------------------------------------------------------------------------*/
  118.     /* GPIO Interrupt Function Test                                                                        */
  119.     /*-----------------------------------------------------------------------------------------------------*/
  120.     printf("\n  P15, P22 are used to test interrupt\n");

  121.     /* Configure P1.5 as Input mode and enable interrupt by rising edge trigger */
  122.     GPIO_SetMode(P1, BIT5, GPIO_PMD_INPUT);
  123.     GPIO_EnableInt(P1, 5, GPIO_INT_RISING);
  124.     NVIC_EnableIRQ(GPIO01_IRQn);

  125.     /*  Configure P2.2 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  126.     GPIO_SetMode(P2, BIT2, GPIO_PMD_QUASI);
  127.     GPIO_EnableInt(P2, 2, GPIO_INT_FALLING);
  128.     NVIC_EnableIRQ(GPIO234_IRQn);

  129.     /* Waiting for interrupts */
  130.     while (1);

  131. }



 楼主| huangcunxiake 发表于 2017-8-14 22:24 | 显示全部楼层
void GPIO234_IRQHandler(void)
{
    /* To check if P2.2 interrupt occurred */
    if (P2->ISRC & BIT2) {
        P2->ISRC = BIT2;
        printf("P2.2 INT occurred. \n");
    } else {
        /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
        P2->ISRC = P2->ISRC;
        P3->ISRC = P3->ISRC;
        P4->ISRC = P4->ISRC;
        printf("Un-expected interrupts. \n");
    }
}

因为共用一个中断接口,所以进来后就先检查是不是自己要的那个。
holts 发表于 2018-1-30 08:01 | 显示全部楼层
TB24dTngVXXXXcVXXXXXXXXXXXX_!!273085872.jpg

好,下了顶起,最近是玩这个
yiy 发表于 2018-1-30 14:03 | 显示全部楼层
holts 发表于 2018-1-30 08:01
好,下了顶起,最近是玩这个

这个板子这么多拨码开关是干啥的
yiy 发表于 2018-1-30 14:03 | 显示全部楼层
有个疑问这个串口使用的printf是怎么选择使用UART0还是UART1的?
xixi2017 发表于 2018-1-30 19:04 | 显示全部楼层
每个系列的IO中断好像都是这样的,只要是ARM-CM系列好像都是。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

222

主题

3700

帖子

11

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

222

主题

3700

帖子

11

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