/******************************************************************************* * Description : PLC Tx ISR * Syntax : * Parameters I: * Parameters O: * return : *******************************************************************************/ void TMR1_IRQHandler(void) { u8 temp_bit = 0u; // if(TIMER_GetIntFlag(TIMER1) == 1) if ((TIMER1->TISR & TIMER_TISR_TIF_Msk) == 1) { /* Clear Timer1 time-out interrupt flag */ //TIMER_ClearIntFlag(TIMER1); TIMER1->TISR = TIMER_TISR_TIF_Msk; // TimerIntCnt[1]++; Freq_cnt--; // Freq_cnt ^= 1; P36 ^= HIGH; //P0.1 = 1 SCCOUT /* Tx running */ if (Freq_cnt < 0) { PLC_Tx_bitCnt++; if (PLC_Tx_bitCnt >= 8u) { PLC_Tx_bitCnt = 0u; PLC_Tx_byteCnt++; } temp_bit = (PLC_Tx_PN9[PLC_Tx_byteCnt] << PLC_Tx_bitCnt) & 0x80; if (0x80 == temp_bit) { // 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; } } else { } /* check Tx ending */ if (PLC_Tx_byte <= PLC_Tx_byteCnt) { /* Stop Timer1 counting */ TIMER_Close(TIMER1); P36 = 0; //P0.0 = 0 } } }
/* void HardwareTimer::initialize(uint32_t microseconds){ //open(PERIODIC, 1000000 / microseconds); uint32_t u32ClkInMHz = 0; u32ClkInMHz = getModuleClock() / 1000000; //dev->TCSR = PERIODIC | (u32ClkInMHz - 1); // dev->TCSR = (dev->TCSR & ~TIMER_TCSR_PRESCALE_Msk)| PERIODIC |(u32ClkInMHz - 1); //setCompare(microseconds); dev->TCMPR = microseconds;// range - 2us - 16,777,216 us } */ void HardwareTimer::close() { TIMER_Close(dev); }
//========================================================================= // Additional initialisations //------------------------------------------------------------------------- __myevic__ void CustomStartup() { //------------------------------------------------------------------------- // EADC test if ( 0 ) { uint32_t s1, s2, s3; SetADCState( 0, 1 ); SetADCState( 4, 1 ); do { ClearScreenBuffer(); CLK_SysTickDelay( 10 ); s3 = ADC_Read( 4 ); CLK_SysTickDelay( 10 ); s1 = ADC_Read( 18 ); CLK_SysTickDelay( 10 ); s2 = ADC_Read( 0 ); DrawValue( 8, 0, s1, 0, 0x29, 4 ); DrawValue( 8, 20, s2, 0, 0x29, 4 ); DrawValue( 8, 40, s3, 0, 0x29, 4 ); DisplayRefresh(); WaitOnTMR2( 1000 ); } while ( PD3 ); } //------------------------------------------------------------------------- // Timer test 1 if ( 0 ) { TIMER_Stop( TIMER3 ); TIMER_Close( TIMER3 ); MemClear( gPlayfield.uc, 256 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 ); gPlayfield.ul[1] = TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 ); TIMER_EnableInt( TIMER3 ); TIMER_Start( TIMER3 ); } //------------------------------------------------------------------------- // Timer test 2 if ( 0 ) { TIMER_Close( TIMER2 ); TIMER_Close( TIMER3 ); MemClear( gPlayfield.uc, 256 ); CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HXT, 0 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_LIRC, 0 ); CLK_EnableModuleClock( TMR2_MODULE ); CLK_EnableModuleClock( TMR3_MODULE ); __set_PRIMASK(1); TIMER3->CTL |= TIMER_CTL_RSTCNT_Msk; TIMER2->CTL |= TIMER_CTL_RSTCNT_Msk; TIMER3->CMP = 1000; TIMER3->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE; TIMER2->CTL = TIMER_CTL_CNTEN_Msk | TIMER_CONTINUOUS_MODE; while(!(TIMER3->INTSTS & TIMER_INTSTS_TIF_Msk)); TIMER2->CTL = 0; gPlayfield.ul[0] = TIMER2->CNT; __set_PRIMASK(0); TIMER_Close( TIMER2 ); TIMER_Close( TIMER3 ); CLK_SetModuleClock( TMR2_MODULE, CLK_CLKSEL1_TMR2SEL_HIRC, 0 ); CLK_EnableModuleClock( TMR2_MODULE ); TIMER_Open( TIMER2, TIMER_PERIODIC_MODE, 1000 ); TIMER_EnableInt( TIMER2 ); TIMER_Start( TIMER2 ); CLK_SetModuleClock( TMR3_MODULE, CLK_CLKSEL1_TMR3SEL_HXT, 0 ); CLK_EnableModuleClock( TMR3_MODULE ); TIMER_Open( TIMER3, TIMER_PERIODIC_MODE, 10 ); TIMER_EnableInt( TIMER3 ); TIMER_Start( TIMER3 ); } return; }
/*---------------------------------------------------------------------------------------------------------*/ 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); }
/******************************************************************************* * Description : PLC_Tx_begin * Syntax : * Parameters I: * Parameters O: * return : *******************************************************************************/ void PLC_Tx_end(void) { /* Stop Timer1 counting */ TIMER_Close(TIMER1); }