/*********************************************************************//** * @brief Initial Timer/Counter device * Set Clock frequency for Timer * Set initial configuration for Timer * @param[in] TIMx Timer selection, should be: * - LPC_TIM0 :TIMER0 peripheral * - LPC_TIM1 :TIMER1 peripheral * - LPC_TIM2 :TIMER2 peripheral * - LPC_TIM3 :TIMER3 peripheral * @param[in] TimerCounterMode Timer counter mode, should be: * - TIM_TIMER_MODE :Timer mode * - TIM_COUNTER_RISING_MODE :Counter rising mode * - TIM_COUNTER_FALLING_MODE :Counter falling mode * - TIM_COUNTER_ANY_MODE :Counter on both edges * @param[in] TIM_ConfigStruct pointer to TIM_TIMERCFG_Type * that contains the configuration information for the * specified Timer peripheral. * @return None **********************************************************************/ void TIM_Init(LPC_TIMERn_Type *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct) { TIM_TIMERCFG_Type *pTimeCfg; TIM_COUNTERCFG_Type *pCounterCfg; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode)); //set power if (TIMx== LPC_TIMER0) { } else if (TIMx== LPC_TIMER1) { } else if (TIMx== LPC_TIMER2) { } else if (TIMx== LPC_TIMER3) { } TIMx->CCR &= ~TIM_CTCR_MODE_MASK; TIMx->CCR |= TIM_TIMER_MODE; TIMx->TC =0; TIMx->PC =0; TIMx->PR =0; TIMx->TCR |= (1<<1); //Reset Counter TIMx->TCR &= ~(1<<1); //release reset if (TimerCounterMode == TIM_TIMER_MODE ) { pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct; if (pTimeCfg->PrescaleOption == TIM_PRESCALE_TICKVAL) { TIMx->PR = pTimeCfg->PrescaleValue -1 ; } else { TIMx->PR = converUSecToVal (converPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1; } } else { pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct; TIMx->CCR &= ~TIM_CTCR_INPUT_MASK; if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1) TIMx->CCR |= _BIT(2); } // Clear interrupt pending TIMx->IR = 0xFFFFFFFF; }
/*********************************************************************//** * @brief Initial Timer/Counter device * Set Clock frequency for Timer * Set initial configuration for Timer * @param[in] TIMx Timer selection, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @param[in] TimerCounterMode Timer counter mode, should be: * - TIM_TIMER_MODE: Timer mode * - TIM_COUNTER_RISING_MODE: Counter rising mode * - TIM_COUNTER_FALLING_MODE: Counter falling mode * - TIM_COUNTER_ANY_MODE:Counter on both edges * @param[in] TIM_ConfigStruct pointer to TIM_TIMERCFG_Type * that contains the configuration information for the * specified Timer peripheral. * @return None **********************************************************************/ void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct) { TIM_TIMERCFG_Type *pTimeCfg; TIM_COUNTERCFG_Type *pCounterCfg; //set power if (TIMx== LPC_TIM0) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, ENABLE); } else if (TIMx== LPC_TIM1) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, ENABLE); } else if (TIMx== LPC_TIM2) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, ENABLE); } else if (TIMx== LPC_TIM3) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM3, ENABLE); } TIMx->CTCR &= ~TIM_CTCR_MODE_MASK; TIMx->CTCR |= TIM_TIMER_MODE; TIMx->TC =0; TIMx->PC =0; TIMx->PR =0; TIMx->TCR |= (1<<1); //Reset Counter TIMx->TCR &= ~(1<<1); //release reset if (TimerCounterMode == TIM_TIMER_MODE ) { pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct; if (pTimeCfg->PrescaleOption == TIM_PRESCALE_TICKVAL) { TIMx->PR = pTimeCfg->PrescaleValue -1 ; } else { TIMx->PR = converUSecToVal (converPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1; } } else { pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct; TIMx->CTCR &= ~TIM_CTCR_INPUT_MASK; if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1) TIMx->CCR |= _BIT(2); } // Clear interrupt pending TIMx->IR = 0xFFFFFFFF; }