[DemoCode下载] NUC029用定时器写延时函数

[复制链接]
832|7
 楼主| zhuotuzi 发表于 2020-6-23 09:01 | 显示全部楼层 |阅读模式
  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: 2 $
  5. * $Date: 16/10/25 4:30p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to use timer0 to create various delay time.
  7. * @note
  8. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  9. ******************************************************************************/
  10. #include <stdio.h>
  11. #include "NUC029xGE.h"


  12. #define PLL_CLOCK       72000000


  13. void SYS_Init(void)
  14. {
  15.     /*---------------------------------------------------------------------------------------------------------*/
  16.     /* Init System Clock                                                                                       */
  17.     /*---------------------------------------------------------------------------------------------------------*/
  18.     /* Enable HIRC clock */
  19.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  20.     /* Waiting for HIRC clock ready */
  21.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  22.     /* Switch HCLK clock source to HIRC */
  23.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  24.     /* Enable HXT */
  25.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  26.     /* Waiting for clock ready */
  27.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  28.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  29.     CLK_SetCoreClock(PLL_CLOCK);
  30.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  31.     /* Enable peripheral clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);
  33.     CLK_EnableModuleClock(TMR0_MODULE);
  34.     CLK_EnableModuleClock(TMR1_MODULE);

  35.     /* Peripheral clock source */
  36.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_PLL, CLK_CLKDIV0_UART(1));
  37.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);
  38.     CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_HXT, 0);

  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init I/O Multi-function                                                                                 */
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Set multi-function pins for UART0 RXD and TXD */
  43.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
  44.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);
  45. }

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

  53.     /* Configure UART0 and set UART0 Baudrate */
  54.     UART_Open(UART0, 115200);
  55. }

  56. /*---------------------------------------------------------------------------------------------------------*/
  57. /*  MAIN function                                                                                          */
  58. /*---------------------------------------------------------------------------------------------------------*/
  59. int main(void)
  60. {
  61.     volatile uint32_t u32DelayTime;

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

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

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

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

  70.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  71.     printf("+-----------------------------------+\n");
  72.     printf("|    Timer Delay API Sample Code    |\n");
  73.     printf("+-----------------------------------+\n\n");

  74.     printf("# This sample code is using Timer1 to check Timer0 TIMER_Delay API delay time is reasonable or not.\n");
  75.     printf("# Delay time includes 100 ms, 200 ms, 300 ms, 400 ms and 500 ms.\n\n");

  76.     /* Start Timer1 to measure delay period of TIMER_Delay API is reasonable or not */
  77.     TIMER1->CTL = TIMER_PERIODIC_MODE | (12 - 1);
  78.     TIMER_ResetCounter(TIMER1);
  79.     TIMER_Start(TIMER1);

  80.     TIMER_ResetCounter(TIMER1);
  81.     TIMER_Delay(TIMER0, 100000);
  82.     u32DelayTime = TIMER_GetCounter(TIMER1) / 1000;
  83.     printf("    Check DelayTime-1 is %d ms .... ", u32DelayTime);
  84.     if(u32DelayTime == 100)
  85.         printf("PASS.\n");
  86.     else
  87.         printf("FAIL.\n");

  88.     TIMER_ResetCounter(TIMER1);
  89.     TIMER_Delay(TIMER0, 200000);
  90.     u32DelayTime = TIMER_GetCounter(TIMER1) / 1000;
  91.     printf("    Check DelayTime-2 is %d ms .... ", u32DelayTime);
  92.     if(u32DelayTime == 200)
  93.         printf("PASS.\n");
  94.     else
  95.         printf("FAIL.\n");

  96.     TIMER_ResetCounter(TIMER1);
  97.     TIMER_Delay(TIMER0, 300000);
  98.     u32DelayTime = TIMER_GetCounter(TIMER1) / 1000;
  99.     printf("    Check DelayTime-3 is %d ms .... ", u32DelayTime);
  100.     if(u32DelayTime == 300)
  101.         printf("PASS.\n");
  102.     else
  103.         printf("FAIL.\n");

  104.     TIMER_ResetCounter(TIMER1);
  105.     TIMER_Delay(TIMER0, 400000);
  106.     u32DelayTime = TIMER_GetCounter(TIMER1) / 1000;
  107.     printf("    Check DelayTime-4 is %d ms .... ", u32DelayTime);
  108.     if(u32DelayTime == 400)
  109.         printf("PASS.\n");
  110.     else
  111.         printf("FAIL.\n");

  112.     TIMER_ResetCounter(TIMER1);
  113.     TIMER_Delay(TIMER0, 500000);
  114.     u32DelayTime = TIMER_GetCounter(TIMER1) / 1000;
  115.     printf("    Check DelayTime-5 is %d ms .... ", u32DelayTime);
  116.     if(u32DelayTime == 500)
  117.         printf("PASS.\n");
  118.     else
  119.         printf("FAIL.\n");

  120.     printf("\n*** Check TIMER_Delay API delay time done ***\n");

  121.     while(1);
  122. }

  123. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| zhuotuzi 发表于 2020-6-23 09:02 | 显示全部楼层
库函数已经帮你实现了,非常方便
antusheng 发表于 2020-6-23 16:59 | 显示全部楼层
前来学习。。。。。。
稳稳の幸福 发表于 2020-6-23 23:23 | 显示全部楼层
这种延时方法比较精准。
dongnanxibei 发表于 2020-6-24 11:43 | 显示全部楼层
用定时器做比较方便,毕竟比51快多了,跑空指令不现实。
稳稳の幸福 发表于 2020-6-24 20:53 | 显示全部楼层
定时器还是很准的。
幸福小强 发表于 2020-6-24 21:08 | 显示全部楼层
029好像性价比很高的。
余三水 发表于 2020-6-28 16:47 | 显示全部楼层
定时器写延时函数也是不错的,不过就是会占用一点资源,对于定时器紧张的场合,有点浪费。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

214

主题

3375

帖子

7

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