[DemoCode下载] M451的GPIO操作

[复制链接]
1426|13
 楼主| zhuotuzi 发表于 2017-9-6 18:25 | 显示全部楼层 |阅读模式
  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: 6 $
  5. * $Date: 15/09/02 10:04a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to set GPIO pin mode and use pin data input/output control.
  7. * @note
  8. * Copyright (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
  9. *
  10. ******************************************************************************/
  11. #include "stdio.h"
  12. #include "M451Series.h"


  13. #define PLL_CLOCK       72000000


  14. void SYS_Init(void)
  15. {

  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  20.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  21.     /* Wait for HIRC clock ready */
  22.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

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

  27.     /* Wait for HXT clock ready */
  28.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_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 as HXT and UART module clock divider as 1 */
  34.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

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

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

  41. }

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

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

  52. /*---------------------------------------------------------------------------------------------------------*/
  53. /*  Main Function                                                                                          */
  54. /*---------------------------------------------------------------------------------------------------------*/
  55. int32_t main(void)
  56. {

  57.     int32_t i32Err, i32TimeOutCnt;

  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 [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  67.     printf("+-------------------------------------------------+\n");
  68.     printf("|    PB.3(Output) and PD.7(Input) Sample Code     |\n");
  69.     printf("+-------------------------------------------------+\n\n");

  70.     /*-----------------------------------------------------------------------------------------------------*/
  71.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  72.     /*-----------------------------------------------------------------------------------------------------*/
  73.     printf("  >> Please connect PB.3 and PD.7 first << \n");
  74.     printf("     Press any key to start test by using [Pin Data Input/Output Control] \n\n");
  75.     getchar();

  76.     /* Configure PB.3 as Output mode and PD.7 as Input mode then close it */
  77.     GPIO_SetMode(PB, BIT3, GPIO_MODE_OUTPUT);
  78.     GPIO_SetMode(PD, BIT7, GPIO_MODE_INPUT);

  79.     i32Err = 0;
  80.     printf("GPIO PB.3(output mode) connect to PD.7(input mode) ......");

  81.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  82.     /* Set PB.3 output pin value is low */
  83.     PB3 = 0;

  84.     /* Set time out counter */
  85.     i32TimeOutCnt = 100;

  86.     /* Wait for PD.7 input pin status is low for a while */
  87.     while(PD7 != 0)
  88.     {
  89.         if(i32TimeOutCnt > 0)
  90.         {
  91.             i32TimeOutCnt--;
  92.         }
  93.         else
  94.         {
  95.             i32Err = 1;
  96.             break;
  97.         }
  98.     }

  99.     /* Set PB.3 output pin value is high */
  100.     PB3 = 1;

  101.     /* Set time out counter */
  102.     i32TimeOutCnt = 100;

  103.     /* Wait for PD.7 input pin status is high for a while */
  104.     while(PD7 != 1)
  105.     {
  106.         if(i32TimeOutCnt > 0)
  107.         {
  108.             i32TimeOutCnt--;
  109.         }
  110.         else
  111.         {
  112.             i32Err = 1;
  113.             break;
  114.         }
  115.     }

  116.     /* Print test result */
  117.     if(i32Err)
  118.     {
  119.         printf("  [FAIL].\n");
  120.     }
  121.     else
  122.     {
  123.         printf("  [OK].\n");
  124.     }

  125.     /* Configure PB.3 and PD.7 to default Quasi-bidirectional mode */
  126.     GPIO_SetMode(PB, BIT3, GPIO_MODE_QUASI);
  127.     GPIO_SetMode(PD, BIT7, GPIO_MODE_QUASI);

  128.     while(1);

  129. }


 楼主| zhuotuzi 发表于 2017-9-6 18:26 | 显示全部楼层
这个是基本的输入输出操作,输入模式,输出模式,和准双向模式。
 楼主| zhuotuzi 发表于 2017-9-6 18:26 | 显示全部楼层
另外IO可以用于中断事件
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 7 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief    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 @ %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. ***/
 楼主| zhuotuzi 发表于 2017-9-6 18:37 | 显示全部楼层
还可以通过GPIO中断触发休眠模式的唤醒
  1. /**************************************************************************//**
  2. * @file     main.c
  3. * @version  V3.00
  4. * $Revision: 9 $
  5. * $Date: 15/09/02 10:04a $
  6. * @brief    Show how to wake up system from Power-down mode by GPIO interrupt.
  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. /*  Function for System Entry to Power Down Mode                                                           */
  16. /*---------------------------------------------------------------------------------------------------------*/
  17. void PowerDownFunction(void)
  18. {
  19.     /* Check if all the debug messages are finished */
  20.     UART_WAIT_TX_EMPTY(UART0);

  21.     /* Enter to Power-down mode */
  22.     CLK_PowerDown();
  23. }

  24. /**
  25. * @brief       GPIO PB IRQ
  26. *
  27. * @param       None
  28. *
  29. * @return      None
  30. *
  31. * @details     The PB default IRQ, declared in startup_M451Series.s.
  32. */
  33. void GPB_IRQHandler(void)
  34. {
  35.     /* To check if PB.3 interrupt occurred */
  36.     if(GPIO_GET_INT_FLAG(PB, BIT3))
  37.     {
  38.         GPIO_CLR_INT_FLAG(PB, BIT3);
  39.         printf("PB.3 INT occurred.\n");
  40.     }
  41.     else
  42.     {
  43.         /* Un-expected interrupt. Just clear all PB interrupts */
  44.         PB->INTSRC = PB->INTSRC;
  45.         printf("Un-expected interrupts.\n");
  46.     }
  47. }

  48. void SYS_Init(void)
  49. {

  50.     /*---------------------------------------------------------------------------------------------------------*/
  51.     /* Init System Clock                                                                                       */
  52.     /*---------------------------------------------------------------------------------------------------------*/

  53.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  54.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  55.     /* Wait for HIRC clock ready */
  56.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  59.     /* Enable HXT clock (external XTAL 12MHz) */
  60.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  61.     /* Wait for HXT clock ready */
  62.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  63.     /* Set core clock as PLL_CLOCK from PLL */
  64.     CLK_SetCoreClock(PLL_CLOCK);

  65.     /* Enable UART module clock */
  66.     CLK_EnableModuleClock(UART0_MODULE);

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

  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Init I/O Multi-function                                                                                 */
  71.     /*---------------------------------------------------------------------------------------------------------*/

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

  75. }

  76. void UART0_Init()
  77. {
  78.     /*---------------------------------------------------------------------------------------------------------*/
  79.     /* Init UART                                                                                               */
  80.     /*---------------------------------------------------------------------------------------------------------*/
  81.     /* Reset UART module */
  82.     SYS_ResetModule(UART0_RST);

  83.     /* Configure UART0 and set UART0 baud rate */
  84.     UART_Open(UART0, 115200);
  85. }

  86. /*---------------------------------------------------------------------------------------------------------*/
  87. /* MAIN function                                                                                           */
  88. /*---------------------------------------------------------------------------------------------------------*/
  89. int main(void)
  90. {
  91.     /* Unlock protected registers */
  92.     SYS_UnlockReg();

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

  95.     /* Lock protected registers */
  96.     SYS_LockReg();

  97.     /* Init UART0 for printf */
  98.     UART0_Init();

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

  103.     /* Configure PB.3 as Input mode and enable interrupt by rising edge trigger */
  104.     GPIO_SetMode(PB, BIT3, GPIO_MODE_INPUT);
  105.     GPIO_EnableInt(PB, 3, GPIO_INT_RISING);
  106.     NVIC_EnableIRQ(GPB_IRQn);

  107.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time is 1024 clocks of LIRC clock */
  108.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBCTL_DBCLKSRC_LIRC, GPIO_DBCTL_DBCLKSEL_1024);
  109.     GPIO_ENABLE_DEBOUNCE(PB, BIT3);

  110.     /* Unlock protected registers before entering Power-down mode */
  111.     SYS_UnlockReg();

  112.     /* Waiting for PB.3 rising-edge interrupt event */
  113.     while(1)
  114.     {
  115.         printf("Enter to Power-Down ......\n");

  116.         /* Enter to Power-down mode */
  117.         PowerDownFunction();

  118.         printf("System waken-up done.\n\n");
  119.     }

  120. }

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


 楼主| zhuotuzi 发表于 2017-9-6 19:13 | 显示全部楼层
 楼主| zhuotuzi 发表于 2017-9-6 19:16 | 显示全部楼层
 楼主| zhuotuzi 发表于 2017-9-6 19:17 | 显示全部楼层
wahahaheihei 发表于 2017-9-6 22:11 | 显示全部楼层
好多时候,都可以用IO来实现。
wahahaheihei 发表于 2017-9-6 22:11 | 显示全部楼层
掌握IO的几种用法很有必要。
heisexingqisi 发表于 2017-9-6 22:26 | 显示全部楼层
    PowerDownFunction();
封装一个函数调用了。
Andy003 发表于 2017-9-7 12:58 | 显示全部楼层
捉虫天师 发表于 2017-9-10 12:22 来自手机 | 显示全部楼层
掉电是一个状态还是一个动作
mintspring 发表于 2017-9-10 17:05 来自手机 | 显示全部楼层
其实就是那几个寄存器操作封装成函数,比较好用
gejigeji521 发表于 2017-9-12 23:07 | 显示全部楼层
配置串口很容易的
    /* Set PD multi-function pins for UART0 RXD(PD.0) and TXD(PD.1) */
    SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
    SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3375

帖子

7

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