Frequency counter using LPC1768 hardware counter, tested up-to 11MHz forked from https://developer.mbed.org/users/mio/code/5MHzOSC/
Fork of 5MHzOSC by
main.cpp
00001 // 00002 // CLOCK OUT to PWM1[6] Sample with Freq Counter using Cap2.0 00003 // For LPC1768-mbed 00004 // 00005 // Reference: 5MHz Clock Out Code and Comment - http://mbed.org/forum/mbed/topic/733/ 00006 // 00007 // !! To Self Measurement Output Clock, Connect p21 <-> p30 with jumper wire. 00008 // 2013.6.18 : Wrong comment about MR6 and Duty fix. 00009 // 00010 00011 #include "mbed.h" 00012 00013 PwmOut fmclck(p21); // for RESERVE pin21 as PWM1[6] 00014 DigitalIn clkin(p30); // for RESERVE pin30 as CAP2[0] 00015 00016 // Reset Counter and Count Start 00017 void P30_RESET_CTR(void) 00018 { 00019 LPC_TIM2->TCR = 2; // Reset the counter (bit1<=1,bit0<=0) 00020 LPC_TIM2->TCR = 1; // UnReset counter (bit1<=0,bit0<=1) 00021 } 00022 00023 // Get Counter Value 00024 int P30_GET_CTR(void) 00025 { 00026 return LPC_TIM2->TC; // Read the counter value 00027 } 00028 00029 // Setting p30 to Cap2.0 00030 void P30_INIT_CTR(void) 00031 { 00032 LPC_SC->PCONP |= 1 << 22; // 1)Power up TimerCounter2 (bit22) 00033 LPC_PINCON->PINSEL0 |= 3 << 8; // 2)Set P0[4] to CAP2[0] 00034 LPC_TIM2->TCR = 2; // 3)Counter Reset (bit1<=1,bit0<=0) 00035 LPC_TIM2->CTCR = 1; // 4)Count on riging edge Cap2[0] 00036 LPC_TIM2->CCR = 0; // 5)Input Capture Disabled 00037 LPC_TIM2->TCR = 1; // 6)Counter Start (bit1<=0,bit0<=1) 00038 } 00039 00040 // Clock Output From pin21(PWM6) 00041 // Set Clock Freq with div. 00042 // if mbed is running at 96MHz, div is set 96 to Get 1MHz. 00043 void PWM6_SETCLK(int div) 00044 { 00045 LPC_PWM1->TCR = (1 << 1); // 1)Reset counter, disable PWM 00046 LPC_SC->PCLKSEL0 &= ~(0x3 << 12); 00047 LPC_SC->PCLKSEL0 |= (1 << 12); // 2)Set peripheral clock divider to /1, i.e. system clock 00048 LPC_PWM1->MR0 = div - 1; // 3)Match Register 0 is shared period counter for all PWM1 00049 LPC_PWM1->MR6 = (div + 1)>> 1; // 00050 LPC_PWM1->LER |= 1; // 4)Start updating at next period start 00051 LPC_PWM1->TCR = (1 << 0) || (1 << 3); // 5)Enable counter and PWM 00052 } 00053 00054 int main() { 00055 PWM6_SETCLK(19) ; // Outout mbed's "PWM6" pin to 96MHZ/19 = 5.052MHz (Approx) 00056 // PWM6_SETCLK(96) ; // Outout mbed's "PWM6" pin to 96MHZ/96 = 1.000MHz (Approx) 00057 P30_INIT_CTR(); 00058 while(1){ 00059 P30_RESET_CTR(); 00060 wait_us(100000); // Gate time for count 00061 printf("pin30 Freq = %u (Hz)\r\n",P30_GET_CTR()); 00062 } 00063 }
Generated on Tue Jul 19 2022 19:41:25 by 1.7.2