完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/**************************************************************************//**
* @file main.c * @version V3.00 * $Revision: 2 $ * $Date: 16/10/25 4:30p $ * @brief Show how to use timer0 to create various delay time. * @note * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved. ******************************************************************************/ #include #include "NUC029xGE.h" #define PLL_CLOCK 72000000 void SYS_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable HIRC clock */ CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk); /* Waiting for HIRC clock ready */ CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk); /* Switch HCLK clock source to HIRC */ CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1)); /* Enable HXT */ CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk); /* Waiting for clock ready */ CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk); /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/ CLK_SetCoreClock(PLL_CLOCK); CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2); /* Enable peripheral clock */ CLK_EnableModuleClock(UART0_MODULE); CLK_EnableModuleClock(TMR0_MODULE); CLK_EnableModuleClock(TMR1_MODULE); /* Peripheral clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_PLL, CLK_CLKDIV0_UART(1)); CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0); CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_HXT, 0); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set multi-function pins for UART0 RXD and TXD */ SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk); SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD); } void UART0_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ /* Reset UART module */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------------------------------*/ int main(void) { volatile uint32_t u32DelayTime; /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, peripheral clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); printf("\n\nCPU @ %d Hz\n", SystemCoreClock); printf("+-----------------------------------+\n"); printf("| Timer Delay API Sample Code |\n"); printf("+-----------------------------------+\n\n"); printf("# This sample code is using Timer1 to check Timer0 TIMER_Delay API delay time is reasonable or not.\n"); printf("# Delay time includes 100 ms, 200 ms, 300 ms, 400 ms and 500 ms.\n\n"); /* Start Timer1 to measure delay period of TIMER_Delay API is reasonable or not */ TIMER1->CTL = TIMER_PERIODIC_MODE | (12 - 1); TIMER_ResetCounter(TIMER1); TIMER_Start(TIMER1); TIMER_ResetCounter(TIMER1); TIMER_Delay(TIMER0, 100000); u32DelayTime = TIMER_GetCounter(TIMER1) / 1000; printf(" Check DelayTime-1 is %d ms .... ", u32DelayTime); if(u32DelayTime == 100) printf("PASS.\n"); else printf("FAIL.\n"); TIMER_ResetCounter(TIMER1); TIMER_Delay(TIMER0, 200000); u32DelayTime = TIMER_GetCounter(TIMER1) / 1000; printf(" Check DelayTime-2 is %d ms .... ", u32DelayTime); if(u32DelayTime == 200) printf("PASS.\n"); else printf("FAIL.\n"); TIMER_ResetCounter(TIMER1); TIMER_Delay(TIMER0, 300000); u32DelayTime = TIMER_GetCounter(TIMER1) / 1000; printf(" Check DelayTime-3 is %d ms .... ", u32DelayTime); if(u32DelayTime == 300) printf("PASS.\n"); else printf("FAIL.\n"); TIMER_ResetCounter(TIMER1); TIMER_Delay(TIMER0, 400000); u32DelayTime = TIMER_GetCounter(TIMER1) / 1000; printf(" Check DelayTime-4 is %d ms .... ", u32DelayTime); if(u32DelayTime == 400) printf("PASS.\n"); else printf("FAIL.\n"); TIMER_ResetCounter(TIMER1); TIMER_Delay(TIMER0, 500000); u32DelayTime = TIMER_GetCounter(TIMER1) / 1000; printf(" Check DelayTime-5 is %d ms .... ", u32DelayTime); if(u32DelayTime == 500) printf("PASS.\n"); else printf("FAIL.\n"); printf("\n*** Check TIMER_Delay API delay time done ***\n"); while(1); } /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/ |
|
相关推荐
1个回答
|
|
include "NUC029xGE.h"
#define DELAY_TIME 1000 // delay time in milliseconds #define TIMER_CLKSRC_HXT 0x0 // Timer clock source is HXT void TMR0_IRQHandler(void) { TIMER_ClearIntFlag(TIMER0); // clear timer interrupt flag } void delay(uint32_t u32DelayTime) { uint32_t u32Prescale, u32LoadValue; // select timer clock source and set prescaler CLK->CLKSEL1 |= CLK_CLKSEL1_TMR0_S_HXT; u32Prescale = 1; // calculate load value to achieve desired delay time u32LoadValue = (u32DelayTime * (SystemCoreClock / u32Prescale) / 1000); // enable timer0 and set reload value TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, u32LoadValue); TIMER_Start(TIMER0); // wait for timer interrupt NVIC_EnableIRQ(TMR0_IRQn); while (!TIMER_GetIntFlag(TIMER0)); // disable and reset timer0 TIMER_Stop(TIMER0); TIMER_ClearIntFlag(TIMER0); TIMER_Close(TIMER0); NVIC_DisableIRQ(TMR0_IRQn); } int main() { SYS_Init(); delay(DELAY_TIME); return 0; } |
|
|
|
只有小组成员才能发言,加入小组>>
930 浏览 1 评论
2321 浏览 5 评论
2637 浏览 9 评论
移植了freeRTOS到STMf103之后显示没有定义的原因?
2445 浏览 6 评论
2348 浏览 7 评论
使用eim外接fpga可是端口一点反应都没有有没有大哥指点一下啊
492浏览 9评论
504浏览 7评论
请教大神怎样去解决iMX6Q在linux3.0.35内核上做AP失败的问题呢
605浏览 6评论
483浏览 5评论
518浏览 5评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-30 22:25 , Processed in 1.028922 second(s), Total 83, Slave 63 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号