/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device * @param[in] TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type * - MatchChannel : choose channel 0 or 1 * - IntOnMatch : if SET, interrupt will be generated when MRxx match * the value in TC * - StopOnMatch : if SET, TC and PC will be stopped whenM Rxx match * the value in TC * - ResetOnMatch : if SET, Reset on MR0 when MRxx match * the value in TC * -ExtMatchOutputType: Select output for external match * + 0: Do nothing for external output pin if match * + 1: Force external output pin to low if match * + 2: Force external output pin to high if match * + 3: Toggle external output pin if match * MatchValue: Set the value to be compared with TC value * @return None **********************************************************************/ void TIM_ConfigMatch(TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); uint32_t timer = TIM_ConverPtrToTimeNum(TIMx) ; // TIMx->MR[TIM_MatchConfigStruct->MatchChannel] = TIM_MatchConfigStruct->MatchValue; switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; } //interrupt on MRn TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel); if (TIM_MatchConfigStruct->IntOnMatch) TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //reset on MRn if (TIM_MatchConfigStruct->ResetOnMatch) TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //stop on MRn if (TIM_MatchConfigStruct->StopOnMatch) TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); // match output type TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel); TIMx->EMR = TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType); //pin output configuration if (TIM_MatchConfigStruct->ExtMatchOutputType >0) { if ((timer <2)&& (TIM_MatchConfigStruct->MatchChannel < 2)) { PINSEL_ConfigPin((PINSEL_CFG_Type *)&timer_match_pin[2*timer +TIM_MatchConfigStruct->MatchChannel]); } if ((timer ==2)) { PINSEL_ConfigPin( (PINSEL_CFG_Type *)&timer_match_pin[2*timer + TIM_MatchConfigStruct->MatchChannel]); } if ((timer ==3)&&(TIM_MatchConfigStruct->MatchChannel < 2)) { PINSEL_ConfigPin( (PINSEL_CFG_Type *)&timer_match_pin[4*timer + TIM_MatchConfigStruct->MatchChannel]); } } }
/*********************************************************************//** * @brief Configuration for Capture register * @param[in] TIMx Pointer to timer device * - CaptureChannel: set the channel to capture data * - RisingEdge : if SET, Capture at rising edge * - FallingEdge : if SET, Capture at falling edge * - IntOnCaption : if SET, Capture generate interrupt * @param[in] TIM_CaptureConfigStruct Pointer to TIM_CAPTURECFG_Type * @return None **********************************************************************/ void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct) { uint32_t timer; CHECK_PARAM(PARAM_TIMx(TIMx)); timer = TIM_ConverPtrToTimeNum(TIMx) ; TIMx->CCR &= ~TIM_CCR_CHANNEL_MASKBIT(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->RisingEdge) TIMx->CCR |= TIM_CAP_RISING(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->FallingEdge) TIMx->CCR |= TIM_CAP_FALLING(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->IntOnCaption) TIMx->CCR |= TIM_INT_ON_CAP(TIM_CaptureConfigStruct->CaptureChannel); }
/*********************************************************************//** * @brief Configuration for Capture register * @param[in] TIMx Pointer to timer device * - CaptureChannel: set the channel to capture data * - RisingEdge : if SET, Capture at rising edge * - FallingEdge : if SET, Capture at falling edge * - IntOnCaption : if SET, Capture generate interrupt * @param[in] TIM_CaptureConfigStruct Pointer to TIM_CAPTURECFG_Type * @return None **********************************************************************/ void TIM_ConfigCapture(TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct) { CHECK_PARAM(PARAM_TIMx(TIMx)); uint32_t timer = TIM_ConverPtrToTimeNum(TIMx) ; TIMx->CCR &= ~TIM_CCR_CHANNEL_MASKBIT(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->RisingEdge) TIMx->CCR |= TIM_CAP_RISING(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->FallingEdge) TIMx->CCR |= TIM_CAP_FALLING(TIM_CaptureConfigStruct->CaptureChannel); if (TIM_CaptureConfigStruct->IntOnCaption) TIMx->CCR |= TIM_INT_ON_CAP(TIM_CaptureConfigStruct->CaptureChannel); // set pin caption input PINSEL_ConfigPin((PINSEL_CFG_Type *)&(timer_caption_pin[2*timer + TIM_CaptureConfigStruct->CaptureChannel])); }
/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device * @param[in] TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type * - MatchChannel : choose channel 0 or 1 * - IntOnMatch : if SET, interrupt will be generated when MRxx match * the value in TC * - StopOnMatch : if SET, TC and PC will be stopped whenM Rxx match * the value in TC * - ResetOnMatch : if SET, Reset on MR0 when MRxx match * the value in TC * -ExtMatchOutputType: Select output for external match * + 0: Do nothing for external output pin if match * + 1: Force external output pin to low if match * + 2: Force external output pin to high if match * + 3: Toggle external output pin if match * MatchValue: Set the value to be compared with TC value * @return None **********************************************************************/ void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct) { uint32_t timer; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); timer = TIM_ConverPtrToTimeNum(TIMx) ; switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; } //interrupt on MRn TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel); if (TIM_MatchConfigStruct->IntOnMatch) TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //reset on MRn if (TIM_MatchConfigStruct->ResetOnMatch) TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //stop on MRn if (TIM_MatchConfigStruct->StopOnMatch) TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); // TIMx->MCR = 0x02; // match output type TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel); TIMx->EMR = TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType); }
/*********************************************************************//** * @brief Initial Timer/Counter device * Set Clock frequency for Timer * Set initial configuration for Timer * @param[in] TIMx Timer selection, should be TIM0, TIM1, TIM2, TIM3 * @param[in] TimerCounterMode TIM_MODE_OPT * @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, uint8_t TimerCounterMode, void *TIM_ConfigStruct) { TIM_TIMERCFG_Type *pTimeCfg; TIM_COUNTERCFG_Type *pCounterCfg; uint32_t timer; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode)); timer = TIM_ConverPtrToTimeNum(TIMx) ; //set power if (TIMx== LPC_TIM0) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER0, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== LPC_TIM1) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER1, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== LPC_TIM2) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER2, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== LPC_TIM3) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM3, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER3, CLKPWR_PCLKSEL_CCLK_DIV_4); } //TIMx->CCR &= ~TIM_CTCR_MODE_MASK; //TIMx->CCR |= TIM_TIMER_MODE; TIMx->CTCR &= ~TIM_CTCR_MODE_MASK; TIMx->CTCR |= TimerCounterMode; TIMx->TC =0; TIMx->PC =0; TIMx->PR =0; 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 = TIM_ConverUSecToVal (TIM_ConverPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1; } } else { pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct; //TIMx->CCR &= ~TIM_CTCR_INPUT_MASK; TIMx->CTCR &= ~TIM_CTCR_INPUT_MASK; if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1) TIMx->CCR |= _BIT(2); } // Clear interrupt pending TIMx->IR = 0xFFFFFFFF; }