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.
lib_ClockCounter.cpp
00001 /** Mbed Library Clock Counter 00002 * Hardware pulse counter with TIMER2 (CAP2.0 or CAP2.1) on Mbed LPC1768 00003 * 00004 * Counts signal transitions on p30(CAP2.0) or p29(CAP2.1) for LPC1768 target. 00005 * Can detecte rising, falling or both signal edge. 00006 * Return the signal edge count during a period in seconds. 00007 * In theory (Shannon's theorem) input signal frequency can up to 48 MHz with 96 MHz CCLK. 00008 * But only tested with frequencys up to 20 MHz and it work. 00009 * 00010 * Example : 00011 * @code 00012 * #include "mbed.h" 00013 * #include "lib_ClockCounter.h" 00014 * 00015 * Serial pc(USBTX, USBRX); 00016 * ClockCounter Frequency; 00017 * 00018 * int main() 00019 * { 00020 * while(1) pc.printf("Frequency = %d Hz\r\n", Frequency.getCount()); 00021 * } 00022 * @endcode 00023 */ 00024 00025 #include "lib_ClockCounter.h" 00026 #include <cstdint> 00027 00028 ClockCounter::ClockCounter(PinName PIN_CAP2, edgeDetection EDGE) 00029 { 00030 _count = 0; 00031 _selectPin = NC; 00032 switch(PIN_CAP2) 00033 { 00034 case p30: case p29: 00035 // PCONP => Power Mode Control register 00036 LPC_SC->PCONP |= (0x1<<22); // Bit(22) 1 => Timer2 power on 00037 // PCLKSEL1 => Peripheral Clock Selection register 1 00038 LPC_SC->PCLKSEL1 |= (0x1<<12); // Bits(13,12) 01 => PCLK_TIMER2 = CCLK(96 MHz) 00039 // TCR => Timer Control Register 00040 LPC_TIM2->TCR = 0x0; // Bits(1,0) 00 => Timer2 disabled 00041 // PINSEL0 => Pin Function Select register 0 00042 LPC_PINCON->PINSEL0 |= (0x3<<((PIN_CAP2==p30)?8:10)); // Bits(9,8) 11 => pin function CAP2.0 (p30), Bits(11,10) 11 => pin function CAP2.1 (p29) 00043 // CTCR => Count Control Register 00044 LPC_TIM2->CTCR = ((PIN_CAP2==p30)?0:4)+EDGE; // Bits(3,2) 00 => CAP2.0 (p30 signal is counter clock) or 11 => CAP2.1 (p29 signal is counter clock), Bits(1,0) XX => timer is incremented on edge 00045 // CCR => Capture Control Register 00046 LPC_TIM2->CCR = 0x0; // Bits(5,4,3,2,1,0) 000000 => capture and interrupt on event disabled 00047 break; 00048 default: 00049 break; 00050 } 00051 } 00052 00053 void ClockCounter::setPin(PinName PIN_CAP2, edgeDetection EDGE) 00054 { 00055 _selectPin = PIN_CAP2; 00056 switch(PIN_CAP2) 00057 { 00058 case p30: case p29: 00059 // PCONP => Power Mode Control register 00060 LPC_SC->PCONP |= (0x1<<22); // Bit(22) 1 => Timer2 power on 00061 // PCLKSEL1 => Peripheral Clock Selection register 1 00062 LPC_SC->PCLKSEL1 |= (0x1<<12); // Bits(13,12) 01 => PCLK_TIMER2 = CCLK(96 MHz) 00063 // TCR => Timer Control Register 00064 LPC_TIM2->TCR = 0x0; // Bits(1,0) 00 => Timer2 disabled 00065 // PINSEL0 => Pin Function Select register 0 00066 LPC_PINCON->PINSEL0 |= (0x3<<((PIN_CAP2==p30)?8:10)); // Bits(9,8) 11 => pin function CAP2.0 (p30), Bits(11,10) 11 => pin function CAP2.1 (p29) 00067 // CTCR => Count Control Register 00068 LPC_TIM2->CTCR = ((PIN_CAP2==p30)?0:4)+EDGE; // Bits(3,2) 00 => CAP2.0 (p30 signal is counter clock) or 11 => CAP2.1 (p29 signal is counter clock), Bits(1,0) XX => timer is incremented on edge 00069 // CCR => Capture Control Register 00070 LPC_TIM2->CCR = 0x0; // Bits(5,4,3,2,1,0) 000000 => capture and interrupt on event disabled 00071 break; 00072 default: 00073 break; 00074 } 00075 } 00076 00077 void ClockCounter::startCount(void) 00078 { 00079 // TCR => Timer Control Register 00080 LPC_TIM2->TCR = 0x2; // Bits(1,0) 10 => Timer2 count reset 00081 LPC_TIM2->TCR = 0x1; // Bits(1,0) 01 => Timer2 enabled 00082 } 00083 00084 int ClockCounter::stopCount(void) 00085 { 00086 LPC_TIM2->TCR = 0x0; // Bits(1,0) 00 => Timer2 disabled 00087 uint32_t TC = LPC_TIM2->TC; // TC => Timer Counter 00088 _selectPin = NC; 00089 return TC; 00090 } 00091 00092 int ClockCounter::getCount(int period) 00093 { 00094 // TCR => Timer Control Register 00095 LPC_TIM2->TCR = 0x2; // Bits(1,0) 10 => Timer2 count reset 00096 LPC_TIM2->TCR = 0x1; // Bits(1,0) 01 => Timer2 enabled 00097 wait_us(period); 00098 LPC_TIM2->TCR = 0x0; // Bits(1,0) 00 => Timer2 disabled 00099 return LPC_TIM2->TC; // TC => Timer Counter 00100 } 00101 00102 PinName ClockCounter::getPin(void) 00103 { 00104 return _selectPin; 00105 }
Generated on Fri Jul 15 2022 07:54:03 by
1.7.2