My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc17xx_timer.c 20KB

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