01 点亮一盏LED灯

以下为功能代码

 

  1  1 /**************************************************************************//**
  2   2  * @file     main.c
  3   3  * @version  V3.00
  4   4  * @brief    Show how to set GPIO pin mode and use pin data input/output control.
  5   5  *
  6   6  * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  7   7  *
  8   8  ******************************************************************************/
  9   9 #include "stdio.h"
 10  10 #include "NuMicro.h"
 11  11 
 12  12 
 13  13 void SYS_Init(void)
 14  14 {
 15  15     /* Unlock protected registers */
 16  16     SYS_UnlockReg();
 17  17 
 18  18     /* Enable HIRC */
 19  19     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
 20  20 
 21  21     /* Waiting for HIRC clock ready */
 22  22     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
 23  23 
 24  24     /* Switch HCLK clock source to HIRC */
 25  25     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
 26  26 
 27  27     /* Set both PCLK0 and PCLK1 as HCLK/2 */
 28  28     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);
 29  29     
 30  30 
 31  31     /* Switch UART0 clock source to HIRC */
 32  32     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
 33  33 
 34  34     /* Enable UART peripheral clock */
 35  35     CLK_EnableModuleClock(UART0_MODULE);
 36  36 
 37  37     /* Update System Core Clock */
 38  38     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
 39  39     SystemCoreClockUpdate();
 40  40 
 41  41     /*----------------------------------------------------------------------*/
 42  42     /* Init I/O Multi-function                                              */
 43  43     /*----------------------------------------------------------------------*/
 44  44     /* Set GPB multi-function pins for UART0 RXD and TXD */
 45  45     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
 46  46                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
 47  47 
 48  48     /* Lock protected registers */
 49  49     SYS_LockReg();
 50  50 }
 51  51 
 52  52 /*----------------------------------------------------------------------*/
 53  53 /* Init UART0                                                           */
 54  54 /*----------------------------------------------------------------------*/
 55  55 void UART0_Init(void)
 56  56 {
 57  57     /* Reset UART0 */
 58  58     SYS_ResetModule(UART0_RST);
 59  59 
 60  60     /* Configure UART0 and set UART0 baud rate */
 61  61     UART_Open(UART0, 115200);
 62  62 }
 63  63 static void delay_ms(uint16_t nDelay)
 64  64 {
 65  65     uint16_t nIndex;
 66  66 
 67  67     for(nIndex = 0; nIndex < nDelay; nIndex++)
 68  68 
 69  69     {
 70  70         CLK_SysTickDelay(1000);//delay one ms
 71  71     }
 72  72 }
 73  73 
 74  74 int32_t main(void)
 75  75 {
 76  76     /* Init System, IP clock and multi-function I/O. */
 77  77     SYS_Init();
 78  78 
 79  79     /* Init UART0 for printf */
 80  80     UART0_Init();
 81  81 
 82  82     printf("\n\nCPU @ %dHz\n", SystemCoreClock);
 83  83 
 84  84     printf("+-------------------------------------------------+\n");
 85  85     printf("|    PB.14(Output) Sample Code     |\n");
 86  86     printf("+-------------------------------------------------+\n\n");
 87  87 
 88  88     GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
 89  89       
 90  90     printf("GPIO PB.14(output mode) is OK ......");
 91  91 
 92  92     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
 93  93     /* Set PB.8 output pin value is low */
 94  94         while(1)
 95  95         {
 96  96             PB14 = 0;
 97  97             printf("\n GPIO PB.14 = %d",PB14);
 98  98             delay_ms(2000);
 99  99             PB14 = 1;
100 100             printf("\n GPIO PB.14 = %d",PB14);
101 101             delay_ms(2000);
102 102         }
103 103 }
View Code

 


 

实现功能:LED灯点亮2S,再熄灭2S,串口打印当前状态值。

需要特备注意的几点:

1 延时函数的实现,以后可以借鉴使用

 1 static void delay_ms(uint16_t nDelay)
 2 {
 3     uint16_t nIndex;
 4 
 5     for(nIndex = 0; nIndex < nDelay; nIndex++)
 6 
 7     {
 8         CLK_SysTickDelay(1000);//delay one ms
 9     }
10 }

   函数CLK_SysTickDelay()以us为单位的,所以只需要更改delay_ms()的形参就可以实现需要的延时时间  delay_ms(1000)就是1s

2 GPIO不需要再重新配置时钟

3 特别注意  CLK_SysTickDelay()函数会关闭滴答定时器,也就是说如果在任务中调用了这个函数,那么会关闭滴答定时器,导致FreeRTOS无法运行,可以使用硬延时来实现

posted @ 2020-09-10 16:10  伺机而动的猎人  阅读(397)  评论(0)    收藏  举报