[DemoCode下载] M261系列的IO输出输入演示

[复制链接]
973|10
 楼主| xuanhuanzi 发表于 2019-6-24 23:54 | 显示全部楼层 |阅读模式
  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. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to set GPIO pin mode and use pin data input/output control.
  5. *
  6. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. *
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"



  11. void SYS_Init(void)
  12. {

  13.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  14.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
  15.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;

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

  19.     /* Enable HIRC clock */
  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 HCLK clock divider as 1 */
  24.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  25.     /* Enable HXT clock */
  26.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  27.     /* Wait for HXT clock ready */
  28.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  29.     /* Enable PLL */
  30.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  31.     /* Waiting for PLL stable */
  32.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  33.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  34.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  35.     /* Enable UART module clock */
  36.     CLK_EnableModuleClock(UART0_MODULE);

  37.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  38.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init I/O Multi-function                                                                                 */
  41.     /*---------------------------------------------------------------------------------------------------------*/

  42.     /* Set multi-function pins for UART0 RXD and TXD */
  43.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  44. }

  45. void UART0_Init(void)
  46. {
  47.     /*---------------------------------------------------------------------------------------------------------*/
  48.     /* Init UART                                                                                               */
  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Reset UART0 */
  51.     SYS_ResetModule(UART0_RST);

  52.     /* Configure UART0 and set UART0 baud rate */
  53.     UART_Open(UART0, 115200);
  54. }

  55. /*---------------------------------------------------------------------------------------------------------*/
  56. /*  Main Function                                                                                          */
  57. /*---------------------------------------------------------------------------------------------------------*/
  58. int32_t main(void)
  59. {

  60.     int32_t i32Err, i32TimeOutCnt;

  61.     /* Unlock protected registers */
  62.     SYS_UnlockReg();

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

  65.     /* Lock protected registers */
  66.     SYS_LockReg();

  67.     /* Init UART0 for printf */
  68.     UART0_Init();

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

  70.     printf("+-------------------------------------------------+\n");
  71.     printf("|    PB.3(Output) and PD.7(Input) Sample Code     |\n");
  72.     printf("+-------------------------------------------------+\n\n");

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

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

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

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

  87.     /* Set time out counter */
  88.     i32TimeOutCnt = 100;

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

  102.     /* Set PB.3 output pin value is high */
  103.     PB3 = 1;

  104.     /* Set time out counter */
  105.     i32TimeOutCnt = 100;

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

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

  128.     /* Configure PB.3 and PD.7 to default Quasi-bidirectional mode */
  129.     GPIO_SetMode(PB, BIT3, GPIO_MODE_QUASI);
  130.     GPIO_SetMode(PD, BIT7, GPIO_MODE_QUASI);

  131.     while(1);

  132. }

  133. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/


评论

怎么通过这个例程点亮灯啊,萌新小白在线求助,可以有偿教下吗  发表于 2020-6-18 15:33
 楼主| xuanhuanzi 发表于 2019-6-24 23:54 | 显示全部楼层
仔细看看,虽然核心换了,但是编程风格和库基本上还是一样的,兼容性很强。
 楼主| xuanhuanzi 发表于 2019-6-24 23:54 | 显示全部楼层
新唐的兼容性做的非常好。
21mengnan 发表于 2019-6-24 23:59 | 显示全部楼层
看起来确实跟M051,M451一样的库。
mintspring 发表于 2019-6-27 00:00 | 显示全部楼层
第一次接触这个系列,学习一下。
heisexingqisi 发表于 2019-6-27 00:36 | 显示全部楼层
需要配置的内容还不少
xinpian101 发表于 2019-6-27 23:32 | 显示全部楼层
按键触发的。。。。
捉虫天师 发表于 2019-6-27 23:48 | 显示全部楼层
是不是中断也可以。
捉虫天师 发表于 2019-6-27 23:48 | 显示全部楼层
是不是中断也可以。这么用。
捉虫天师 发表于 2019-6-27 23:49 | 显示全部楼层
这个API挺好用这么用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

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