/******************************************************************************* * Description : PLC_Tx_begin * Syntax : * Parameters I: * Parameters O: * return : *******************************************************************************/ void PLC_Tx_begin(u8 *data) { PLC_Tx_bitCnt = 0u; PLC_Tx_byteCnt = 0u; if (((*data) & 0x80) == 0x80) { TIMER_SET_PRESCALE_VALUE(TIMER1, 0); TIMER_SET_CMP_VALUE(TIMER1, 98); //285KHz(0,98) Freq_cnt = MAX_285K; } else { TIMER_SET_PRESCALE_VALUE(TIMER1, 0); TIMER_SET_CMP_VALUE(TIMER1, 500); //255KHz(0,88) Freq_cnt = MAX_255K; } // digitalWrite(0, HIGH); //P0.0 = 1 SCC // digitalWrite(1, HIGH); //P0.1 = 1 SCCOUT digitalWrite(36, HIGH); //P0.1 = 1 SCCOUT // Freq_cnt = 0; /* Start Timer1 counting */ TIMER_Start(TIMER1); }
int Timer_InterTimerTriggerMode(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); /* This sample code demonstrate inter timer trigger mode using Timer0 and Timer1 * In this mode, Timer0 is working as counter, and triggers Timer1. Using Timer1 * to calculate the amount of time used by Timer0 to count specified amount of events. * By dividing the time period recorded in Timer1 by the event counts, we get * the event frequency. */ printf("Inter timer trigger mode demo code\n"); printf("Please connect input source with Timer 0 counter pin PB.8, press any key to continue\n"); UART_GetChar(); // 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. Calculate average frequency every 1000 events TIMER_SET_PRESCALE_VALUE(TIMER0, 0); TIMER_SET_CMP_VALUE(TIMER0, 1000); // Update Timer 1 prescale value. So Timer 1 clock is 1MHz TIMER_SET_PRESCALE_VALUE(TIMER1, 11); // We need capture interrupt NVIC_EnableIRQ(TMR1_IRQn); while(1) { complete = 0; // Count event by timer 0, disable drop count (set to 0), disable timeout (set to 0). Enable interrupt after complete TIMER_EnableFreqCounter(TIMER0, 0, 0, TRUE); while(complete == 0); } }
/** * @brief Main routine. * @param None. * @return None. */ int32_t main(void) { SYS_UnlockReg(); SYS->P5_MFP = (SYS->P5_MFP & 0x00FFFCFC) | 0x03; /* P5.1 -> XTAL2, P5.0 -> XTAL1 */ CLK->PWRCON = CLK_PWRCON_XTL12M | 4 | 8 ; SYS_Init(); /* SPI test */ LCD_Init(); LCD_EnableBackLight(); LCD_ClearScreen(); LCD_Print(0, "Welcome! Nuvoton"); LCD_Print(1, "This is LB board"); LCD_Print(2, "Mini51"); LCD_Print(3, "TEST"); // backlight control pin P5.4 GPIO_SetMode(P5,1<<4,GPIO_PMD_OUTPUT); /* INT button triggers P3.2 */ GPIO_SetMode(P3,(1<<2),GPIO_PMD_OPEN_DRAIN); GPIO_EnableInt(P3, 2, GPIO_INT_FALLING); NVIC_EnableIRQ(EINT0_IRQn); /* Enable interrupt de-bounce function and select de-bounce sampling cycle time */ GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_HCLK,GPIO_DBNCECON_DBCLKSEL_16); GPIO_ENABLE_DEBOUNCE(P3,1<<2); /* Reset and stop TIMER0, TIMER1 counting first */ TIMER1->TCSR = TIMER_TCSR_CRST_Msk; /* Enable TIMER0, TIMER1, NVIC */ NVIC_EnableIRQ(TMR1_IRQn); /* To Configure TCMPR values based on Timer clock source and pre-scale value */ TIMER_SET_PRESCALE_VALUE(TIMER1,0); /* Start TIMER1 counting and setting*/ TIMER_Open(TIMER1,TIMER_PERIODIC_MODE,SystemCoreClock/1000); TIMER_EnableInt(TIMER1); while(1) ; // loop forever }
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); }
/* ----------------------- Start implementation -----------------------------*/ BOOL xMBPortTimersInit(USHORT usTim1Timerout50us) { uint16_t PrescalerValue = 0; CLK_EnableModuleClock(TMR0_MODULE); CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK, 0); // Set timer frequency to 20KHZ (50us) TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, usTim1Timerout50us); // Enable timer interrupt TIMER_EnableInt(TIMER0); NVIC_SetPriority(TMR0_IRQn, 10 ); //定时器的中断优先级应该比串口的低 NVIC_EnableIRQ(TMR0_IRQn); TIMER_SET_PRESCALE_VALUE(TIMER0, 0); // Stop Timer 0 TIMER_Stop(TIMER0); return TRUE; }
void vMBPortTimersEnable() { TIMER_SET_PRESCALE_VALUE(TIMER0, 0); TIMER_Start(TIMER0); }
void HardwareTimer::setPrescaleFactor(uint32_t factor) { TIMER_SET_PRESCALE_VALUE(dev, factor); }
/*---------------------------------------------------------------------------------------------------------*/ int main(void) { volatile uint32_t u32InitCount; /* 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("| Timer1 External Counter Input Sample Code |\n"); printf("+-------------------------------------------------+\n\n"); printf("# Timer Settings:\n"); printf(" Timer1: Clock source is HCLK(50 MHz); Continuous counting mode; Interrupt enable;\n"); printf(" External counter input enable; TCMP is 56789.\n"); printf("# Connect P2.0 to T1 pin and pull P2.0 High/Low as T1 counter input source.\n\n"); /* Configure P2.0 as GPIO output pin and pull pin status to Low first */ GPIO_SetMode(P2, 0, GPIO_PMD_OUTPUT); P20 = 0; /* Initial Timer1 default setting */ TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1); /* Configure Timer1 setting for external counter input function */ TIMER_SELECT_TOUT_PIN(TIMER1, TIMER_TOUT_PIN_FROM_TX_PIN); TIMER_SET_PRESCALE_VALUE(TIMER1, 0); TIMER_SET_CMP_VALUE(TIMER1, 56789); TIMER_EnableEventCounter(TIMER1, TIMER_COUNTER_FALLING_EDGE); TIMER_EnableInt(TIMER1); /* Enable Timer1 NVIC */ NVIC_EnableIRQ(TMR1_IRQn); /* Clear Timer1 interrupt counts to 0 */ g_au32TMRINTCount[1] = 0; /* Start Timer1 counting */ TIMER_Start(TIMER1); /* To check if TDR of Timer1 must be 0 as default value */ if(TIMER_GetCounter(TIMER1) != 0) { printf("Default counter value is not 0. (%d)\n", TIMER_GetCounter(TIMER1)); /* Stop Timer1 counting */ TIMER_Close(TIMER1); while(1); } /* To generate one counter event to T1 pin */ GeneratePORT2Counter(0, 1); /* To check if TDR of Timer1 must be 1 */ while(TIMER_GetCounter(TIMER1) == 0); if(TIMER_GetCounter(TIMER1) != 1) { printf("Get unexpected counter value. (%d)\n", TIMER_GetCounter(TIMER1)); /* Stop Timer1 counting */ TIMER_Close(TIMER1); while(1); } /* To generate remains counts to T1 pin */ GeneratePORT2Counter(0, (56789 - 1)); while(1) { if((g_au32TMRINTCount[1] == 1) && (TIMER_GetCounter(TIMER1) == 56789)) { printf("Timer1 external counter input function ... PASS.\n"); break; } } /* Stop Timer1 counting */ TIMER_Close(TIMER1); 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); }
/*---------------------------------------------------------------------------------------------------------*/ int main(void) { volatile uint32_t u32InitCount; /* 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("CPU @ %d Hz\n", SystemCoreClock); printf("+--------------------------------------------+\n"); printf("| Timer Periodic Interrupt Sample Code |\n"); printf("+--------------------------------------------+\n\n"); printf("# Timer0 Settings:\n"); printf(" - Clock source is HXT \n"); printf(" - Time-out frequency is 1 Hz\n"); printf(" - Periodic mode \n"); printf(" - Interrupt enable \n"); printf("# Timer1 Settings:\n"); printf(" - Clock source is HCLK \n"); printf(" - Time-out frequency is 2 Hz\n"); printf(" - Periodic mode \n"); printf(" - Interrupt enable \n"); printf("# Timer2 Settings:\n"); printf(" - Clock source is HIRC \n"); printf(" - Time-out frequency is 4 Hz\n"); printf(" - Periodic mode \n"); printf(" - Interrupt enable \n"); printf("# Timer3 Settings:\n"); printf(" - Clock source is HXT \n"); printf(" - Time-out frequency is 8 Hz\n"); printf(" - Periodic mode \n"); printf(" - Interrupt enable \n"); printf("# Check Timer0 ~ Timer3 interrupt counts are reasonable or not.\n\n"); /* Open Timer0 in periodic mode, enable interrupt and 1 interrupt tick per second */ TIMER0->TCMPR = __HXT; TIMER0->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE; TIMER_SET_PRESCALE_VALUE(TIMER0, 0); /* Open Timer1 in periodic mode, enable interrupt and 2 interrupt ticks per second */ TIMER1->TCMPR = ((SystemCoreClock / 4) / 2); TIMER1->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE; TIMER_SET_PRESCALE_VALUE(TIMER1, 3); /* Open Timer2 in periodic mode, enable interrupt and 4 interrupt ticks per second */ TIMER2->TCMPR = ((__HIRC / 1) / 4); TIMER2->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE; TIMER_SET_PRESCALE_VALUE(TIMER2, 0); /* Open Timer3 in periodic mode, enable interrupt and 8 interrupt ticks per second */ TIMER3->TCMPR = ((__HXT / 1) / 8); TIMER3->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE; TIMER_SET_PRESCALE_VALUE(TIMER3, 0); /* Enable Timer0 ~ Timer3 NVIC */ NVIC_EnableIRQ(TMR0_IRQn); NVIC_EnableIRQ(TMR1_IRQn); NVIC_EnableIRQ(TMR2_IRQn); NVIC_EnableIRQ(TMR3_IRQn); /* Clear Timer0 ~ Timer3 interrupt counts to 0 */ g_au32TMRINTCount[0] = g_au32TMRINTCount[1] = g_au32TMRINTCount[2] = g_au32TMRINTCount[3] = 0; u32InitCount = g_au32TMRINTCount[0]; /* Start Timer0 ~ Timer3 counting */ TIMER_Start(TIMER0); TIMER_Start(TIMER1); TIMER_Start(TIMER2); TIMER_Start(TIMER3); /* Check Timer0 ~ Timer3 interrupt counts */ printf("# Timer interrupt counts :\n"); while(u32InitCount < 20) { if(g_au32TMRINTCount[0] != u32InitCount) { printf(" TMR0:%3d TMR1:%3d TMR2:%3d TMR3:%3d\n", g_au32TMRINTCount[0], g_au32TMRINTCount[1], g_au32TMRINTCount[2], g_au32TMRINTCount[3]); u32InitCount = g_au32TMRINTCount[0]; if((g_au32TMRINTCount[1] > (g_au32TMRINTCount[0] * 2 + 1)) || (g_au32TMRINTCount[1] < (g_au32TMRINTCount[0] * 2 - 1)) || (g_au32TMRINTCount[2] > (g_au32TMRINTCount[0] * 4 + 1)) || (g_au32TMRINTCount[2] < (g_au32TMRINTCount[0] * 4 - 1)) || (g_au32TMRINTCount[3] > (g_au32TMRINTCount[0] * 8 + 1)) || (g_au32TMRINTCount[3] < (g_au32TMRINTCount[0] * 8 - 1))) { printf("*** FAIL ***\n"); while(1); } } } printf("*** PASS ***\n"); while(1); }