lpc_timer.c 21 KB

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