[DemoCode下载] M051的GPIO的4种操作方式

[复制链接]
4240|14
 楼主| wahahaheihei 发表于 2016-4-27 20:28 | 显示全部楼层 |阅读模式
GPIO, TE, se, ck, TI
GPIO_EINTAndDebounce
  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: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. /**
  15. * @brief       External INT0 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 External INT0(P3.2) default IRQ, declared in startup_M051Series.s.
  22. */
  23. void EINT0_IRQHandler(void)
  24. {
  25.     /* For P3.2, clear the INT flag */
  26.     GPIO_CLR_INT_FLAG(P3, BIT2);

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

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

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

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

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

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

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

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

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

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

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

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

  68.     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  69.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P32_Msk | SYS_MFP_P33_Msk);
  70.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P32_INT0 | SYS_MFP_P33_INT1);

  71. }

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

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

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

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

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

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

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

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

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

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

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

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

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


 楼主| wahahaheihei 发表于 2016-4-27 20:28 | 显示全部楼层
根据名字我们知道,这个是外部中断,切消除抖动的应用方式。
 楼主| wahahaheihei 发表于 2016-4-27 20:32 | 显示全部楼层

GPIO_INT
这是个普通的IO中断应用

  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 2 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. /**
  15. * @brief       Port0/Port1 IRQ
  16. *
  17. * @param       None
  18. *
  19. * @return      None
  20. *
  21. * @details     The Port0/Port1 default IRQ, declared in startup_M051Series.s.
  22. */
  23. void GPIOP0P1_IRQHandler(void)
  24. {
  25.     /* To check if P1.3 interrupt occurred */
  26.     if(GPIO_GET_INT_FLAG(P1, BIT3))
  27.     {
  28.         GPIO_CLR_INT_FLAG(P1, BIT3);
  29.         printf("P1.3 INT occurred.\n");
  30.     }
  31.     else
  32.     {
  33.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  34.         P0->ISRC = P0->ISRC;
  35.         P1->ISRC = P1->ISRC;
  36.         printf("Un-expected interrupts.\n");
  37.     }
  38. }

  39. /**
  40. * @brief       Port2/Port3/Port4 IRQ
  41. *
  42. * @param       None
  43. *
  44. * @return      None
  45. *
  46. * @details     The Port2/Port3/Port4 default IRQ, declared in startup_M051Series.s.
  47. */
  48. void GPIOP2P3P4_IRQHandler(void)
  49. {
  50.     /* To check if P4.5 interrupt occurred */
  51.     if(GPIO_GET_INT_FLAG(P4, BIT5))
  52.     {
  53.         GPIO_CLR_INT_FLAG(P4, BIT5);
  54.         printf("P4.5 INT occurred.\n");
  55.     }
  56.     else
  57.     {
  58.         /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
  59.         P2->ISRC = P2->ISRC;
  60.         P3->ISRC = P3->ISRC;
  61.         P4->ISRC = P4->ISRC;
  62.         printf("Un-expected interrupts.\n");
  63.     }
  64. }

  65. void SYS_Init(void)
  66. {
  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init System Clock                                                                                       */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Enable Internal RC 22.1184MHz clock */
  71.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  72.     /* Waiting for Internal RC clock ready */
  73.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  76.     /* Enable external XTAL 12MHz clock */
  77.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  78.     /* Waiting for external XTAL clock ready */
  79.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

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

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

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

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

  92. }

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

  100.     /* Configure UART0 and set UART0 Baudrate */
  101.     UART_Open(UART0, 115200);
  102. }

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

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

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

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

  116.     printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
  117.     printf("+------------------------------------------------+\n");
  118.     printf("|    GPIO P1.3 and P4.5 Interrupt Sample Code    |\n");
  119.     printf("+------------------------------------------------+\n\n");

  120.     /*-----------------------------------------------------------------------------------------------------*/
  121.     /* GPIO Interrupt Function Test                                                                        */
  122.     /*-----------------------------------------------------------------------------------------------------*/
  123.     printf("P1.3 and P4.5 are used to test interrupt ......\n");

  124.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  125.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  126.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  127.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  128.     /*  Configure P4.5 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  129.     GPIO_SetMode(P4, BIT5, GPIO_PMD_QUASI);
  130.     GPIO_EnableInt(P4, 5, GPIO_INT_FALLING);
  131.     NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);

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

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


 楼主| wahahaheihei 发表于 2016-4-27 20:32 | 显示全部楼层
我们可以看出来系统已经定义好了中断函数的名称,只需要照着写就行了。
 楼主| wahahaheihei 发表于 2016-4-27 20:37 | 显示全部楼层
GPIO_OutputInput
字面上看是输出,输入
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/
  19.     /* Enable Internal RC 22.1184MHz clock */
  20.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  21.     /* Waiting for Internal RC clock ready */
  22.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  25.     /* Enable external XTAL 12MHz clock */
  26.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  27.     /* Waiting for external XTAL clock ready */
  28.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  29.     /* Set core clock as PLL_CLOCK from PLL */
  30.     CLK_SetCoreClock(PLL_CLOCK);

  31.     /* Enable UART module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);

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

  35.     /*---------------------------------------------------------------------------------------------------------*/
  36.     /* Init I/O Multi-function                                                                                 */
  37.     /*---------------------------------------------------------------------------------------------------------*/

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

  41. }

  42. void UART0_Init(void)
  43. {
  44.     /*---------------------------------------------------------------------------------------------------------*/
  45.     /* Init UART                                                                                               */
  46.     /*---------------------------------------------------------------------------------------------------------*/
  47.     /* Reset UART */
  48.     SYS_ResetModule(UART0_RST);

  49.     /* Configure UART0 and set UART0 Baudrate */
  50.     UART_Open(UART0, 115200);
  51. }

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /* MAIN function                                                                                           */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. int main(void)
  56. {
  57.     int32_t i32Err;

  58.     /* Unlock protected registers */
  59.     SYS_UnlockReg();

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

  62.     /* Lock protected registers */
  63.     SYS_LockReg();

  64.     /* Init UART0 for printf */
  65.     UART0_Init();

  66.     printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
  67.     printf("+-------------------------------------------------+\n");
  68.     printf("|    P1.2(Output) and P4.1(Input) Sample Code     |\n");
  69.     printf("+-------------------------------------------------+\n\n");

  70.     /* Configure P1.2 as Output mode and P4.1 as Input mode */
  71.     GPIO_SetMode(P1, BIT2, GPIO_PMD_OUTPUT);
  72.     GPIO_SetMode(P4, BIT1, GPIO_PMD_INPUT);

  73.     i32Err = 0;
  74.     printf("GPIO P1.2(output mode) connect to P4.1(input mode) ......");

  75.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  76.     P12 = 0;
  77.     if(P41 != 0)
  78.     {
  79.         i32Err = 1;
  80.     }

  81.     P12 = 1;
  82.     if(P41 != 1)
  83.     {
  84.         i32Err = 1;
  85.     }

  86.     if(i32Err)
  87.     {
  88.         printf("  [FAIL].\n");
  89.     }
  90.     else
  91.     {
  92.         printf("  [OK].\n");
  93.     }

  94.     /* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */
  95.     GPIO_SetMode(P1, BIT2, GPIO_PMD_QUASI);
  96.     GPIO_SetMode(P4, BIT1, GPIO_PMD_QUASI);

  97.     while(1);
  98. }

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


 楼主| wahahaheihei 发表于 2016-4-27 20:39 | 显示全部楼层
实际上这包含两种情况,第一种标准的单向模式,输出是输出,输入是输入,不混用,另外一种就是,既可以作为输入也可以作为输出的准双向模式。
 楼主| wahahaheihei 发表于 2016-4-27 20:46 | 显示全部楼层

GPIO_Powerdown
掉电模式
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 3 $
  5. * $Date: 14/01/28 11:44a $
  6. * @brief    M051 Series GPIO Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLL_CLOCK           50000000


  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /*  Function for System Entry to Power Down Mode                                                           */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. void PowerDownFunction(void)
  18. {
  19.     /* To check if all the debug messages are finished */
  20.     UART_WAIT_TX_EMPTY(UART0);

  21.     SCB->SCR = 4;

  22.     CLK->PWRCON = (CLK->PWRCON & ~(CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WAIT_CPU_Msk)) |
  23.                   CLK_PWRCON_PD_WAIT_CPU_Msk | CLK_PWRCON_PD_WU_INT_EN_Msk;
  24.     CLK->PWRCON |= CLK_PWRCON_PWR_DOWN_EN_Msk;

  25.     __WFI();
  26. }

  27. /**
  28. * @brief       Port0/Port1 IRQ
  29. *
  30. * @param       None
  31. *
  32. * @return      None
  33. *
  34. * @details     The Port0/Port1 default IRQ, declared in startup_M051Series.s.
  35. */
  36. void GPIOP0P1_IRQHandler(void)
  37. {
  38.     /* To check if P1.3 interrupt occurred */
  39.     if(GPIO_GET_INT_FLAG(P1, BIT3))
  40.     {
  41.         GPIO_CLR_INT_FLAG(P1, BIT3);
  42.         printf("P1.3 INT occurred.\n");
  43.     }
  44.     else
  45.     {
  46.         /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
  47.         P0->ISRC = P0->ISRC;
  48.         P1->ISRC = P1->ISRC;
  49.         printf("Un-expected interrupts.\n");
  50.     }
  51. }

  52. void SYS_Init(void)
  53. {
  54.     /*---------------------------------------------------------------------------------------------------------*/
  55.     /* Init System Clock                                                                                       */
  56.     /*---------------------------------------------------------------------------------------------------------*/
  57.     /* Enable Internal RC 22.1184MHz clock */
  58.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  59.     /* Waiting for Internal RC clock ready */
  60.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

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

  63.     /* Enable external XTAL 12MHz clock */
  64.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  65.     /* Waiting for external XTAL clock ready */
  66.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  67.     /* Set core clock as PLL_CLOCK from PLL */
  68.     CLK_SetCoreClock(PLL_CLOCK);

  69.     /* Enable UART module clock */
  70.     CLK_EnableModuleClock(UART0_MODULE);

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

  73.     /*---------------------------------------------------------------------------------------------------------*/
  74.     /* Init I/O Multi-function                                                                                 */
  75.     /*---------------------------------------------------------------------------------------------------------*/

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

  79. }

  80. void UART0_Init(void)
  81. {
  82.     /*---------------------------------------------------------------------------------------------------------*/
  83.     /* Init UART                                                                                               */
  84.     /*---------------------------------------------------------------------------------------------------------*/
  85.     /* Reset UART */
  86.     SYS_ResetModule(UART0_RST);

  87.     /* Configure UART0 and set UART0 Baudrate */
  88.     UART_Open(UART0, 115200);
  89. }

  90. /*---------------------------------------------------------------------------------------------------------*/
  91. /* MAIN function                                                                                           */
  92. /*---------------------------------------------------------------------------------------------------------*/
  93. int main(void)
  94. {
  95.     /* Unlock protected registers */
  96.     SYS_UnlockReg();

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

  99.     /* Init UART0 for printf */
  100.     UART0_Init();

  101.     printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
  102.     printf("+-------------------------------------------------------+\n");
  103.     printf("|    GPIO Power-Down and Wake-up by P1.3 Sample Code    |\n");
  104.     printf("+-------------------------------------------------------+\n\n");

  105.     /* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
  106.     GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
  107.     GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
  108.     NVIC_EnableIRQ(GPIO_P0P1_IRQn);

  109.     /* Waiting for P1.3 rising-edge interrupt event */
  110.     while(1)
  111.     {
  112.         printf("Enter to Power-Down ......\n");
  113.         PowerDownFunction();
  114.         UART_WAIT_TX_EMPTY(UART0);
  115.         printf("System waken-up done.\n\n");
  116.     }

  117. }

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


 楼主| wahahaheihei 发表于 2016-4-27 20:48 | 显示全部楼层
GPIO Power-Down and Wake-up by P1.3 Sample Code
看这里,原来是系统进入掉电模式,也就是关机。然后通过IO的中断触发唤醒系统。
 楼主| wahahaheihei 发表于 2016-4-27 20:54 | 显示全部楼层
/* Configure P1.3 as Input mode and enable interrupt by rising edge trigger */
    GPIO_SetMode(P1, BIT3, GPIO_PMD_INPUT);
    GPIO_EnableInt(P1, 3, GPIO_INT_RISING);
    NVIC_EnableIRQ(GPIO_P0P1_IRQn);
而这里对IO的中断使用,中断嘛,都是输入的,所以第一步设置为输入,只需要三个参数,端口名称,和所处的位置,然后就是输入。
使能也简单,同样指定端口,设置为什么样的中断触发模式,最后开启总中断,我们看到这里P0和P1共用了一个总中断入口。
Roderman_z 发表于 2016-4-27 20:58 | 显示全部楼层
这个GPIO的应用和其他应用的方式还是非常类似的
 楼主| wahahaheihei 发表于 2016-4-27 20:58 | 显示全部楼层
掉电模式就有趣了,如果不会设置的,一定要记住了,直接复制过去用。

/*---------------------------------------------------------------------------------------------------------*/
/*  Function for System Entry to Power Down Mode                                                           */
/*---------------------------------------------------------------------------------------------------------*/
void PowerDownFunction(void)
{
    /* To check if all the debug messages are finished */
    UART_WAIT_TX_EMPTY(UART0);

    SCB->SCR = 4;

    CLK->PWRCON = (CLK->PWRCON & ~(CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WAIT_CPU_Msk)) |
                  CLK_PWRCON_PD_WAIT_CPU_Msk | CLK_PWRCON_PD_WU_INT_EN_Msk;
    CLK->PWRCON |= CLK_PWRCON_PWR_DOWN_EN_Msk;

    __WFI();
}

确实不好懂啊,好在这写的好好的,直接抄过去用。
 楼主| wahahaheihei 发表于 2016-4-27 21:01 | 显示全部楼层
Roderman_z 发表于 2016-4-27 20:58
这个GPIO的应用和其他应用的方式还是非常类似的

是的,这个是基本的操作,不过用法看似很多实际上就跟别的单片机IO是一样的。
 楼主| wahahaheihei 发表于 2016-4-27 21:04 | 显示全部楼层
/**
* @brief       Port0/Port1 IRQ
*
* @param       None
*
* @return      None
*
* @details     The Port0/Port1 default IRQ, declared in startup_M051Series.s.
*/
void GPIOP0P1_IRQHandler(void)
{
    /* To check if P1.3 interrupt occurred */
    if(GPIO_GET_INT_FLAG(P1, BIT3))
    {
        GPIO_CLR_INT_FLAG(P1, BIT3);
        printf("P1.3 INT occurred.\n");
    }
    else
    {
        /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
        P0->ISRC = P0->ISRC;
        P1->ISRC = P1->ISRC;
        printf("Un-expected interrupts.\n");
    }
}
这个中断函数的入口那是系统文件里定义好的地址。
进来后先判断是不是这个我要的那个端口位的中断,是的话,打印一下。不是,那就是其他端口的,清理中断标志位,打印不是期望的中断。
由于这个入口包含P0和P1,因此两个都要清理。

yiyigirl2014 发表于 2016-4-28 11:41 | 显示全部楼层


  •     /* Set P3 multi-function pins for UART0 RXD, TXD, EINT0 and EINT1 */
  •     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk | SYS_MFP_P32_Msk | SYS_MFP_P33_Msk);
  •     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0 | SYS_MFP_P32_INT0 | SYS_MFP_P33_INT1);
  • }
这里第一句,意思是这四个端口都干啥用啊?

最后一句是配置为串口0还有中断0和1.
手册上有关于这个寄存器的用法吗?
heisexingqisi 发表于 2016-4-28 21:39 | 显示全部楼层
/* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
    GPIO_SetMode(P3, BIT2, GPIO_PMD_INPUT);
    GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
    NVIC_EnableIRQ(EINT0_IRQn);
配置3.2作为外部中断引脚,先配置为输入模式,然后启动外部中断功能,这说明信号进来是必须的,然后有个看这个端口的东西叫中断使能位。
最口开启外部中断NVIC,不懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

232

主题

3223

帖子

12

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