[DemoCode下载] M051的中断操作

[复制链接]
868|4
 楼主| zhuomuniao110 发表于 2019-5-26 22:50 | 显示全部楼层 |阅读模式
TE, pi, IO, gp, GPIO
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include "M051Series.h"
  8. #include "LCD_Driver.h"

  9. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  10. #define PLL_CLOCK           50000000


  11. void GPIOP0P1_IRQHandler(void)
  12. {
  13.     /* Re-enable P1.3 debounce function */
  14.     GPIO_ENABLE_DEBOUNCE(P1, BIT3);

  15.     /* Clear P1.3 interrupt flag */
  16.     GPIO_CLR_INT_FLAG(P1, BIT3);

  17.     /* Toggle LED */
  18.     GPIO_TOGGLE(P20);

  19.     printf("P1.3 Interrupt!\n");
  20.     LCD_Print(3, "P1.3 Interrupt!");
  21. }

  22. void GPIOP2P3P4_IRQHandler(void)
  23. {
  24.     /* Re-enable P4.5 debounce function */
  25.     GPIO_ENABLE_DEBOUNCE(P4, BIT5);

  26.     /* Clear P4.5 interrupt flag */
  27.     GPIO_CLR_INT_FLAG(P4, BIT5);

  28.     /* Toggle LED */
  29.     GPIO_TOGGLE(P20);

  30.     printf("P2P3P4 Interrupt!\n");
  31.     LCD_Print(3, "P4.5 Interrupt!");
  32. }

  33. void EINT0_IRQHandler(void)
  34. {
  35.     /* Re-enable P3.2 debounce function */
  36.     GPIO_ENABLE_DEBOUNCE(P3, BIT2);

  37.     /* Clear P3.2 interrupt flag */
  38.     GPIO_CLR_INT_FLAG(P3, BIT2);

  39.     /* Toggle LED */
  40.     GPIO_TOGGLE(P20);

  41.     printf("EINT0 Interrupt!\n");
  42.     LCD_Print(3, "EINT0 Interrupt!");
  43. }

  44. void EINT1_IRQHandler(void)
  45. {
  46.     /* Re-enable P3.3 debounce function */
  47.     GPIO_ENABLE_DEBOUNCE(P3, BIT3);

  48.     /* Clear P3.3 interrupt flag */
  49.     GPIO_CLR_INT_FLAG(P3, BIT3);

  50.     /* Toggle LED */
  51.     GPIO_TOGGLE(P20);

  52.     printf("EINT1 Interrupt!\n");
  53.     LCD_Print(3, "EINT1 Interrupt!");
  54. }

  55. void SYS_Init(void)
  56. {
  57.     /*---------------------------------------------------------------------------------------------------------*/
  58.     /* Init System Clock                                                                                       */
  59.     /*---------------------------------------------------------------------------------------------------------*/

  60.     /* Enable Internal RC 22.1184MHz clock */
  61.     CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk;

  62.     /* Waiting for Internal RC clock ready */
  63.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  66.     /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  67.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;

  68.     /* Enable external 12MHz XTAL, 10kHz */
  69.     CLK->PWRCON |= (CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk);

  70.     /* Enable PLL and Set PLL frequency */
  71.     CLK->PLLCON = PLLCON_SETTING;

  72.     /* Waiting for clock ready */
  73.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC10K_STB_Msk);

  74.     /* Use PLL as system clock source */
  75.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(1));

  76.     /* Enable clock of UART0 and SPI0 */
  77.     CLK_EnableModuleClock(UART0_MODULE);
  78.     CLK_EnableModuleClock(SPI0_MODULE);

  79.     /* Select clock of UART0 and SPI0 */
  80.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  81.     CLK_SetModuleClock(SPI0_MODULE,  CLK_CLKSEL1_SPI0_S_HCLK,  MODULE_NoMsk);

  82.     /* Update System Core Clock */
  83.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  84.     //SystemCoreClockUpdate();
  85.     PllClock        = PLL_CLOCK;            // PLL
  86.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  87.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

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

  91.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  92.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  93.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  94.     /* Set P1.4, P1.5, P1.6, P1.7 for SPI0 to driver LCD */
  95.     SYS->P1_MFP &= ~(SYS_MFP_P14_Msk | SYS_MFP_P15_Msk | SYS_MFP_P16_Msk | SYS_MFP_P17_Msk);
  96.     SYS->P1_MFP |= (SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0);
  97. }

  98. void UART0_Init(void)
  99. {
  100.     /*---------------------------------------------------------------------------------------------------------*/
  101.     /* Init UART                                                                                               */
  102.     /*---------------------------------------------------------------------------------------------------------*/
  103.     UART_Open(UART0, 115200);
  104. }

  105. void GPIO_Init(void)
  106. {
  107.     /* Set P2.0 as output pin */
  108.     GPIO_SetMode(P2, BIT0, GPIO_PMD_OUTPUT);

  109.     /* Set P1.3, P4.5 as Quasi-bidirectional mode */
  110.     GPIO_SetMode(P1, BIT3, GPIO_PMD_QUASI);
  111.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);

  112.     /* Set P1.3 as falling edge trigger and enable its interrupt */
  113.     GPIO_EnableInt(P1, 3, GPIO_INT_FALLING);
  114.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  115.     /* Set P4.5 as low level trigger and enable its interrupt */
  116.     GPIO_EnableInt(P4, 5, GPIO_INT_LOW);
  117.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

  118.     /* Debounce function control */
  119.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_HCLK, GPIO_DBCLKSEL_1024);
  120.     GPIO_ENABLE_DEBOUNCE(P1, BIT3);
  121.     GPIO_ENABLE_DEBOUNCE(P3, BIT2 | BIT3);
  122.     GPIO_ENABLE_DEBOUNCE(P4, BIT5);

  123.     /* Configure P3.2 external interrupt */
  124.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  125.     NVIC_EnableIRQ(EINT0_IRQn);

  126.     /* Configure P3.3 external interrupt */
  127.     GPIO_EnableEINT1(P3, 3, GPIO_INT_BOTH_EDGE);
  128.     NVIC_EnableIRQ(EINT1_IRQn);

  129. }

  130. /*---------------------------------------------------------------------------------------------------------*/
  131. /* MAIN function                                                                                          */
  132. /*---------------------------------------------------------------------------------------------------------*/
  133. int main(void)
  134. {
  135.     /* Unlock protected registers */
  136.     SYS_UnlockReg();

  137.     /* Init System, peripherial clock and multi-function I/O */
  138.     SYS_Init();

  139.     /* Lock protected registers */
  140.     SYS_LockReg();

  141.     /* Init UART0 for printf */
  142.     UART0_Init();

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

  144.     /* Init SPI0 and LCD */
  145.     LCD_Init();
  146.     LCD_EnableBackLight();
  147.     LCD_ClearScreen();

  148.     LCD_Print(0, "Welcome! Nuvoton");
  149.     LCD_Print(1, "This is INT test");

  150.     /*-----------------------------------------------------------------------------------------------------*/
  151.     /* GPIO Interrupt Test                                                                                 */
  152.     /*-----------------------------------------------------------------------------------------------------*/

  153.     printf("P1.3, P4.5, P3.2(EINT0) and P3.3(EINT1) are used to test interrupt\n  and control LEDs(P20)\n");

  154.     /* Init P2.0 (output), P1.3, P4.5 (Quasi-bidirectional) and relative interrupts */
  155.     GPIO_Init();

  156.     /* Waiting for interrupts */
  157.     while(1)
  158.     {
  159.         /* Disable P3.2, P3.3, P1.3, P4.5 debounce to avoid double interrupts when wakeup */
  160.         GPIO_DISABLE_DEBOUNCE(P1, BIT3);
  161.         GPIO_DISABLE_DEBOUNCE(P3, BIT2 | BIT3);
  162.         GPIO_DISABLE_DEBOUNCE(P4, BIT5);

  163.         /* Clear interrupt message */
  164.         LCD_Print(2, "                ");

  165.         /* Hold in wakeup state when P3.2 or P1.3 or P4.5 is low. */
  166.         while((P32 == 0) || (P33 == 0) || (P13 == 0) || (P45 == 0));

  167.         /* Unlock protected registers */
  168.         SYS_UnlockReg();

  169.         /* Waiting for message send out */
  170.         printf("Deep Sleeping\n");
  171.         LCD_Print(2, "Deep Sleeping...");
  172.         LCD_Print(3, "                ");
  173.         while((UART0->FSR & UART_FSR_TE_FLAG_Msk) == 0);

  174.         /* Enter power down. Only P1.3, P4.5, EINT0 or EINT1 can used to wakeup system */
  175.         CLK_PowerDown();
  176.     }

  177. }






 楼主| zhuomuniao110 发表于 2019-5-26 22:51 | 显示全部楼层
这个例子演示了如何用中断唤醒睡着的系统。
21mengnan 发表于 2019-5-27 19:20 | 显示全部楼层
外部中断,新唐的非常靠谱,几乎没啥抖动。
equivalent 发表于 2019-5-29 20:36 | 显示全部楼层
有例程还是很不错的!感谢楼主分享
稳稳の幸福 发表于 2019-5-29 22:16 | 显示全部楼层
多个端口共用一个中断入口,这个可以解决资源。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

233

主题

3529

帖子

11

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