/*********************************************************************//** * @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 Close Timer/Counter device * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0 :TIMER0 peripheral * - LPC_TIM1 :TIMER1 peripheral * - LPC_TIM2 :TIMER2 peripheral * - LPC_TIM3 :TIMER3 peripheral * @return None **********************************************************************/ void TIM_DeInit (LPC_TIMERn_Type *TIMx) { CHECK_PARAM(PARAM_TIMx(TIMx)); // Disable timer/counter TIMx->TCR = 0x00; }
/*********************************************************************//** * @brief Clear Interrupt pending * @param[in] TIMx Timer selection, should be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1 * @param[in] IntFlag should be in TIM_INT_TYPE enum * @return None **********************************************************************/ void TIM_ClearIntPending(TMR_TypeDef *TIMx, uint8_t IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); TIMx->IR = TIM_IR_CLR(IntFlag) ; }
/*********************************************************************//** * @brief Start/Stop Timer/Counter device * @param[in] TIMx Pointer to timer device, should be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1 * @param[in] NewState * - ENABLE : set timer enable * - DISABLE : disable timer * @return None **********************************************************************/ void TIM_Cmd(TMR_TypeDef *TIMx, FunctionalState NewState) { CHECK_PARAM(PARAM_TIMx(TIMx)); if (NewState == ENABLE) { TIMx->TCR |= TIM_ENABLE; } else { TIMx->TCR &= ~TIM_ENABLE; } }
/*********************************************************************//** * @brief Get Capture Interrupt Status * @param[in] TIMx Timer selection, should be TIM0, TIM1, TIM2, TIM3 * @param[in] IntFlag * @return FlagStatus * - SET : interrupt * - RESET : no interrupt **********************************************************************/ FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, uint8_t IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); uint8_t temp = (TIMx->IR) & (1<<(4+IntFlag)); if(temp) return SET; return RESET; }
/*********************************************************************//** * @brief Get Capture Interrupt Status * @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] IntFlag: interrupt type, should be: * - TIM_MR0_INT: Interrupt for Match channel 0 * - TIM_MR1_INT: Interrupt for Match channel 1 * - TIM_MR2_INT: Interrupt for Match channel 2 * - TIM_MR3_INT: Interrupt for Match channel 3 * - TIM_CR0_INT: Interrupt for Capture channel 0 * - TIM_CR1_INT: Interrupt for Capture channel 1 * @return FlagStatus * - SET : interrupt * - RESET : no interrupt **********************************************************************/ FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag) { uint8_t temp; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); temp = (TIMx->IR) & (1<<(4+IntFlag)); if(temp) return (SET); return (RESET); }
/*********************************************************************//** * @brief Read value of capture register in timer/counter device * @param[in] TIMx Pointer to timer/counter device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @param[in] CaptureChannel: capture channel number, should be: * - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn * - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn * @return Value of capture register **********************************************************************/ uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel)); if(CaptureChannel==0) return TIMx->CR0; else return TIMx->CR1; }
/*********************************************************************//** * @brief Initial Timer/Counter device * Set Clock frequency for ADC * Set initial configuration for ADC * @param[in] TIMx Timer selection, should be LPC_TMR16B0, LPC_TMR16B1 * LPC_TMR32B0, LPC_TMR32B1 * @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(TMR_TypeDef *TIMx, uint8_t TimerCounterMode, void *TIM_ConfigStruct) { TIM_TIMERCFG_Type *pTimeCfg; TIM_COUNTERCFG_Type *pCounterCfg; uint32_t val; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode)); // Set power if(TIMx == LPC_TMR16B0) { SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT16B0, ENABLE); } else if(TIMx == LPC_TMR16B1) { SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT16B1, ENABLE); } else if(TIMx == LPC_TMR32B0) { SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT32B0, ENABLE); } else if(TIMx == LPC_TMR32B1) { SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT32B1, ENABLE); } TIMx->CCR &= ~TIM_CTCR_MODE_MASK; TIMx->CCR |= 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) { val = pTimeCfg->PrescaleValue -1 ; } else { val = TIM_ConverUSecToVal (pTimeCfg->PrescaleValue)-1; } if ((TIMx == LPC_TMR16B0) || (TIMx == LPC_TMR16B1)) { val &= 0xFFFF; } TIMx->PR = val; } 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 = 0x3F; }
/*********************************************************************//** * @brief Get Interrupt Status * @param[in] TIMx Timer selection, should be TIM0, TIM1, TIM2, TIM3 * @param[in] IntFlag * @return FlagStatus * - SET : interrupt * - RESET : no interrupt **********************************************************************/ FlagStatus TIM_GetIntStatus(TIM_TypeDef *TIMx, uint8_t IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); uint8_t temp = (TIMx->IR)& TIM_IR_CLR(IntFlag); if (temp) return SET; return RESET; }
/*********************************************************************//** * @brief Get Interrupt Status * @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] IntFlag: interrupt type, should be: * - TIM_MR0_INT :Interrupt for Match channel 0 * - TIM_MR1_INT :Interrupt for Match channel 1 * - TIM_MR2_INT :Interrupt for Match channel 2 * - TIM_MR3_INT :Interrupt for Match channel 3 * - TIM_CR0_INT :Interrupt for Capture channel 0 * - TIM_CR1_INT :Interrupt for Capture channel 1 * @return FlagStatus * - SET :interrupt * - RESET :no interrupt **********************************************************************/ FlagStatus TIM_GetIntStatus(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag) { uint8_t temp; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); temp = (TIMx->IR)& TIM_IR_CLR(IntFlag); if (temp) return SET; return RESET; }
/*********************************************************************//** * @brief Read value of capture register in timer/counter device * @param[in] TIMx Pointer to timer/counter device, should be: * - LPC_TIM0 :TIMER0 peripheral * - LPC_TIM1 :TIMER1 peripheral * - LPC_TIM2 :TIMER2 peripheral * - LPC_TIM3 :TIMER3 peripheral * @param[in] CaptureChannel: capture channel number, should be: * - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn * - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn * - TIM_COUNTER_INCAP1: CAPn.2 input pin for TIMERn * - TIM_COUNTER_INCAP1: CAPn.3 input pin for TIMERn * @return Value of capture register **********************************************************************/ uint32_t TIM_GetCaptureValue(LPC_TIMERn_Type *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel)); switch(CaptureChannel){ case 0: return TIMx->CR[0]; case 1: return TIMx->CR[1]; case 2: return TIMx->CR[2]; case 3: return TIMx->CR[3]; } return 0; }
/*********************************************************************//** * @brief Read value of capture register in timer/counter device * @param[in] TIMx Pointer to timer/counter device, can be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1 * @return Value of capture register **********************************************************************/ uint32_t TIM_GetCaptureValue(TMR_TypeDef *TIMx) { uint32_t val; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel)); val = TIMx->CR0; if((TIMx == LPC_TMR16B0) || (TIMx == LPC_TMR16B1)) { val &= 0xFFFF; } return val; }
/*********************************************************************//** * @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, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * - 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) { CHECK_PARAM(PARAM_TIMx(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 Assign Capture Signals * - CT16B0_CAP0 : PIO0_2 * - CT16B1_CAP0 : PIO1_8 * - CT32B0_CAP0 : PIO1_5 * - CT32B1_CAP0 : PIO1_0 * @param[in] TIMx Pointer to timer/counter device, can be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1 * @return None **********************************************************************/ void TIM_CapturePins(TMR_TypeDef *TIMx) { CHECK_PARAM(PARAM_TIMx(TIMx)); if (TIMx == LPC_TMR16B0) { /** CT16B0_CAP0 */ IOCON_SetPinFunc(IOCON_PIO0_2, PIO0_2_FUN_CT16B0_CAP0); } else if (TIMx == LPC_TMR16B1) { /** CT16B1_CAP0 */ IOCON_SetPinFunc(IOCON_PIO1_8, PIO1_8_FUN_CT16B1_CAP0); } else if (TIMx == LPC_TMR32B0) { /** CT32B0_CAP0 */ IOCON_SetPinFunc(IOCON_PIO1_5, PIO1_5_FUN_CT32B0_CAP0); } else if (TIMx == LPC_TMR32B1) { /** CT32B1_CAP0 */ IOCON_SetPinFunc(IOCON_PIO1_0, PIO1_0_FUN_CT32B1_CAP0); } }
/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @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) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; case 2: TIMx->MR2 = TIM_MatchConfigStruct->MatchValue; break; case 3: TIMx->MR3 = TIM_MatchConfigStruct->MatchValue; break; default: //Error match value //Error loop while(1); 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); }
/*********************************************************************//** * @brief Close Timer/Counter device * @param[in] TIMx Pointer to timer device, should be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1 * @return None **********************************************************************/ void TIM_DeInit (TMR_TypeDef *TIMx) { CHECK_PARAM(PARAM_TIMx(TIMx)); // Disable timer/counter TIMx->TCR = 0x00; // Disable power if (TIMx == LPC_TMR16B0) { // 16-bit counter/timer 0 SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT16B0, DISABLE); } else if(TIMx == LPC_TMR16B1) { // 16-bit counter/timer 1 SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT16B1, DISABLE); } else if(TIMx == LPC_TMR32B0) { // 32-bit counter/timer 0 SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT32B0, DISABLE); } else if(TIMx == LPC_TMR32B1) { // 32-bit counter/timer 1 SYSCON_AHBPeriphClockCmd(SYSCON_AHBPeriph_CT32B1, DISABLE); } }
/*********************************************************************//** * @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 Close Timer/Counter device * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @return None **********************************************************************/ void TIM_DeInit (LPC_TIM_TypeDef *TIMx) { CHECK_PARAM(PARAM_TIMx(TIMx)); // Disable timer/counter TIMx->TCR = 0x00; // Disable power if (TIMx== LPC_TIM0) CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, DISABLE); else if (TIMx== LPC_TIM1) CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, DISABLE); else if (TIMx== LPC_TIM2) CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, DISABLE); else if (TIMx== LPC_TIM3) CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, DISABLE); }
/*********************************************************************//** * @brief Update Match value * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @param[in] MatchChannel Match channel, should be: 0..3 * @param[in] MatchValue updated match value * @return None **********************************************************************/ void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue) { CHECK_PARAM(PARAM_TIMx(TIMx)); switch(MatchChannel) { case 0: TIMx->MR0 = MatchValue; break; case 1: TIMx->MR1 = MatchValue; break; case 2: TIMx->MR2 = MatchValue; break; case 3: TIMx->MR3 = MatchValue; break; default: //Error Loop while(1); } }
/*********************************************************************//** * @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 Reset Timer/Counter device, * Make TC and PC are synchronously reset on the next * positive edge of PCLK * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @return None **********************************************************************/ void TIM_ResetCounter(LPC_TIM_TypeDef *TIMx) { CHECK_PARAM(PARAM_TIMx(TIMx)); TIMx->TCR |= TIM_RESET; TIMx->TCR &= ~TIM_RESET; }
/*********************************************************************//** * @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; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode)); //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->CTCR &= ~TIM_CTCR_MODE_MASK; TIMx->CTCR |= TimerCounterMode; 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; }
/*********************************************************************//** * @brief Clear Capture Interrupt pending * @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] IntFlag interrupt type, should be: * - TIM_MR0_INT: Interrupt for Match channel 0 * - TIM_MR1_INT: Interrupt for Match channel 1 * - TIM_MR2_INT: Interrupt for Match channel 2 * - TIM_MR3_INT: Interrupt for Match channel 3 * - TIM_CR0_INT: Interrupt for Capture channel 0 * - TIM_CR1_INT: Interrupt for Capture channel 1 * @return None **********************************************************************/ void TIM_ClearIntCapturePending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); TIMx->IR |= (1<<IntFlag); }
/*********************************************************************//** * @brief Clear Interrupt pending * @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] IntFlag: interrupt type, should be: * - TIM_MR0_INT: Interrupt for Match channel 0 * - TIM_MR1_INT: Interrupt for Match channel 1 * - TIM_MR2_INT: Interrupt for Match channel 2 * - TIM_MR3_INT: Interrupt for Match channel 3 * - TIM_CR0_INT: Interrupt for Capture channel 0 * - TIM_CR1_INT: Interrupt for Capture channel 1 * @return None **********************************************************************/ void TIM_ClearIntPending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); TIMx->IR |= TIM_IR_CLR(IntFlag); }
/*********************************************************************//** * @brief Assign External Match Output Pins * @param[in] TIMx Pointer to timer/counter device, can be LPC_TMR16B0, * LPC_TMR16B1, LPC_TMR32B0, LPC_TMR32B1, pins assgign: * 16-bit Counter/Timer 0 * - MAT0 : PIO0_8 * - MAT1 : PIO0_9 * - MAT2 : PIO0_10 * - MAT3 : None pin out * 16-bit Counter/Timer 1 * - MAT0 : PIO1_9 * - MAT1 : PIO1_10 * - MAT2 : None pin out * - MAT3 : None pin out * 32-bit Counter/Timer 0 * - MAT0 : PIO1_6 * - MAT1 : PIO1_7 * - MAT2 : PIO0_1 * - MAT3 : PIO0_11 * 32-bit Counter/Timer 1 * - MAT0 : PIO1_1 * - MAT1 : PIO1_2 * - MAT2 : PIO1_3 * - MAT3 : PIO1_4 * @param[in] matFlag, external Match Output, can be: * - TIM_PINS_MAT0 : External Match Output 0 * - TIM_PINS_MAT1 : External Match Output 1 * - TIM_PINS_MAT2 : External Match Output 2 * - TIM_PINS_MAT3 : External Match Output 3 * @return None **********************************************************************/ void TIM_MatchPins(TMR_TypeDef *TIMx, uint8_t matFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MAT_PINS(matFlag)); // 16-bit counter/timer 0 external Match Output if (TIMx == LPC_TMR16B0) { switch(matFlag) { case TIM_PINS_MAT0: IOCON_SetPinFunc(IOCON_PIO0_8, PIO0_8_FUN_CT16B0_MAT0); break; case TIM_PINS_MAT1: IOCON_SetPinFunc(IOCON_PIO0_9, PIO0_9_FUN_CT16B0_MAT1); break; case TIM_PINS_MAT2: IOCON_SetPinFunc(IOCON_PIO0_10, PIO0_10_FUN_CT16B0_MAT2); break; case TIM_PINS_MAT3: break; } // 16-bit counter/timer 1 external Match Output } else if (TIMx == LPC_TMR16B1) { switch(matFlag) { case TIM_PINS_MAT0: IOCON_SetPinFunc(IOCON_PIO1_9, PIO1_9_FUN_CT16B1_MAT0); break; case TIM_PINS_MAT1: IOCON_SetPinFunc(IOCON_PIO1_10, PIO1_10_FUN_CT16B1_MAT1); break; case TIM_PINS_MAT2: case TIM_PINS_MAT3: break; } // 32-bit counter/timer 0 external Match Output } else if (TIMx == LPC_TMR32B0) { switch(matFlag) { case TIM_PINS_MAT0: IOCON_SetPinFunc(IOCON_PIO1_6, PIO1_6_FUN_CT32B0_MAT0); break; case TIM_PINS_MAT1: IOCON_SetPinFunc(IOCON_PIO1_7, PIO1_7_FUN_CT32B0_MAT1); break; case TIM_PINS_MAT2: IOCON_SetPinFunc(IOCON_PIO0_1, PIO0_1_FUN_CT32B0_MAT2); break; case TIM_PINS_MAT3: IOCON_SetPinFunc(IOCON_PIO0_11, PIO0_11_FUN_CT32B0_MAT3); break; } // 32-bit counter/timer 1 external Match Output } else if (TIMx == LPC_TMR32B1) { switch(matFlag) { case TIM_PINS_MAT0: IOCON_SetPinFunc(IOCON_PIO1_1, PIO1_1_FUN_CT32B1_MAT0); break; case TIM_PINS_MAT1: IOCON_SetPinFunc(IOCON_PIO1_2, PIO1_2_FUN_CT32B1_MAT1); break; case TIM_PINS_MAT2: IOCON_SetPinFunc(IOCON_PIO1_3, PIO1_3_FUN_CT32B1_MAT2); break; case TIM_PINS_MAT3: IOCON_SetPinFunc(IOCON_PIO1_4, PIO1_4_FUN_CT32B1_MAT3); break; } } }
/*********************************************************************//** * @brief Initial Timer/Counter device * Set Clock frequency for ADC * Set initial configuration for ADC * @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(TIM_TypeDef *TIMx, uint8_t TimerCounterMode, void *TIM_ConfigStruct) { TIM_TIMERCFG_Type *pTimeCfg; TIM_COUNTERCFG_Type *pCounterCfg; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode)); uint32_t timer = TIM_ConverPtrToTimeNum(TIMx) ; //set power if (TIMx== TIM0) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER0, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== TIM1) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER1, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== TIM2) { CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, ENABLE); CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER2, CLKPWR_PCLKSEL_CCLK_DIV_4); } else if (TIMx== 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->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; if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1) TIMx->CCR |= _BIT(2); //set pin function PINSEL_ConfigPin((PINSEL_CFG_Type *)&timer_caption_pin[2*timer + pCounterCfg->CountInputSelect]); } // Clear interrupt pending TIMx->IR = 0xFFFFFFFF; }
/*********************************************************************//** * @brief Clear Capture Interrupt pending * @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] IntFlag interrupt type, should be: * - TIM_MR0_INT :Interrupt for Match channel 0 * - TIM_MR1_INT :Interrupt for Match channel 1 * - TIM_MR2_INT :Interrupt for Match channel 2 * - TIM_MR3_INT :Interrupt for Match channel 3 * - TIM_CR0_INT :Interrupt for Capture channel 0 * - TIM_CR1_INT :Interrupt for Capture channel 1 * @return None **********************************************************************/ void TIM_ClearIntCapturePending(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag)); TIMx->IR = (1<<(4+IntFlag)); }