[DemoCode下载] NM1200GPIO演示

[复制链接]
1520|16
 楼主| wanduzi 发表于 2018-2-9 17:01 | 显示全部楼层 |阅读模式
  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/04/08 4:36p $
  5. * [url=home.php?mod=space&uid=247401]@brief[/url]    Use GPIO driver to control the GPIO pin direction, control their
  6. *           high/low state, and how to use GPIO interrupts.
  7. *
  8. * @note
  9. * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "NM1200_NM1100.h"
  14. #include "GPIO.h"

  15. /**
  16. * @brief       Port0/Port1 IRQ
  17. *
  18. * @param       None
  19. *
  20. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  21. *
  22. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Port0/Port1 default IRQ, declared in startup_NM1200_NM1100.s.
  23. */
  24. void GPIO01_IRQHandler(void)
  25. {
  26.     /* To check if P1.5 interrupt occurred */
  27.     if (P1->INTSRC & BIT5) {
  28.         P1->INTSRC = BIT5;
  29.         P30 = P30 ^ 1;
  30.         printf("P1.5 INT occurred. \n");

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


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


  62. /**
  63. * @brief       External INT0 IRQ
  64. *
  65. * @param       None
  66. *
  67. * @return      None
  68. *
  69. * @details     The External INT0(P3.2) default IRQ, declared in startup_NM1200_NM1100.s.
  70. */
  71. void EINT0_IRQHandler(void)
  72. {
  73.     /* For P3.2, clear the INT flag */
  74.     P3->INTSRC = BIT2;
  75.     P30 = P30 ^ 1;
  76.     printf("P3.2 EINT0 occurred. \n");
  77. }


  78. /**
  79. * @brief       External INT1 IRQ
  80. *
  81. * @param       None
  82. *
  83. * @return      None
  84. *
  85. * @details     The External INT1(P5.2) default IRQ, declared in startup_NM1200_NM1100.s.
  86. */
  87. void EINT1_IRQHandler(void)
  88. {
  89.     /* For P5.2, clear the INT flag */
  90.     P5->INTSRC = BIT2;
  91.     P30 = P30 ^ 1;
  92.     printf("P5.2 EINT1 occurred. \n");
  93. }

  94. void SYS_Init(void)
  95. {
  96.     /* Unlock protected registers */
  97.     SYS_UnlockReg();

  98.     /* Read User Config to select internal high speed RC */
  99.     SystemInit();

  100.     /*---------------------------------------------------------------------------------------------------------*/
  101.     /* Init System Clock                                                                                       */
  102.     /*---------------------------------------------------------------------------------------------------------*/

  103.     /* Enable HIRC */
  104.     CLK->PWRCTL |= CLK_PWRCTL_HIRCEN_Msk;

  105.     /* Waiting for clock ready */
  106.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  107.     /* Switch HCLK clock source to XTL */
  108.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_XTAL,CLK_CLKDIV_HCLK(1));

  109.     /* STCLK to XTL STCLK to HCLK/2 */
  110.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  111.     /* Enable IP clock */
  112.     CLK_EnableModuleClock(UART0_MODULE);

  113.     /* Select IP clock source */
  114.     CLK_SetModuleClock(UART0_MODULE,CLK_CLKSEL1_UART0SEL_HIRC,CLK_CLKDIV_UART(1));

  115.     /*---------------------------------------------------------------------------------------------------------*/
  116.     /* Init I/O Multi-function                                                                                 */
  117.     /*---------------------------------------------------------------------------------------------------------*/
  118.     /* Set P0 multi-function pins for UART RXD and TXD */
  119.     SYS->P0_MFP &= ~(SYS_MFP_P01_Msk | SYS_MFP_P00_Msk);
  120.     SYS->P0_MFP |= (SYS_MFP_P01_RXD | SYS_MFP_P00_TXD);

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

  123.     /* To update the variable SystemCoreClock */
  124.     SystemCoreClockUpdate();

  125.     /* Lock protected registers */
  126.     SYS_LockReg();
  127. }

  128. void UART_Init(void)
  129. {
  130.     /*---------------------------------------------------------------------------------------------------------*/
  131.     /* Init UART                                                                                               */
  132.     /*---------------------------------------------------------------------------------------------------------*/
  133.     /* Reset IP */
  134.     SYS_ResetModule(SYS_IPRST1_UART0RST_Msk);

  135.     /* Configure UART and set UART Baudrate */
  136.     UART_Open(UART0, 115200);

  137. }

  138. /*---------------------------------------------------------------------------------------------------------*/
  139. /* MAIN function                                                                                           */
  140. /*---------------------------------------------------------------------------------------------------------*/
  141. int main (void)
  142. {
  143.     int32_t i32Err;

  144.     /* Init System, IP clock and multi-function I/O */
  145.     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.

  146.     /* Init UART for printf */
  147.     UART_Init();

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

  149.     printf("+-------------------------------------+ \n");
  150.     printf("|       GPIO Driver Sample Code       | \n");
  151.     printf("+-------------------------------------+ \n");

  152.     /*-----------------------------------------------------------------------------------------------------*/
  153.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  154.     /*-----------------------------------------------------------------------------------------------------*/
  155.     printf("  >> Please connect P1.0 and P3.4 first << \n");
  156.     printf("     Press any key to start test by using [Pin Data Input/Output Control] \n\n");
  157.     getchar();

  158.     /* Configure P1.0 as Output mode and P3.4 as Input mode then close it */
  159.     GPIO_SetMode(P1, BIT0, GPIO_MODE_OUTPUT);
  160.     GPIO_SetMode(P3, BIT4, GPIO_MODE_INPUT);

  161.     i32Err = 0;
  162.     printf("  GPIO Output/Input test ...... \n");

  163.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  164.     P10 = 0;
  165.     if (P34 != 0) {
  166.         i32Err = 1;
  167.     }

  168.     P10 = 1;
  169.     if (P34 != 1) {
  170.         i32Err = 1;
  171.     }

  172.     if ( i32Err ) {
  173.         printf("  [FAIL] --- Please make sure P1.0 and P3.4 are connected. \n");
  174.     } else {
  175.         printf("  [OK] \n");
  176.     }

  177.     /* Configure P1.0 and P3.4 to default Quasi-bidirectional mode */
  178.     GPIO_SetMode(P1, BIT0, GPIO_MODE_QUASI);
  179.     GPIO_SetMode(P3, BIT4, GPIO_MODE_QUASI);


  180.     /*-----------------------------------------------------------------------------------------------------*/
  181.     /* GPIO Interrupt Function Test                                                                        */
  182.     /*-----------------------------------------------------------------------------------------------------*/
  183.     printf("\n  P15, P22, P32(INT0) and P52(INT1) are used to test interrupt\n  and control LEDs(P30)\n");

  184.     /*Configure P30 for LED control */
  185.     GPIO_SetMode(P3, BIT0, GPIO_MODE_OUTPUT);

  186.     /* Configure P1.5 as Input mode and enable interrupt by rising edge trigger */
  187.     GPIO_SetMode(P1, BIT5, GPIO_MODE_INPUT);
  188.     GPIO_EnableInt(P1, 5, GPIO_INT_RISING);
  189.     NVIC_EnableIRQ(GPIO01_IRQn);


  190.     /*  Configure P2.2 as Quasi-bidirection mode and enable interrupt by falling edge trigger */
  191.     GPIO_SetMode(P2, BIT2, GPIO_MODE_QUASI);
  192.     GPIO_EnableInt(P2, 2, GPIO_INT_FALLING);
  193.     NVIC_EnableIRQ(GPIO234_IRQn);

  194.     /* Configure P3.2 as EINT0 pin and enable interrupt by falling edge trigger */
  195.     GPIO_SetMode(P3, BIT2, GPIO_MODE_INPUT);
  196.     GPIO_EnableEINT0(P3, 2, GPIO_INT_FALLING);
  197.     NVIC_EnableIRQ(EINT0_IRQn);

  198.     /* Configure P5.2 as EINT1 pin and enable interrupt by rising and falling edge trigger */
  199.     GPIO_SetMode(P5, BIT2, GPIO_MODE_INPUT);
  200.     GPIO_EnableEINT1(P5, 2, GPIO_INT_BOTH_EDGE);
  201.     NVIC_EnableIRQ(EINT1_IRQn);

  202.     /* Enable interrupt de-bounce function and select de-bounce sampling cycle time */
  203.     GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK, GPIO_DBNCECON_DBCLKSEL_1);
  204.     GPIO_ENABLE_DEBOUNCE(P1, BIT5);
  205.     GPIO_ENABLE_DEBOUNCE(P2, BIT2);
  206.     GPIO_ENABLE_DEBOUNCE(P3, BIT2);
  207.     GPIO_ENABLE_DEBOUNCE(P5, BIT2);

  208.     /* Waiting for interrupts */
  209.     while (1);

  210. }




yiyigirl2014 发表于 2018-2-9 17:38 | 显示全部楼层
各种操作都演示了一下。
dongnanxibei 发表于 2018-2-10 17:06 | 显示全部楼层
这个系列很少见到。
734774645 发表于 2018-2-10 18:52 | 显示全部楼层
这个多种模式搞不懂,容易出很大问题。
xixi2017 发表于 2018-2-10 20:32 | 显示全部楼层
用到了getchar,不知道是怎么用,找找下载看看。
mintspring 发表于 2018-2-11 10:18 | 显示全部楼层
学习了。这个寄存器还是要记点的。
dongliushui 发表于 2018-2-11 12:02 | 显示全部楼层
用这个IO的操作,还可以模拟好多通信协议。
zhuotuzi 发表于 2018-2-19 16:56 | 显示全部楼层
还具备防抖功能。
zhuotuzi 发表于 2018-2-19 16:57 | 显示全部楼层
还具备防抖功能。
mintspring 发表于 2018-2-24 16:51 | 显示全部楼层
看这命名跟其他系列一样啊,是不是新唐单片机可以互相移植
玛尼玛尼哄 发表于 2018-2-24 18:50 | 显示全部楼层
这个系列有提供开发板吗
dongliushui 发表于 2018-2-24 19:49 来自手机 | 显示全部楼层
看着好复杂啊
小明的同学 发表于 2018-2-25 12:56 | 显示全部楼层
i32Err 这种命名规则是什么鬼
huahuagg 发表于 2018-2-25 20:14 | 显示全部楼层
最简单的就是IO操作了,我觉得最难的是时钟
643757107 发表于 2018-2-27 18:21 | 显示全部楼层
全面详细的对IO的各种操作进行了演示,很好的例子啊
jiekou001 发表于 2018-2-27 19:11 | 显示全部楼层
不知道准双向有没有51的好用啊。
598330983 发表于 2018-7-10 17:28 | 显示全部楼层
有这个系列开发板的手册吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

156

主题

1882

帖子

3

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