[DemoCode下载] M451的GPIO中断演示分析

[复制链接]
792|8
 楼主| 643757107 发表于 2018-11-16 10:18 | 显示全部楼层 |阅读模式
GPIO, TE, ck, se, rc
  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: 7 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show the usage of GPIO interrupt function.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "M451Series.h"


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


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

  38. /**
  39. * @brief       GPIO PC IRQ
  40. *
  41. * @param       None
  42. *
  43. * @return      None
  44. *
  45. * @details     The PC default IRQ, declared in startup_M451Series.s.
  46. */
  47. void GPC_IRQHandler(void)
  48. {
  49.     /* To check if PC.5 interrupt occurred */
  50.     if(GPIO_GET_INT_FLAG(PC, BIT5))
  51.     {
  52.         GPIO_CLR_INT_FLAG(PC, BIT5);
  53.         printf("PC.5 INT occurred.\n");
  54.     }
  55.     else
  56.     {
  57.         /* Un-expected interrupt. Just clear all PC interrupts */
  58.         PC->INTSRC = PC->INTSRC;
  59.         printf("Un-expected interrupts.\n");
  60.     }
  61. }

  62. void SYS_Init(void)
  63. {

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

  67.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  68.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  69.     /* Wait for HIRC clock ready */
  70.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  73.     /* Enable HXT clock (external XTAL 12MHz) */
  74.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  75.     /* Wait for HXT clock ready */
  76.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  77.     /* Set core clock as PLL_CLOCK from PLL */
  78.     CLK_SetCoreClock(PLL_CLOCK);

  79.     /* Enable UART module clock */
  80.     CLK_EnableModuleClock(UART0_MODULE);

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

  83.     /*---------------------------------------------------------------------------------------------------------*/
  84.     /* Init I/O Multi-function                                                                                 */
  85.     /*---------------------------------------------------------------------------------------------------------*/

  86.     /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
  87.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  88.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  89. }

  90. void UART0_Init()
  91. {
  92.     /*---------------------------------------------------------------------------------------------------------*/
  93.     /* Init UART                                                                                               */
  94.     /*---------------------------------------------------------------------------------------------------------*/
  95.     /* Reset UART module */
  96.     SYS_ResetModule(UART0_RST);

  97.     /* Configure UART0 and set UART0 baud rate */
  98.     UART_Open(UART0, 115200);
  99. }

  100. /*---------------------------------------------------------------------------------------------------------*/
  101. /* MAIN function                                                                                           */
  102. /*---------------------------------------------------------------------------------------------------------*/
  103. int main(void)
  104. {
  105.     /* Unlock protected registers */
  106.     SYS_UnlockReg();

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

  109.     /* Lock protected registers */
  110.     SYS_LockReg();

  111.     /* Init UART0 for printf */
  112.     UART0_Init();

  113.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  114.     printf("+------------------------------------------------+\n");
  115.     printf("|    GPIO PB.2 and PC.5 Interrupt Sample Code    |\n");
  116.     printf("+------------------------------------------------+\n\n");

  117.     /*-----------------------------------------------------------------------------------------------------*/
  118.     /* GPIO Interrupt Function Test                                                                        */
  119.     /*-----------------------------------------------------------------------------------------------------*/
  120.     printf("PB.2 and PC.5 are used to test interrupt ......\n");

  121.     /* Configure PB.2 as Input mode and enable interrupt by rising edge trigger */
  122.     GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
  123.     GPIO_EnableInt(PB, 2, GPIO_INT_RISING);
  124.     NVIC_EnableIRQ(GPB_IRQn);

  125.     /* Configure PC.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  126.     GPIO_SetMode(PC, BIT5, GPIO_MODE_QUASI);
  127.     GPIO_EnableInt(PC, 5, GPIO_INT_FALLING);
  128.     NVIC_EnableIRQ(GPC_IRQn);

  129.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
  130.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
  131.     GPIO_ENABLE_DEBOUNCE(PB, BIT2);
  132.     GPIO_ENABLE_DEBOUNCE(PC, BIT5);

  133.     /* Waiting for interrupts */
  134.     while(1);
  135. }

  136. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/


 楼主| 643757107 发表于 2018-11-16 10:23 | 显示全部楼层
系统初始化函数,先运行,这个函数进行了时钟的配置和IO口多功能的配置(设置串口的功能)
然后是串口初始化,之前串口的IO功能端口已经设置了,这个函数就是配置串口的参数了,然后打开串口并设置波特率
后面是中断端口的选择与配置
设置需要中断IO的端口以及模式,隐身IO中断都是输入类型,所以先设置为输入
然后设置端口的中断触发类型
接着要把端口对应的中断向量激活,也就是使能管脚所在端口的中断。一般一组管脚(端口由多个管脚组成)共用一个中断入口。
 楼主| 643757107 发表于 2018-11-16 10:24 | 显示全部楼层
中断处理函数就是中断的入口函数了。这个名字一般是固定的。
进入后先要判断是哪个管脚的触发,这样你才好知道你设置的哪个管脚是否触发了中断。
如果触发了可以清理该管脚的中断标志位。
如果没有你要的哪个管脚中断,结果发生了中断,就要全部清理了,因为没有你要的。
 楼主| 643757107 发表于 2018-11-16 10:31 | 显示全部楼层
中断函数名字在哪儿定义呢?
953105bee2bf7083f6.png
在这里。
 楼主| 643757107 发表于 2018-11-16 10:32 | 显示全部楼层
所以你不知道用哪个中断函数的时候,可以查看这个文件。
稳稳の幸福 发表于 2018-11-16 22:30 | 显示全部楼层
时钟配置占用一大半代码
734774645 发表于 2018-11-16 23:41 | 显示全部楼层
总结的好啊,分析分析其他外设的呗。比如定时器,PWM
598330983 发表于 2018-11-16 23:46 | 显示全部楼层
    NVIC_EnableIRQ(GPC_IRQn);
这个是不是相当于51单片机开总中断那回事
heisexingqisi 发表于 2018-11-18 12:15 | 显示全部楼层
有没有串口的例子
您需要登录后才可以回帖 登录 | 注册

本版积分规则

223

主题

3937

帖子

11

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