Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TimerCapture.cpp
00001 #include "TimerCapture.h" 00002 00003 bool TimerCapture::timerStarted = false; 00004 00005 TimerCapture::TimerCapture(PinName pCapturePin) { 00006 uint8_t bitcount_pinselect = 0; 00007 uint8_t bitcount_capture_control = 0; 00008 00009 #if DEBUG 00010 printf("Entering ctor\r\n"); 00011 #endif 00012 00013 switch (pCapturePin) { 00014 case p30: 00015 bitcount_pinselect = 8; 00016 bitcount_capture_control = 0; 00017 break; 00018 case p29: 00019 bitcount_pinselect = 10; 00020 bitcount_capture_control = 3; 00021 break; 00022 default: 00023 error("TimerCapture: Invalid pin specified! Pick either p29 (P0.5) or p30 (p0.4)."); 00024 break; 00025 } 00026 00027 #if DEBUG 00028 printf("Bitcounts selected: %d (pinselect) %d (capture control)\r\n", bitcount_pinselect, bitcount_capture_control); 00029 #endif 00030 00031 uint32_t bitmask_pinselect = (0x3 << bitcount_pinselect); 00032 mCapturePin = pCapturePin; 00033 00034 // error out if the pin is already configured. 00035 if ((LPC_PINCON->PINSEL0 & bitmask_pinselect) == bitmask_pinselect) { 00036 error("TimerCapture: Pin is already configured!"); 00037 } 00038 00039 00040 //check if peripheral has power, else this operation will hang! 00041 if ((LPC_SC->PCONP & (1 << 22)) == 0) { 00042 error("TimerCapture: Attempted to write to timer registers with power off!"); 00043 } 00044 00045 #if DEBUG 00046 printf("OK to configure registers\r\n"); 00047 #endif 00048 00049 // configure the pin 00050 LPC_PINCON->PINSEL0 |= bitmask_pinselect; 00051 00052 #if DEBUG 00053 printf("Configuring rising edge. Register is %08x\r\n", LPC_TIM2->CCR); 00054 #endif 00055 00056 // store on rising edge of input 00057 LPC_TIM2->CCR |= (1 << bitcount_capture_control); 00058 00059 #if DEBUG 00060 printf("Leaving ctor\r\n"); 00061 #endif 00062 } 00063 00064 void TimerCapture::startTimer() { 00065 if (!timerStarted) { 00066 timerStarted = true; 00067 00068 configureTimer(); 00069 00070 //start timer 00071 LPC_TIM2->TCR = 1; 00072 } 00073 } 00074 00075 void TimerCapture::stopTimer() { 00076 timerStarted = false; 00077 //stop timer 00078 LPC_TIM2->TCR = 0; 00079 } 00080 00081 bool TimerCapture::isRunning() { 00082 return timerStarted; 00083 } 00084 00085 void TimerCapture::resetTimer() { 00086 //reset timer 00087 LPC_TIM2->TCR = 2; 00088 LPC_TIM2->TCR = 0; 00089 LPC_TIM2->CR0 = 0; 00090 LPC_TIM2->CR1 = 0; 00091 } 00092 00093 uint32_t TimerCapture::getTime() { 00094 uint32_t observedTime = 0; 00095 switch(mCapturePin) { 00096 case p30: 00097 observedTime = LPC_TIM2->CR0; 00098 break; 00099 case p29: 00100 observedTime = LPC_TIM2->CR1; 00101 break; 00102 default: 00103 observedTime = 0; 00104 break; 00105 } 00106 00107 return observedTime; 00108 } 00109 00110 void TimerCapture::configureTimer() { 00111 // Power on Peripheral TIMER2 00112 LPC_SC->PCONP |= (1 << 22); 00113 00114 // Set clock source for TIMER2 00115 uint8_t clockSel = 0x01; 00116 LPC_SC->PCLKSEL1 |= (clockSel << 12); 00117 00118 // Set prescaler counter 00119 LPC_TIM2->PR = SystemCoreClock/1000; //should increment once a milisecond. 00120 00121 } 00122
Generated on Thu Jul 14 2022 05:37:43 by
