lpc-field

Template project for programming NXP's LPC1768 MCUs
git clone git://git.mdnr.space/lpc-field
Log | Files | Refs | README | LICENSE

lpc17xx_timer.c (20246B)


      1 /**********************************************************************
      2 * $Id$		lpc17xx_timer.c				2011-03-10
      3 *//**
      4 * @file		lpc17xx_timer.c
      5 * @brief	Contains all functions support for Timer firmware library
      6 * 			on LPC17xx
      7 * @version	3.1
      8 * @date		10. March. 2011
      9 * @author	NXP MCU SW Application Team
     10 *
     11 * Copyright(C) 2011, NXP Semiconductor
     12 * All rights reserved.
     13 *
     14 ***********************************************************************
     15 * Software that is described herein is for illustrative purposes only
     16 * which provides customers with programming information regarding the
     17 * products. This software is supplied "AS IS" without any warranties.
     18 * NXP Semiconductors assumes no responsibility or liability for the
     19 * use of the software, conveys no license or title under any patent,
     20 * copyright, or mask work right to the product. NXP Semiconductors
     21 * reserves the right to make changes in the software without
     22 * notification. NXP Semiconductors also make no representation or
     23 * warranty that such application will be suitable for the specified
     24 * use without further testing or modification.
     25 * Permission to use, copy, modify, and distribute this software and its
     26 * documentation is hereby granted, under NXP Semiconductors'
     27 * relevant copyright in the software, without fee, provided that it
     28 * is used in conjunction with NXP Semiconductors microcontrollers.  This
     29 * copyright, permission, and disclaimer notice must appear in all copies of
     30 * this code.
     31 **********************************************************************/
     32 
     33 /* Peripheral group ----------------------------------------------------------- */
     34 /** @addtogroup TIM
     35  * @{
     36  */
     37 
     38 /* Includes ------------------------------------------------------------------- */
     39 #include "lpc17xx_timer.h"
     40 #include "lpc17xx_clkpwr.h"
     41 #include "lpc17xx_pinsel.h"
     42 
     43 /* If this source file built with example, the LPC17xx FW library configuration
     44  * file in each example directory ("lpc17xx_libcfg.h") must be included,
     45  * otherwise the default FW library configuration file must be included instead
     46  */
     47 #ifdef __BUILD_WITH_EXAMPLE__
     48 #include "lpc17xx_libcfg.h"
     49 #else
     50 #include "lpc17xx_libcfg_default.h"
     51 #endif /* __BUILD_WITH_EXAMPLE__ */
     52 
     53 #ifdef _TIM
     54 
     55 /* Private Functions ---------------------------------------------------------- */
     56 
     57 static uint32_t getPClock (uint32_t timernum);
     58 static uint32_t converUSecToVal (uint32_t timernum, uint32_t usec);
     59 static uint32_t converPtrToTimeNum (LPC_TIM_TypeDef *TIMx);
     60 
     61 
     62 /*********************************************************************//**
     63  * @brief 		Get peripheral clock of each timer controller
     64  * @param[in]	timernum Timer number
     65  * @return 		Peripheral clock of timer
     66  **********************************************************************/
     67 static uint32_t getPClock (uint32_t timernum)
     68 {
     69 	uint32_t clkdlycnt = 0;
     70 	switch (timernum)
     71 	{
     72 	case 0:
     73 		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER0);
     74 		break;
     75 
     76 	case 1:
     77 		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER1);
     78 		break;
     79 
     80 	case 2:
     81 		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER2);
     82 		break;
     83 
     84 	case 3:
     85 		clkdlycnt = CLKPWR_GetPCLK (CLKPWR_PCLKSEL_TIMER3);
     86 		break;
     87 	}
     88 	return clkdlycnt;
     89 }
     90 
     91 
     92 /*********************************************************************//**
     93  * @brief 		Convert a time to a timer count value
     94  * @param[in]	timernum Timer number
     95  * @param[in]	usec Time in microseconds
     96  * @return 		The number of required clock ticks to give the time delay
     97  **********************************************************************/
     98 uint32_t converUSecToVal (uint32_t timernum, uint32_t usec)
     99 {
    100 	uint64_t clkdlycnt = 0;
    101 
    102 	// Get Pclock of timer
    103 	clkdlycnt = (uint64_t) getPClock(timernum);
    104 
    105 	clkdlycnt = (clkdlycnt * usec) / 1000000;
    106 	return (uint32_t) clkdlycnt;
    107 }
    108 
    109 
    110 /*********************************************************************//**
    111  * @brief 		Convert a timer register pointer to a timer number
    112  * @param[in]	TIMx Pointer to LPC_TIM_TypeDef, should be:
    113  * 				- LPC_TIM0: TIMER0 peripheral
    114  * 				- LPC_TIM1: TIMER1 peripheral
    115  * 				- LPC_TIM2: TIMER2 peripheral
    116  * 				- LPC_TIM3: TIMER3 peripheral
    117  * @return 		The timer number (0 to 3) or 0xFFFF FFFF if register pointer is bad
    118  **********************************************************************/
    119 uint32_t converPtrToTimeNum (LPC_TIM_TypeDef *TIMx)
    120 {
    121 	uint32_t tnum = 0xFFFFFFFF;
    122 
    123 	if (TIMx == LPC_TIM0)
    124 	{
    125 		tnum = 0;
    126 	}
    127 	else if (TIMx == LPC_TIM1)
    128 	{
    129 		tnum = 1;
    130 	}
    131 	else if (TIMx == LPC_TIM2)
    132 	{
    133 		tnum = 2;
    134 	}
    135 	else if (TIMx == LPC_TIM3)
    136 	{
    137 		tnum = 3;
    138 	}
    139 
    140 	return tnum;
    141 }
    142 
    143 /* End of Private Functions ---------------------------------------------------- */
    144 
    145 
    146 /* Public Functions ----------------------------------------------------------- */
    147 /** @addtogroup TIM_Public_Functions
    148  * @{
    149  */
    150 
    151 /*********************************************************************//**
    152  * @brief 		Get Interrupt Status
    153  * @param[in]	TIMx Timer selection, should be:
    154  *   			- LPC_TIM0: TIMER0 peripheral
    155  * 				- LPC_TIM1: TIMER1 peripheral
    156  * 				- LPC_TIM2: TIMER2 peripheral
    157  * 				- LPC_TIM3: TIMER3 peripheral
    158  * @param[in]	IntFlag: interrupt type, should be:
    159  * 				- TIM_MR0_INT: Interrupt for Match channel 0
    160  * 				- TIM_MR1_INT: Interrupt for Match channel 1
    161  * 				- TIM_MR2_INT: Interrupt for Match channel 2
    162  * 				- TIM_MR3_INT: Interrupt for Match channel 3
    163  * 				- TIM_CR0_INT: Interrupt for Capture channel 0
    164  * 				- TIM_CR1_INT: Interrupt for Capture channel 1
    165  * @return 		FlagStatus
    166  * 				- SET : interrupt
    167  * 				- RESET : no interrupt
    168  **********************************************************************/
    169 FlagStatus TIM_GetIntStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag)
    170 {
    171 	uint8_t temp;
    172 	CHECK_PARAM(PARAM_TIMx(TIMx));
    173 	CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));
    174 	temp = (TIMx->IR)& TIM_IR_CLR(IntFlag);
    175 	if (temp)
    176 		return SET;
    177 
    178 	return RESET;
    179 
    180 }
    181 /*********************************************************************//**
    182  * @brief 		Get Capture Interrupt Status
    183  * @param[in]	TIMx Timer selection, should be:
    184  *  	   		- LPC_TIM0: TIMER0 peripheral
    185  * 				- LPC_TIM1: TIMER1 peripheral
    186  * 				- LPC_TIM2: TIMER2 peripheral
    187  * 				- LPC_TIM3: TIMER3 peripheral
    188  * @param[in]	IntFlag: interrupt type, should be:
    189  * 				- TIM_MR0_INT: Interrupt for Match channel 0
    190  * 				- TIM_MR1_INT: Interrupt for Match channel 1
    191  * 				- TIM_MR2_INT: Interrupt for Match channel 2
    192  * 				- TIM_MR3_INT: Interrupt for Match channel 3
    193  * 				- TIM_CR0_INT: Interrupt for Capture channel 0
    194  * 				- TIM_CR1_INT: Interrupt for Capture channel 1
    195  * @return 		FlagStatus
    196  * 				- SET : interrupt
    197  * 				- RESET : no interrupt
    198  **********************************************************************/
    199 FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag)
    200 {
    201 	uint8_t temp;
    202 	CHECK_PARAM(PARAM_TIMx(TIMx));
    203 	CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));
    204 	temp = (TIMx->IR) & (1<<(4+IntFlag));
    205 	if(temp)
    206 		return SET;
    207 	return RESET;
    208 }
    209 /*********************************************************************//**
    210  * @brief 		Clear Interrupt pending
    211  * @param[in]	TIMx Timer selection, should be:
    212  *    			- LPC_TIM0: TIMER0 peripheral
    213  * 				- LPC_TIM1: TIMER1 peripheral
    214  * 				- LPC_TIM2: TIMER2 peripheral
    215  * 				- LPC_TIM3: TIMER3 peripheral
    216  * @param[in]	IntFlag: interrupt type, should be:
    217  * 				- TIM_MR0_INT: Interrupt for Match channel 0
    218  * 				- TIM_MR1_INT: Interrupt for Match channel 1
    219  * 				- TIM_MR2_INT: Interrupt for Match channel 2
    220  * 				- TIM_MR3_INT: Interrupt for Match channel 3
    221  * 				- TIM_CR0_INT: Interrupt for Capture channel 0
    222  * 				- TIM_CR1_INT: Interrupt for Capture channel 1
    223  * @return 		None
    224  **********************************************************************/
    225 void TIM_ClearIntPending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag)
    226 {
    227 	CHECK_PARAM(PARAM_TIMx(TIMx));
    228 	CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));
    229 	TIMx->IR = TIM_IR_CLR(IntFlag);
    230 }
    231 
    232 /*********************************************************************//**
    233  * @brief 		Clear Capture Interrupt pending
    234  * @param[in]	TIMx Timer selection, should be
    235  *    			- LPC_TIM0: TIMER0 peripheral
    236  * 				- LPC_TIM1: TIMER1 peripheral
    237  * 				- LPC_TIM2: TIMER2 peripheral
    238  * 				- LPC_TIM3: TIMER3 peripheral
    239  * @param[in]	IntFlag interrupt type, should be:
    240  *				- TIM_MR0_INT: Interrupt for Match channel 0
    241  * 				- TIM_MR1_INT: Interrupt for Match channel 1
    242  * 				- TIM_MR2_INT: Interrupt for Match channel 2
    243  * 				- TIM_MR3_INT: Interrupt for Match channel 3
    244  * 				- TIM_CR0_INT: Interrupt for Capture channel 0
    245  * 				- TIM_CR1_INT: Interrupt for Capture channel 1
    246  * @return 		None
    247  **********************************************************************/
    248 void TIM_ClearIntCapturePending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag)
    249 {
    250 	CHECK_PARAM(PARAM_TIMx(TIMx));
    251 	CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));
    252 	TIMx->IR = (1<<(4+IntFlag));
    253 }
    254 
    255 /*********************************************************************//**
    256  * @brief 		Configuration for Timer at initial time
    257  * @param[in] 	TimerCounterMode timer counter mode, should be:
    258  * 				- TIM_TIMER_MODE: Timer mode
    259  * 				- TIM_COUNTER_RISING_MODE: Counter rising mode
    260  * 				- TIM_COUNTER_FALLING_MODE: Counter falling mode
    261  * 				- TIM_COUNTER_ANY_MODE:Counter on both edges
    262  * @param[in] 	TIM_ConfigStruct pointer to TIM_TIMERCFG_Type or
    263  * 				TIM_COUNTERCFG_Type
    264  * @return 		None
    265  **********************************************************************/
    266 void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)
    267 {
    268 	if (TimerCounterMode == TIM_TIMER_MODE )
    269 	{
    270 		TIM_TIMERCFG_Type * pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;
    271 		pTimeCfg->PrescaleOption = TIM_PRESCALE_USVAL;
    272 		pTimeCfg->PrescaleValue = 1;
    273 	}
    274 	else
    275 	{
    276 		TIM_COUNTERCFG_Type * pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;
    277 		pCounterCfg->CountInputSelect = TIM_COUNTER_INCAP0;
    278 	}
    279 }
    280 
    281 /*********************************************************************//**
    282  * @brief 		Initial Timer/Counter device
    283  * 				 	Set Clock frequency for Timer
    284  * 					Set initial configuration for Timer
    285  * @param[in]	TIMx  Timer selection, should be:
    286  * 				- LPC_TIM0: TIMER0 peripheral
    287  * 				- LPC_TIM1: TIMER1 peripheral
    288  * 				- LPC_TIM2: TIMER2 peripheral
    289  * 				- LPC_TIM3: TIMER3 peripheral
    290  * @param[in]	TimerCounterMode Timer counter mode, should be:
    291  * 				- TIM_TIMER_MODE: Timer mode
    292  * 				- TIM_COUNTER_RISING_MODE: Counter rising mode
    293  * 				- TIM_COUNTER_FALLING_MODE: Counter falling mode
    294  * 				- TIM_COUNTER_ANY_MODE:Counter on both edges
    295  * @param[in]	TIM_ConfigStruct pointer to TIM_TIMERCFG_Type
    296  * 				that contains the configuration information for the
    297  *                    specified Timer peripheral.
    298  * @return 		None
    299  **********************************************************************/
    300 void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)
    301 {
    302 	TIM_TIMERCFG_Type *pTimeCfg;
    303 	TIM_COUNTERCFG_Type *pCounterCfg;
    304 
    305 	CHECK_PARAM(PARAM_TIMx(TIMx));
    306 	CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode));
    307 
    308 	//set power
    309 
    310 	if (TIMx== LPC_TIM0)
    311 	{
    312 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, ENABLE);
    313 		//PCLK_Timer0 = CCLK/4
    314 		CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER0, CLKPWR_PCLKSEL_CCLK_DIV_4);
    315 	}
    316 	else if (TIMx== LPC_TIM1)
    317 	{
    318 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, ENABLE);
    319 		//PCLK_Timer1 = CCLK/4
    320 		CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER1, CLKPWR_PCLKSEL_CCLK_DIV_4);
    321 
    322 	}
    323 
    324 	else if (TIMx== LPC_TIM2)
    325 	{
    326 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, ENABLE);
    327 		//PCLK_Timer2= CCLK/4
    328 		CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER2, CLKPWR_PCLKSEL_CCLK_DIV_4);
    329 	}
    330 	else if (TIMx== LPC_TIM3)
    331 	{
    332 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM3, ENABLE);
    333 		//PCLK_Timer3= CCLK/4
    334 		CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_TIMER3, CLKPWR_PCLKSEL_CCLK_DIV_4);
    335 
    336 	}
    337 
    338 	TIMx->CCR &= ~TIM_CTCR_MODE_MASK;
    339 	TIMx->CCR |= TIM_TIMER_MODE;
    340 
    341 	TIMx->TC =0;
    342 	TIMx->PC =0;
    343 	TIMx->PR =0;
    344 	TIMx->TCR |= (1<<1); //Reset Counter
    345 	TIMx->TCR &= ~(1<<1); //release reset
    346 	if (TimerCounterMode == TIM_TIMER_MODE )
    347 	{
    348 		pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;
    349 		if (pTimeCfg->PrescaleOption  == TIM_PRESCALE_TICKVAL)
    350 		{
    351 			TIMx->PR   = pTimeCfg->PrescaleValue -1  ;
    352 		}
    353 		else
    354 		{
    355 			TIMx->PR   = converUSecToVal (converPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1;
    356 		}
    357 	}
    358 	else
    359 	{
    360 
    361 		pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;
    362 		TIMx->CCR  &= ~TIM_CTCR_INPUT_MASK;
    363 		if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1)
    364 			TIMx->CCR |= _BIT(2);
    365 	}
    366 
    367 	// Clear interrupt pending
    368 	TIMx->IR = 0xFFFFFFFF;
    369 
    370 }
    371 
    372 /*********************************************************************//**
    373  * @brief 		Close Timer/Counter device
    374  * @param[in]	TIMx  Pointer to timer device, should be:
    375  * 				- LPC_TIM0: TIMER0 peripheral
    376  * 				- LPC_TIM1: TIMER1 peripheral
    377  * 				- LPC_TIM2: TIMER2 peripheral
    378  * 				- LPC_TIM3: TIMER3 peripheral
    379  * @return 		None
    380  **********************************************************************/
    381 void TIM_DeInit (LPC_TIM_TypeDef *TIMx)
    382 {
    383 	CHECK_PARAM(PARAM_TIMx(TIMx));
    384 	// Disable timer/counter
    385 	TIMx->TCR = 0x00;
    386 
    387 	// Disable power
    388 	if (TIMx== LPC_TIM0)
    389 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM0, DISABLE);
    390 
    391 	else if (TIMx== LPC_TIM1)
    392 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM1, DISABLE);
    393 
    394 	else if (TIMx== LPC_TIM2)
    395 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, DISABLE);
    396 
    397 	else if (TIMx== LPC_TIM3)
    398 		CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCTIM2, DISABLE);
    399 
    400 }
    401 
    402 /*********************************************************************//**
    403  * @brief	 	Start/Stop Timer/Counter device
    404  * @param[in]	TIMx Pointer to timer device, should be:
    405  *  			- LPC_TIM0: TIMER0 peripheral
    406  * 				- LPC_TIM1: TIMER1 peripheral
    407  * 				- LPC_TIM2: TIMER2 peripheral
    408  * 				- LPC_TIM3: TIMER3 peripheral
    409  * @param[in]	NewState
    410  * 				-	ENABLE  : set timer enable
    411  * 				-	DISABLE : disable timer
    412  * @return 		None
    413  **********************************************************************/
    414 void TIM_Cmd(LPC_TIM_TypeDef *TIMx, FunctionalState NewState)
    415 {
    416 	CHECK_PARAM(PARAM_TIMx(TIMx));
    417 	if (NewState == ENABLE)
    418 	{
    419 		TIMx->TCR	|=  TIM_ENABLE;
    420 	}
    421 	else
    422 	{
    423 		TIMx->TCR &= ~TIM_ENABLE;
    424 	}
    425 }
    426 
    427 /*********************************************************************//**
    428  * @brief 		Reset Timer/Counter device,
    429  * 					Make TC and PC are synchronously reset on the next
    430  * 					positive edge of PCLK
    431  * @param[in]	TIMx Pointer to timer device, should be:
    432  *   			- LPC_TIM0: TIMER0 peripheral
    433  * 				- LPC_TIM1: TIMER1 peripheral
    434  * 				- LPC_TIM2: TIMER2 peripheral
    435  * 				- LPC_TIM3: TIMER3 peripheral
    436  * @return 		None
    437  **********************************************************************/
    438 void TIM_ResetCounter(LPC_TIM_TypeDef *TIMx)
    439 {
    440 	CHECK_PARAM(PARAM_TIMx(TIMx));
    441 	TIMx->TCR |= TIM_RESET;
    442 	TIMx->TCR &= ~TIM_RESET;
    443 }
    444 
    445 /*********************************************************************//**
    446  * @brief 		Configuration for Match register
    447  * @param[in]	TIMx Pointer to timer device, should be:
    448  *   			- LPC_TIM0: TIMER0 peripheral
    449  * 				- LPC_TIM1: TIMER1 peripheral
    450  * 				- LPC_TIM2: TIMER2 peripheral
    451  * 				- LPC_TIM3: TIMER3 peripheral
    452  * @param[in]   TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type
    453  * 					- MatchChannel : choose channel 0 or 1
    454  * 					- IntOnMatch	 : if SET, interrupt will be generated when MRxx match
    455  * 									the value in TC
    456  * 					- StopOnMatch	 : if SET, TC and PC will be stopped whenM Rxx match
    457  * 									the value in TC
    458  * 					- ResetOnMatch : if SET, Reset on MR0 when MRxx match
    459  * 									the value in TC
    460  * 					-ExtMatchOutputType: Select output for external match
    461  * 						 +	 0:	Do nothing for external output pin if match
    462  *						 +   1:	Force external output pin to low if match
    463  *						 + 	 2: Force external output pin to high if match
    464  *						 + 	 3: Toggle external output pin if match
    465  *					MatchValue: Set the value to be compared with TC value
    466  * @return 		None
    467  **********************************************************************/
    468 void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct)
    469 {
    470 
    471 	CHECK_PARAM(PARAM_TIMx(TIMx));
    472 	CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType));
    473 
    474 	switch(TIM_MatchConfigStruct->MatchChannel)
    475 	{
    476 	case 0:
    477 		TIMx->MR0 = TIM_MatchConfigStruct->MatchValue;
    478 		break;
    479 	case 1:
    480 		TIMx->MR1 = TIM_MatchConfigStruct->MatchValue;
    481 		break;
    482 	case 2:
    483 		TIMx->MR2 = TIM_MatchConfigStruct->MatchValue;
    484 		break;
    485 	case 3:
    486 		TIMx->MR3 = TIM_MatchConfigStruct->MatchValue;
    487 		break;
    488 	default:
    489 		//Error match value
    490 		//Error loop
    491 		while(1);
    492 	}
    493 	//interrupt on MRn
    494 	TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel);
    495 
    496 	if (TIM_MatchConfigStruct->IntOnMatch)
    497 		TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
    498 
    499 	//reset on MRn
    500 	if (TIM_MatchConfigStruct->ResetOnMatch)
    501 		TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
    502 
    503 	//stop on MRn
    504 	if (TIM_MatchConfigStruct->StopOnMatch)
    505 		TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
    506 
    507 	// match output type
    508 
    509 	TIMx->EMR 	&= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel);
    510 	TIMx->EMR   |= TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType);
    511 }
    512 /*********************************************************************//**
    513  * @brief 		Update Match value
    514  * @param[in]	TIMx Pointer to timer device, should be:
    515  *   			- LPC_TIM0: TIMER0 peripheral
    516  * 				- LPC_TIM1: TIMER1 peripheral
    517  * 				- LPC_TIM2: TIMER2 peripheral
    518  * 				- LPC_TIM3: TIMER3 peripheral
    519  * @param[in]	MatchChannel	Match channel, should be: 0..3
    520  * @param[in]	MatchValue		updated match value
    521  * @return 		None
    522  **********************************************************************/
    523 void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue)
    524 {
    525 	CHECK_PARAM(PARAM_TIMx(TIMx));
    526 	switch(MatchChannel)
    527 	{
    528 	case 0:
    529 		TIMx->MR0 = MatchValue;
    530 		break;
    531 	case 1:
    532 		TIMx->MR1 = MatchValue;
    533 		break;
    534 	case 2:
    535 		TIMx->MR2 = MatchValue;
    536 		break;
    537 	case 3:
    538 		TIMx->MR3 = MatchValue;
    539 		break;
    540 	default:
    541 		//Error Loop
    542 		while(1);
    543 	}
    544 
    545 }
    546 /*********************************************************************//**
    547  * @brief 		Configuration for Capture register
    548  * @param[in]	TIMx Pointer to timer device, should be:
    549  *   			- LPC_TIM0: TIMER0 peripheral
    550  * 				- LPC_TIM1: TIMER1 peripheral
    551  * 				- LPC_TIM2: TIMER2 peripheral
    552  * 				- LPC_TIM3: TIMER3 peripheral
    553  * 					- CaptureChannel: set the channel to capture data
    554  * 					- RisingEdge    : if SET, Capture at rising edge
    555  * 					- FallingEdge	: if SET, Capture at falling edge
    556  * 					- IntOnCaption  : if SET, Capture generate interrupt
    557  * @param[in]   TIM_CaptureConfigStruct	Pointer to TIM_CAPTURECFG_Type
    558  * @return 		None
    559  **********************************************************************/
    560 void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct)
    561 {
    562 
    563 	CHECK_PARAM(PARAM_TIMx(TIMx));
    564 	TIMx->CCR &= ~TIM_CCR_CHANNEL_MASKBIT(TIM_CaptureConfigStruct->CaptureChannel);
    565 
    566 	if (TIM_CaptureConfigStruct->RisingEdge)
    567 		TIMx->CCR |= TIM_CAP_RISING(TIM_CaptureConfigStruct->CaptureChannel);
    568 
    569 	if (TIM_CaptureConfigStruct->FallingEdge)
    570 		TIMx->CCR |= TIM_CAP_FALLING(TIM_CaptureConfigStruct->CaptureChannel);
    571 
    572 	if (TIM_CaptureConfigStruct->IntOnCaption)
    573 		TIMx->CCR |= TIM_INT_ON_CAP(TIM_CaptureConfigStruct->CaptureChannel);
    574 }
    575 
    576 /*********************************************************************//**
    577  * @brief 		Read value of capture register in timer/counter device
    578  * @param[in]	TIMx Pointer to timer/counter device, should be:
    579  *  			- LPC_TIM0: TIMER0 peripheral
    580  * 				- LPC_TIM1: TIMER1 peripheral
    581  * 				- LPC_TIM2: TIMER2 peripheral
    582  * 				- LPC_TIM3: TIMER3 peripheral
    583  * @param[in]	CaptureChannel: capture channel number, should be:
    584  * 				- TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn
    585  * 				- TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn
    586  * @return 		Value of capture register
    587  **********************************************************************/
    588 uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel)
    589 {
    590 	CHECK_PARAM(PARAM_TIMx(TIMx));
    591 	CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel));
    592 
    593 	if(CaptureChannel==0)
    594 		return TIMx->CR0;
    595 	else
    596 		return TIMx->CR1;
    597 }
    598 
    599 /**
    600  * @}
    601  */
    602 
    603 #endif /* _TIMER */
    604 
    605 /**
    606  * @}
    607  */
    608 
    609 /* --------------------------------- End Of File ------------------------------ */