- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V3.00
- * [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrates the timer event counter function.
- *
- * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
- ******************************************************************************/
- #include <stdio.h>
- #include "NuMicro.h"
- /**
- * @brief Timer0 IRQ
- *
- * @param None
- *
- * [url=home.php?mod=space&uid=266161]@return[/url] None
- *
- * [url=home.php?mod=space&uid=1543424]@Details[/url] The Timer0 default IRQ, declared in startup_M261.s.
- */
- void TMR0_IRQHandler(void)
- {
- printf("Count 1000 falling events! Test complete.\n");
- TIMER_ClearIntFlag(TIMER0);
- }
- 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 PLL */
- CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
- /* Waiting for PLL stable */
- CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
- /* Select HCLK clock source as PLL and HCLK source divider as 2 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));
- /* Set SysTick source to HCLK/2 */
- CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
- /* Enable TIMER module clock */
- CLK_EnableModuleClock(TMR0_MODULE);
- CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set multi-function pins for UART0 RXD and TXD */
- SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
- /* Set Timer0 event counter pin */
- SYS->GPB_MFPL &= ~TM0_PB5_Msk;
- SYS->GPB_MFPL |= TM0_PB5;
- }
- void UART_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART module */
- SYS_ResetModule(UART0_RST);
- /* Configure UART and set UART Baudrate */
- UART_Open(DEBUG_PORT, 115200);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* MAIN function */
- /*---------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- volatile uint32_t i;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Init UART for printf */
- UART_Init();
- /* Lock protected registers */
- SYS_LockReg();
- printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
- printf("+---------------------------------------------+\n");
- printf("| Timer Event Counter Input Sample Code |\n");
- printf("+---------------------------------------------+\n\n");
- printf("# Timer0 Settings:\n");
- printf(" - Clock source is HCLK \n");
- printf(" - Continuous counting mode \n");
- printf(" - Interrupt enable \n");
- printf(" - Event counter mode enable \n");
- printf("# Connect PB.4 pin to event counter pin TM0(PB.5) and pull PB.4 Low to generate TM0 event input source.\n\n");
- printf("Press any key to continue.\n\n");
- getchar();
- /* Configure PB.4 as GPIO output pin and pull initial pin status to High */
- PB->MODE = (PB->MODE & ~GPIO_MODE_MODE4_Msk) | (GPIO_MODE_OUTPUT << GPIO_MODE_MODE4_Pos);
- PB4 = 1;
- /* Give a dummy target frequency here. Will over write prescale and compare value with macro. */
- TIMER_Open(TIMER0, TIMER_ONESHOT_MODE, 100);
- /* Update prescale and compare value to what we need in event counter mode. */
- TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
- TIMER_SET_CMP_VALUE(TIMER0, 1000);
- /* Counter increase on falling edge */
- TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_EVENT_FALLING);
- /* Enable timer interrupt */
- TIMER_EnableInt(TIMER0);
- NVIC_EnableIRQ(TMR0_IRQn);
- /* Start Timer 0 */
- TIMER_Start(TIMER0);
- for(i = 0; i < 1000; i++)
- {
- PB4 = 0; // low
- CLK_SysTickDelay(1);
- PB4 = 1; // high
- CLK_SysTickDelay(1);
- }
- while(1) {}
- }
- /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
|