Exemplo n.º 1
0
/*********************************************************************//**
 * @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;
}
Exemplo n.º 2
0
/*********************************************************************//**
 * @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;

}