Frequency counter using GPS 1PPS signal and temperature controlled 50MHz Base clock. Ported from F411 Frequency Counter.
Dependencies: QEI DRV8830 PID ADT7410 TextLCD Frq_cuntr_Nucleo-F746ZG RingBuffer
Fork of Frequency_Counter_w_GPS_1PPS by
Please refer following.
/users/kenjiArai/notebook/frequency-counters/
main.cpp@5:af9fa3d0731c, 2014-10-19 (annotated)
- Committer:
- kenjiArai
- Date:
- Sun Oct 19 06:45:13 2014 +0000
- Revision:
- 5:af9fa3d0731c
- Parent:
- 4:e7d16ef216d4
- Child:
- 6:44c2bcbdd77b
Frequency Counter program based on mio/5MHzOSC program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 5:af9fa3d0731c | 1 | /* |
kenjiArai | 5:af9fa3d0731c | 2 | * mbed Application program / Frequency Counter |
kenjiArai | 5:af9fa3d0731c | 3 | * |
kenjiArai | 5:af9fa3d0731c | 4 | * Copyright (c) 2014 Kenji Arai / JH1PJL |
kenjiArai | 5:af9fa3d0731c | 5 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 5:af9fa3d0731c | 6 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 5:af9fa3d0731c | 7 | * Created: October 18th, 2014 |
kenjiArai | 5:af9fa3d0731c | 8 | * Revised: October 19th, 2014 |
kenjiArai | 5:af9fa3d0731c | 9 | * |
kenjiArai | 5:af9fa3d0731c | 10 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 5:af9fa3d0731c | 11 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 5:af9fa3d0731c | 12 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 5:af9fa3d0731c | 13 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 5:af9fa3d0731c | 14 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 5:af9fa3d0731c | 15 | */ |
mio | 0:c988614df67a | 16 | |
kenjiArai | 5:af9fa3d0731c | 17 | // Include --------------------------------------------------------------------------------------- |
mio | 0:c988614df67a | 18 | #include "mbed.h" |
kenjiArai | 5:af9fa3d0731c | 19 | #include "TextLCD.h" // Std. lib./ LCD control |
kenjiArai | 5:af9fa3d0731c | 20 | #include "freq_counter.h" |
mio | 0:c988614df67a | 21 | |
kenjiArai | 5:af9fa3d0731c | 22 | // Definition ------------------------------------------------------------------------------------ |
kenjiArai | 5:af9fa3d0731c | 23 | #define USE_COM // use Communication with PC(UART) |
mio | 1:2a347c40b1da | 24 | |
kenjiArai | 5:af9fa3d0731c | 25 | #ifdef USE_COM |
kenjiArai | 5:af9fa3d0731c | 26 | #define BAUD(x) pc.baud(x) |
kenjiArai | 5:af9fa3d0731c | 27 | #define GETC(x) pc.getc(x) |
kenjiArai | 5:af9fa3d0731c | 28 | #define PUTC(x) pc.putc(x) |
kenjiArai | 5:af9fa3d0731c | 29 | #define PRINTF(...) pc.printf(__VA_ARGS__) |
kenjiArai | 5:af9fa3d0731c | 30 | #define READABLE(x) pc.readable(x) |
kenjiArai | 5:af9fa3d0731c | 31 | #else |
kenjiArai | 5:af9fa3d0731c | 32 | #define BAUD(x) {;} |
kenjiArai | 5:af9fa3d0731c | 33 | #define GETC(x) {;} |
kenjiArai | 5:af9fa3d0731c | 34 | #define PUTC(x) {;} |
kenjiArai | 5:af9fa3d0731c | 35 | #define PRINTF(...) {;} |
kenjiArai | 5:af9fa3d0731c | 36 | #define READABLE(x) {;} |
kenjiArai | 5:af9fa3d0731c | 37 | #endif |
mio | 1:2a347c40b1da | 38 | |
kenjiArai | 5:af9fa3d0731c | 39 | // LPC1768 Frequency example |
kenjiArai | 5:af9fa3d0731c | 40 | // Outout mbed's "PWM6" pin to 96MHZ/19 = 5.052MHz (Approx) |
kenjiArai | 5:af9fa3d0731c | 41 | #define PWM_EAMPLE() PWM6_SETCLK(19) |
kenjiArai | 5:af9fa3d0731c | 42 | // Outout mbed's "PWM6" pin to 96MHZ/96 = 1.000MHz (Approx) |
kenjiArai | 5:af9fa3d0731c | 43 | //#define PWM_EAMPLE() PWM6_SETCLK(96) |
kenjiArai | 5:af9fa3d0731c | 44 | |
kenjiArai | 5:af9fa3d0731c | 45 | // Object ---------------------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 46 | DigitalOut led_gate(LED1); |
kenjiArai | 5:af9fa3d0731c | 47 | DigitalOut led_not_zero(LED2); |
kenjiArai | 5:af9fa3d0731c | 48 | DigitalOut led_01(LED3); |
kenjiArai | 5:af9fa3d0731c | 49 | DigitalOut led_10(LED4); |
kenjiArai | 5:af9fa3d0731c | 50 | DigitalIn sw_01(p19); |
kenjiArai | 5:af9fa3d0731c | 51 | DigitalIn sw_10(p20); |
kenjiArai | 5:af9fa3d0731c | 52 | Serial pc(USBTX, USBRX); |
kenjiArai | 5:af9fa3d0731c | 53 | I2C i2cBus(p9, p10); // SDA, SCL |
kenjiArai | 5:af9fa3d0731c | 54 | TextLCD_I2C_N lcd(&i2cBus, 0x7c, TextLCD::LCD8x2); // LCD(Akizuki AQM0802A) |
kenjiArai | 5:af9fa3d0731c | 55 | PwmOut fmclck(p21); // for RESERVE pin21 as PWM1[6] |
kenjiArai | 5:af9fa3d0731c | 56 | F_COUNTER fc(p30); |
mio | 0:c988614df67a | 57 | |
kenjiArai | 5:af9fa3d0731c | 58 | // RAM ------------------------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 59 | float freqency; |
kenjiArai | 5:af9fa3d0731c | 60 | float t_gate; |
kenjiArai | 5:af9fa3d0731c | 61 | uint8_t sw; |
kenjiArai | 5:af9fa3d0731c | 62 | |
kenjiArai | 5:af9fa3d0731c | 63 | // ROM / Constant data --------------------------------------------------------------------------- |
mio | 1:2a347c40b1da | 64 | |
kenjiArai | 5:af9fa3d0731c | 65 | // Function prototypes --------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 66 | |
kenjiArai | 5:af9fa3d0731c | 67 | // Function prototypes --------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 68 | |
kenjiArai | 5:af9fa3d0731c | 69 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 70 | // Control Program |
kenjiArai | 5:af9fa3d0731c | 71 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 5:af9fa3d0731c | 72 | #if defined(TARGET_LPC1768) |
mio | 1:2a347c40b1da | 73 | // Clock Output From pin21(PWM6) |
mio | 1:2a347c40b1da | 74 | // Set Clock Freq with div. |
mio | 1:2a347c40b1da | 75 | // if mbed is running at 96MHz, div is set 96 to Get 1MHz. |
mio | 1:2a347c40b1da | 76 | void PWM6_SETCLK(int div) |
mio | 1:2a347c40b1da | 77 | { |
kenjiArai | 5:af9fa3d0731c | 78 | LPC_PWM1->TCR = (1 << 1); // 1)Reset counter, disable PWM |
kenjiArai | 5:af9fa3d0731c | 79 | LPC_SC->PCLKSEL0 &= ~(0x3 << 12); |
kenjiArai | 5:af9fa3d0731c | 80 | LPC_SC->PCLKSEL0 |= (1 << 12); // 2)Set peripheral clock divider to /1, i.e. system clock |
kenjiArai | 5:af9fa3d0731c | 81 | LPC_PWM1->MR0 = div - 1; // 3)Match Register 0 is shared period counter for all PWM1 |
kenjiArai | 5:af9fa3d0731c | 82 | LPC_PWM1->MR6 = (div + 1)>> 1; // |
kenjiArai | 5:af9fa3d0731c | 83 | LPC_PWM1->LER |= 1; // 4)Start updating at next period start |
kenjiArai | 5:af9fa3d0731c | 84 | LPC_PWM1->TCR = (1 << 0) || (1 << 3); // 5)Enable counter and PWM |
kenjiArai | 5:af9fa3d0731c | 85 | } |
kenjiArai | 5:af9fa3d0731c | 86 | #else |
kenjiArai | 5:af9fa3d0731c | 87 | #error "No support for this CPU" |
kenjiArai | 5:af9fa3d0731c | 88 | #endif |
kenjiArai | 5:af9fa3d0731c | 89 | |
kenjiArai | 5:af9fa3d0731c | 90 | void example_freq_out(void) |
kenjiArai | 5:af9fa3d0731c | 91 | { |
kenjiArai | 5:af9fa3d0731c | 92 | PWM_EAMPLE(); |
kenjiArai | 5:af9fa3d0731c | 93 | } |
kenjiArai | 5:af9fa3d0731c | 94 | |
kenjiArai | 5:af9fa3d0731c | 95 | void read_sw_and_set_gate_time(void) |
kenjiArai | 5:af9fa3d0731c | 96 | { |
kenjiArai | 5:af9fa3d0731c | 97 | if (sw_10) { |
kenjiArai | 5:af9fa3d0731c | 98 | led_10 = 1; |
kenjiArai | 5:af9fa3d0731c | 99 | sw = 2; |
kenjiArai | 5:af9fa3d0731c | 100 | } else { |
kenjiArai | 5:af9fa3d0731c | 101 | led_10 = 0; |
kenjiArai | 5:af9fa3d0731c | 102 | sw = 0; |
kenjiArai | 5:af9fa3d0731c | 103 | } |
kenjiArai | 5:af9fa3d0731c | 104 | if (sw_01) { |
kenjiArai | 5:af9fa3d0731c | 105 | led_01 = 1; |
kenjiArai | 5:af9fa3d0731c | 106 | sw += 1; |
kenjiArai | 5:af9fa3d0731c | 107 | } else { |
kenjiArai | 5:af9fa3d0731c | 108 | led_01 = 0; |
kenjiArai | 5:af9fa3d0731c | 109 | } |
kenjiArai | 5:af9fa3d0731c | 110 | switch (sw) { |
kenjiArai | 5:af9fa3d0731c | 111 | case 0: |
kenjiArai | 5:af9fa3d0731c | 112 | t_gate = 0.001; |
kenjiArai | 5:af9fa3d0731c | 113 | break; |
kenjiArai | 5:af9fa3d0731c | 114 | case 1: |
kenjiArai | 5:af9fa3d0731c | 115 | t_gate = 0.01; |
kenjiArai | 5:af9fa3d0731c | 116 | break; |
kenjiArai | 5:af9fa3d0731c | 117 | case 2: |
kenjiArai | 5:af9fa3d0731c | 118 | t_gate = 0.1; |
kenjiArai | 5:af9fa3d0731c | 119 | break; |
kenjiArai | 5:af9fa3d0731c | 120 | case 3: |
kenjiArai | 5:af9fa3d0731c | 121 | default: |
kenjiArai | 5:af9fa3d0731c | 122 | t_gate = 1.0; |
kenjiArai | 5:af9fa3d0731c | 123 | break; |
kenjiArai | 5:af9fa3d0731c | 124 | } |
mio | 1:2a347c40b1da | 125 | } |
mio | 0:c988614df67a | 126 | |
kenjiArai | 5:af9fa3d0731c | 127 | int main() |
kenjiArai | 5:af9fa3d0731c | 128 | { |
kenjiArai | 5:af9fa3d0731c | 129 | PRINTF("Frequency Counter by JH1PJL created on "__DATE__"\r\n"); |
kenjiArai | 5:af9fa3d0731c | 130 | t_gate = 1.0; |
kenjiArai | 5:af9fa3d0731c | 131 | // Initialize LCD |
kenjiArai | 5:af9fa3d0731c | 132 | lcd.locate(0, 0); // 1st line top |
kenjiArai | 5:af9fa3d0731c | 133 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 134 | lcd.printf("Fre-Cntr"); |
kenjiArai | 5:af9fa3d0731c | 135 | lcd.locate(0, 1); // 2nd line top |
kenjiArai | 5:af9fa3d0731c | 136 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 137 | lcd.puts(" JH1PJL "); |
kenjiArai | 5:af9fa3d0731c | 138 | lcd.setContrast(0x16); |
kenjiArai | 5:af9fa3d0731c | 139 | wait(5.0); |
kenjiArai | 5:af9fa3d0731c | 140 | lcd.locate(0, 1); // 2nd line top |
kenjiArai | 5:af9fa3d0731c | 141 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 142 | lcd.puts(" "); |
kenjiArai | 5:af9fa3d0731c | 143 | // Set PWM for example |
kenjiArai | 5:af9fa3d0731c | 144 | example_freq_out(); |
kenjiArai | 5:af9fa3d0731c | 145 | freqency = 0; |
kenjiArai | 5:af9fa3d0731c | 146 | while(true) { |
kenjiArai | 5:af9fa3d0731c | 147 | led_gate = 1; |
kenjiArai | 5:af9fa3d0731c | 148 | freqency = (float)fc.read_frequency(t_gate); |
kenjiArai | 5:af9fa3d0731c | 149 | led_gate = 0; |
kenjiArai | 5:af9fa3d0731c | 150 | wait(1.1 - t_gate); |
kenjiArai | 5:af9fa3d0731c | 151 | if (freqency == 0) { |
kenjiArai | 5:af9fa3d0731c | 152 | led_not_zero = 1; |
kenjiArai | 5:af9fa3d0731c | 153 | } else { |
kenjiArai | 5:af9fa3d0731c | 154 | led_not_zero = 0; |
kenjiArai | 5:af9fa3d0731c | 155 | } |
kenjiArai | 5:af9fa3d0731c | 156 | read_sw_and_set_gate_time(); |
kenjiArai | 5:af9fa3d0731c | 157 | PRINTF("p30 f = %9.0f [Hz] gate %4.3f [Sec]\r\n", freqency/t_gate, t_gate); |
kenjiArai | 5:af9fa3d0731c | 158 | lcd.locate(0, 0); // 1st line top |
kenjiArai | 5:af9fa3d0731c | 159 | lcd.printf("%8.0f", freqency); |
kenjiArai | 5:af9fa3d0731c | 160 | lcd.locate(0, 1); // 2nd line top |
kenjiArai | 5:af9fa3d0731c | 161 | switch (sw) { |
kenjiArai | 5:af9fa3d0731c | 162 | case 0: |
kenjiArai | 5:af9fa3d0731c | 163 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 164 | lcd.printf("x1000 Hz"); |
kenjiArai | 5:af9fa3d0731c | 165 | break; |
kenjiArai | 5:af9fa3d0731c | 166 | case 1: |
kenjiArai | 5:af9fa3d0731c | 167 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 168 | lcd.printf("x100 Hz"); |
kenjiArai | 5:af9fa3d0731c | 169 | break; |
kenjiArai | 5:af9fa3d0731c | 170 | case 2: |
kenjiArai | 5:af9fa3d0731c | 171 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 172 | lcd.printf("x10 Hz"); |
kenjiArai | 5:af9fa3d0731c | 173 | break; |
kenjiArai | 5:af9fa3d0731c | 174 | case 3: |
kenjiArai | 5:af9fa3d0731c | 175 | default: |
kenjiArai | 5:af9fa3d0731c | 176 | // 12345678 |
kenjiArai | 5:af9fa3d0731c | 177 | lcd.printf("x1 Hz"); |
kenjiArai | 5:af9fa3d0731c | 178 | break; |
kenjiArai | 5:af9fa3d0731c | 179 | } |
mio | 1:2a347c40b1da | 180 | } |
mio | 1:2a347c40b1da | 181 | } |