int Timer_FreeCountingMode(void) { int volatile i; /* Init System, IP clock and multi-function I/O In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register if necessary */ SysInit(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); printf("\nThis sample code demonstrate timer free counting mode.\n"); printf("Please connect input source with Timer 0 capture pin PD.11, press any key to continue\n"); UART_GeyChar(); // Give a dummy target frequency here. Will over write capture resolution with macro TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000000); // Update prescale to set proper resolution. // Timer 0 clock source is 12MHz, to set resolution to 1us, we need to // set clock divider to 12. e.g. set prescale to 12 - 1 = 11 TIMER_SET_PRESCALE_VALUE(TIMER0, 11); // Set compare value as large as possible, so don't need to worry about counter overrun too frequently. TIMER_SET_CMP_VALUE(TIMER0, 0xFFFFFF); // Configure Timer 0 free counting mode, capture TDR value on rising edge TIMER_EnableCapture(TIMER0, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_RISING_EDGE); // Start Timer 0 TIMER_Start(TIMER0); // Enable timer interrupt TIMER_EnableCaptureInt(TIMER0); NVIC_EnableIRQ(TMR0_IRQn); while(1); }
/*---------------------------------------------------------------------------------------------------------*/ int main(void) { volatile uint32_t u32InitCount; uint32_t au32CAPValus[10]; /* 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 External Capture Function Sample Code |\n"); printf("+---------------------------------------------------+\n\n"); printf("# Timer Settings:\n"); printf(" Timer0: Clock source is 12 MHz; Toggle-output mode and frequency is 500 Hz.\n"); printf(" Timer3: Clock source is 12 MHz; Toggle-output mode and frequency is 1 Hz.\n"); printf(" Timer2: Clock source is HCLK(72 MHz); Continuous counting mode; TCMP is 0xFFFFFF;\n"); printf(" Counter pin enable; Capture pin and capture interrupt enable;\n"); printf("# Generate 500 Hz frequency from TM0 and connect TM0 pin to Timer2 counter pin.\n"); printf("# Generate 1 Hz frequency from TM3 and connect TM3 pin to TM2_EXT capture pin.\n"); printf("# Get 500 event counts from Timer2 counter pin when each TM2_EXT pin interrupt occurred.\n\n"); /* Initial Timer0 and Timer3 default setting */ TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000); TIMER_Open(TIMER3, TIMER_TOGGLE_MODE, 2); /* Initial Timer2 default setting */ TIMER_Open(TIMER2, TIMER_CONTINUOUS_MODE, 1); /* Configure Timer2 setting for external counter input and capture function */ TIMER_SET_PRESCALE_VALUE(TIMER2, 0); TIMER_SET_CMP_VALUE(TIMER2, 0xFFFFFF); TIMER_EnableEventCounter(TIMER2, TIMER_COUNTER_FALLING_EDGE); TIMER_EnableCapture(TIMER2, TIMER_CAPTURE_FREE_COUNTING_MODE, TIMER_CAPTURE_FALLING_EDGE); TIMER_EnableCaptureInt(TIMER2); /* Enable Timer2 NVIC */ NVIC_EnableIRQ(TMR2_IRQn); /* Clear Timer2 interrupt counts to 0 */ u32InitCount = g_au32TMRINTCount[2] = 0; /* Start Timer0, Timer2 and Timer3 counting */ TIMER_Start(TIMER0); TIMER_Start(TIMER2); TIMER_Start(TIMER3); /* Check TM2_EXT interrupt counts */ while(1) { if(g_au32TMRINTCount[2] != u32InitCount) { au32CAPValus[u32InitCount] = TIMER_GetCaptureData(TIMER2); printf("[%2d] - %4d\n", g_au32TMRINTCount[2], au32CAPValus[u32InitCount]); if(u32InitCount > 1) { if((au32CAPValus[u32InitCount] - au32CAPValus[u32InitCount - 1]) != 500) { printf("*** FAIL ***\n"); while(1); } } u32InitCount = g_au32TMRINTCount[2]; } if(u32InitCount == 10) break; } /* Stop Timer0, Timer2 and Timer3 counting */ TIMER_Close(TIMER0); TIMER_Close(TIMER2); TIMER_Close(TIMER3); printf("*** PASS ***\n"); while(1); }