Only for mbed LPC1768, mbed LPC1114FN28, Nucleo-F401 and Nucleo-F411. No way to change pin assign.

Dependencies:   freq_counter TextLCD

Fork of 5MHzOSC by fuyuno sakura

Please refer following page.
http://developer.mbed.org/users/kenjiArai/notebook/simple-frequency-counter/
/users/kenjiArai/notebook/frequency-counters/

Revision:
6:44c2bcbdd77b
Parent:
5:af9fa3d0731c
Child:
7:0c09d29c4cf3
--- a/main.cpp	Sun Oct 19 06:45:13 2014 +0000
+++ b/main.cpp	Tue Oct 21 12:37:05 2014 +0000
@@ -5,7 +5,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: October   18th, 2014
- *      Revised: October   19th, 2014
+ *      Revised: October   21st, 2014
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -14,14 +14,21 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#define USE_COM         // use Communication with PC(UART)
+//#define USE_TEXT_LCD    // use Text LCD/I2C Interface
+//#define USE_GRAP_LCD    // use Grafic LCD/SPI interface
+
 //  Include ---------------------------------------------------------------------------------------
 #include "mbed.h"
+#include "freq_counter.h"
+#if defined(USE_TEXT_LCD)
 #include "TextLCD.h"            // Std. lib./ LCD control
-#include "freq_counter.h"
+#endif
+#if defined(USE_GRAP_LCD)
+#include "ST7565_SPI_LCD.h"
+#endif
 
 //  Definition ------------------------------------------------------------------------------------
-#define USE_COM         // use Communication with PC(UART)
-
 #ifdef USE_COM
 #define BAUD(x)         pc.baud(x)
 #define GETC(x)         pc.getc(x)
@@ -36,13 +43,24 @@
 #define READABLE(x)     {;}
 #endif
 
+#if defined(TARGET_LPC1768)
 // LPC1768 Frequency example
 // Outout mbed's "PWM6" pin to 96MHZ/19 = 5.052MHz (Approx)
 #define PWM_EAMPLE()    PWM6_SETCLK(19)
 // Outout mbed's "PWM6" pin to 96MHZ/96 = 1.000MHz (Approx)
 //#define PWM_EAMPLE()    PWM6_SETCLK(96)
-  
+#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
+#define led_not_zero    temp_ram
+#define led_01          temp_ram
+#define led_10          temp_ram
+#define PWM_EAMPLE()    port_mco1_mco2_set()
+#else
+#error "No support for this CPU"
+#endif
+
 //  Object ----------------------------------------------------------------------------------------
+#if defined(TARGET_LPC1768)
+
 DigitalOut led_gate(LED1);
 DigitalOut led_not_zero(LED2);
 DigitalOut led_01(LED3);
@@ -51,15 +69,42 @@
 DigitalIn  sw_10(p20);
 Serial pc(USBTX, USBRX);
 I2C i2cBus(p9, p10);   // SDA, SCL
+#if defined(USE_TEXT_LCD)
 TextLCD_I2C_N lcd(&i2cBus, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki AQM0802A)
+#endif
+#if defined(USE_GRAP_LCD)
+ST7565 glcd(p5, p7, p19, p8, p20, ST7565::AD12864SPI); // mosi, sck, reset, a0, ncs
+#endif
 PwmOut fmclck(p21);                 // for RESERVE pin21 as PWM1[6]
 F_COUNTER fc(p30);
 
+#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
+
+DigitalOut led_gate(LED1);
+DigitalIn  sw_01(PC_0);
+DigitalIn  sw_10(PC_1);
+Serial pc(USBTX, USBRX);
+I2C i2cBus(PB_9,PB_8);  // SDA, SCL
+#if defined(USE_TEXT_LCD)
+TextLCD_I2C_N lcd(&i2cBus, 0x7c, TextLCD::LCD8x2);  // LCD(Akizuki AQM0802A)
+#endif
+#if defined(USE_GRAP_LCD)
+ST7565 glcd(PB_5, PB_3, PA_10, PB_10, PA_9, ST7565::AD12864SPI); // mosi, sck, reset, a0, ncs
+#endif
+F_COUNTER fc(PA_0);
+// PA8 & PC9 uses for MCO_1 % MCO_2 -> Clock output for checking
+
+#else
+#error "No support for this CPU"
+#endif
+
 //  RAM -------------------------------------------------------------------------------------------
 float freqency;
-float t_gate;
+double t_gate;
 uint8_t sw;
 
+uint32_t temp_ram;  // dummy ram (please keep it!)
+
 //  ROM / Constant data ---------------------------------------------------------------------------
 
 //  Function prototypes ---------------------------------------------------------------------------
@@ -83,6 +128,29 @@
     LPC_PWM1->LER |= 1;                 // 4)Start updating at next period start
     LPC_PWM1->TCR = (1 << 0) || (1 << 3); // 5)Enable counter and PWM
 }
+#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
+void port_mco1_mco2_set(void)
+{
+    uint32_t temp = 0x00;
+
+    // PA8 -> MCO_1
+    temp = ((uint32_t)(GPIO_AF0_MCO) << (((uint32_t)8 & (uint32_t)0x07) * 4)) ;
+    GPIOA->AFR[8 >> 3] &= ~((uint32_t)0xf << ((uint32_t)(8 & (uint32_t)0x07) * 4)) ;
+    GPIOA->AFR[8 >> 3] |= temp;
+    GPIOA->MODER &= ~(GPIO_MODER_MODER0 << (8 * 2));
+    GPIOA->MODER |= (0x2 << (8 * 2));
+    // PC9 -> MCO_2
+    temp = ((uint32_t)(GPIO_AF0_MCO) << (((uint32_t)9 & (uint32_t)0x07) * 4)) ;
+    GPIOC->AFR[9 >> 3] &= ~((uint32_t)0xf << ((uint32_t)(9 & (uint32_t)0x07) * 4)) ;
+    GPIOC->AFR[9 >> 3] |= temp;
+    GPIOC->MODER &= ~(GPIO_MODER_MODER0 << (9 * 2));
+    GPIOC->MODER |= (0x2 << (9 * 2));
+    // Select output clock source
+    RCC->CFGR &= 0x009fffff;
+    // MC0_1 output HSE 1/4, MCO_2 output SYSCLK 1/4
+    //             MCO2          MCO2PRE       MCO1PRE       MCO1
+    RCC->CFGR |= (0x0 << 30) + (0x6 << 27) + (0x6 << 24) + (0x3 << 22);
+}
 #else
 #error "No support for this CPU"
 #endif
@@ -126,9 +194,10 @@
 
 int main()
 {
-    PRINTF("Frequency Counter by JH1PJL created on "__DATE__"\r\n");
+    PRINTF("\r\nFrequency Counter by JH1PJL created on "__DATE__"\r\n");
     t_gate = 1.0;
     // Initialize LCD
+#if defined(USE_TEXT_LCD)
     lcd.locate(0, 0);    // 1st line top
     //          12345678
     lcd.printf("Fre-Cntr");
@@ -136,10 +205,30 @@
     //        12345678
     lcd.puts(" JH1PJL ");
     lcd.setContrast(0x16);
+#endif
+#if defined(USE_GRAP_LCD)
+    glcd.cls();
+    glcd.set_contrast(0x06);
+    glcd.locate(0, 0);
+    glcd.printf("--- Frequency Counter --\r\n");
+    glcd.printf("     Kenji Arai / JH1PJL\r\n" );
+    glcd.printf("                        \r\n");
+#if defined(TARGET_LPC1768)
+    glcd.printf(" Input: P30 PWM out: P21\r\n" );
+    glcd.printf(" LED1:Gate  LED2:signal \r\n" );
+#elif defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F411RE)
+    glcd.printf(" Input: PA0 PWM out: PA8\r\n" );
+    glcd.printf(" LED1:Gate              \r\n" );
+#else
+#error "No support for this CPU"
+#endif
+#endif  // defined(USE_GRAP_LCD)
+#if defined(USE_TEXT_LCD)
     wait(5.0);
     lcd.locate(0, 1);    // 2nd line top
     //        12345678
     lcd.puts("        ");
+#endif
     // Set PWM for example
     example_freq_out();
     freqency = 0;
@@ -154,27 +243,56 @@
             led_not_zero = 0;
         }
         read_sw_and_set_gate_time();
-        PRINTF("p30 f = %9.0f [Hz] gate %4.3f [Sec]\r\n", freqency/t_gate, t_gate);
+        PRINTF("f= %9.0f [Hz], gate= %4.3f [Sec]\r\n", freqency/t_gate, t_gate);
+#if defined(USE_TEXT_LCD)
         lcd.locate(0, 0);    // 1st line top
         lcd.printf("%8.0f", freqency);
         lcd.locate(0, 1);    // 2nd line top
+#endif
+#if defined(USE_GRAP_LCD)
+        glcd.locate(0, 10);
+        glcd.printf("                      \r\n");
+        glcd.locate(10, 10);
+        glcd.printf("%8.0f Hz", freqency);
+        glcd.locate(10, 20);
+#endif
         switch (sw) {
             case 0:
+#if defined(USE_TEXT_LCD)
                 //          12345678
                 lcd.printf("x1000 Hz");
+#endif
+#if defined(USE_GRAP_LCD)
+                glcd.printf("  x1000   ");
+#endif
                 break;
             case 1:
+#if defined(USE_TEXT_LCD)
                 //          12345678
                 lcd.printf("x100  Hz");
+#endif
+#if defined(USE_GRAP_LCD)
+                glcd.printf("  x100    ");
+#endif
                 break;
             case 2:
+#if defined(USE_TEXT_LCD)
                 //          12345678
                 lcd.printf("x10   Hz");
+#endif
+#if defined(USE_GRAP_LCD)
+                glcd.printf("x10     ");
+#endif
                 break;
             case 3:
             default:
+#if defined(USE_TEXT_LCD)
                 //          12345678
                 lcd.printf("x1    Hz");
+#endif
+#if defined(USE_GRAP_LCD)
+                glcd.printf("  x1      ");
+#endif
                 break;
         }
     }