Freq calculations using counters and interrupt
Dependencies: FastPWM PwmIn USBDevice mbed
main.cpp@0:6d4f91decc4f, 2014-11-13 (annotated)
- Committer:
- Ellor1
- Date:
- Thu Nov 13 13:26:08 2014 +0000
- Revision:
- 0:6d4f91decc4f
Freq calculation of signal using counters and interrupt routines
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ellor1 | 0:6d4f91decc4f | 1 | #include "FastPWM.h" |
Ellor1 | 0:6d4f91decc4f | 2 | #include "mbed.h" |
Ellor1 | 0:6d4f91decc4f | 3 | #include "PwmIn.h" |
Ellor1 | 0:6d4f91decc4f | 4 | #include "USBSerial.h" |
Ellor1 | 0:6d4f91decc4f | 5 | |
Ellor1 | 0:6d4f91decc4f | 6 | // using fastpwm signal as a clock signal to be used to trigger the counter whilst the signal I want to measure is high. |
Ellor1 | 0:6d4f91decc4f | 7 | // to get this to work properly I believe I will need to use nested interrupts along with setting interupt priorities. This is so that |
Ellor1 | 0:6d4f91decc4f | 8 | // when the signal I want to read (led) causes an interrupt on a rising edge whilst this value is high I want the clock trigger to interrupt a seperate pin |
Ellor1 | 0:6d4f91decc4f | 9 | // which will continuoulsy count until the (led) signal causes another interrupt by going low. |
Ellor1 | 0:6d4f91decc4f | 10 | |
Ellor1 | 0:6d4f91decc4f | 11 | // clkin is currently set as a DigitalIn but this likely needs to become an interrupt. |
Ellor1 | 0:6d4f91decc4f | 12 | |
Ellor1 | 0:6d4f91decc4f | 13 | USBSerial serial; |
Ellor1 | 0:6d4f91decc4f | 14 | |
Ellor1 | 0:6d4f91decc4f | 15 | FastPWM clkoutput(p20); |
Ellor1 | 0:6d4f91decc4f | 16 | DigitalIn clkin(p8); |
Ellor1 | 0:6d4f91decc4f | 17 | InterruptIn led(p7); |
Ellor1 | 0:6d4f91decc4f | 18 | Ticker display; |
Ellor1 | 0:6d4f91decc4f | 19 | |
Ellor1 | 0:6d4f91decc4f | 20 | int count; |
Ellor1 | 0:6d4f91decc4f | 21 | int count_2; |
Ellor1 | 0:6d4f91decc4f | 22 | int t_rise; |
Ellor1 | 0:6d4f91decc4f | 23 | int t_fall; |
Ellor1 | 0:6d4f91decc4f | 24 | int t_period = 0; |
Ellor1 | 0:6d4f91decc4f | 25 | |
Ellor1 | 0:6d4f91decc4f | 26 | |
Ellor1 | 0:6d4f91decc4f | 27 | void clock_pulse() { |
Ellor1 | 0:6d4f91decc4f | 28 | |
Ellor1 | 0:6d4f91decc4f | 29 | clkoutput.period(8e-7); //around 1.2 MHz signal |
Ellor1 | 0:6d4f91decc4f | 30 | clkoutput.write(0.5f); // 50% duty cycle |
Ellor1 | 0:6d4f91decc4f | 31 | } |
Ellor1 | 0:6d4f91decc4f | 32 | |
Ellor1 | 0:6d4f91decc4f | 33 | |
Ellor1 | 0:6d4f91decc4f | 34 | void rise() { // rising edge interrupt routine |
Ellor1 | 0:6d4f91decc4f | 35 | |
Ellor1 | 0:6d4f91decc4f | 36 | while (led && clkin) { // whilst the signal I want to measure is high (led) and the clk signal (clkin) is high increment the counter |
Ellor1 | 0:6d4f91decc4f | 37 | // if (clkin) { |
Ellor1 | 0:6d4f91decc4f | 38 | count++; |
Ellor1 | 0:6d4f91decc4f | 39 | } |
Ellor1 | 0:6d4f91decc4f | 40 | // if (led && clkoutput) { |
Ellor1 | 0:6d4f91decc4f | 41 | |
Ellor1 | 0:6d4f91decc4f | 42 | // count++; |
Ellor1 | 0:6d4f91decc4f | 43 | // while (led) { |
Ellor1 | 0:6d4f91decc4f | 44 | // if (clkin) { |
Ellor1 | 0:6d4f91decc4f | 45 | // count++; |
Ellor1 | 0:6d4f91decc4f | 46 | // } |
Ellor1 | 0:6d4f91decc4f | 47 | t_fall = count_2; |
Ellor1 | 0:6d4f91decc4f | 48 | // count_2 = 0; |
Ellor1 | 0:6d4f91decc4f | 49 | } |
Ellor1 | 0:6d4f91decc4f | 50 | // } |
Ellor1 | 0:6d4f91decc4f | 51 | |
Ellor1 | 0:6d4f91decc4f | 52 | void fall() { // falling edge interrupt routine |
Ellor1 | 0:6d4f91decc4f | 53 | |
Ellor1 | 0:6d4f91decc4f | 54 | while (!led && clkin) { // whilst the signal I want to measure is low and the clk signal is high increment counter 2 |
Ellor1 | 0:6d4f91decc4f | 55 | count_2++; |
Ellor1 | 0:6d4f91decc4f | 56 | } |
Ellor1 | 0:6d4f91decc4f | 57 | t_rise = count; |
Ellor1 | 0:6d4f91decc4f | 58 | // count = 0; |
Ellor1 | 0:6d4f91decc4f | 59 | |
Ellor1 | 0:6d4f91decc4f | 60 | } |
Ellor1 | 0:6d4f91decc4f | 61 | |
Ellor1 | 0:6d4f91decc4f | 62 | // } |
Ellor1 | 0:6d4f91decc4f | 63 | |
Ellor1 | 0:6d4f91decc4f | 64 | /*void period() { |
Ellor1 | 0:6d4f91decc4f | 65 | t_period = count; |
Ellor1 | 0:6d4f91decc4f | 66 | count = 0; |
Ellor1 | 0:6d4f91decc4f | 67 | } |
Ellor1 | 0:6d4f91decc4f | 68 | */ |
Ellor1 | 0:6d4f91decc4f | 69 | |
Ellor1 | 0:6d4f91decc4f | 70 | void disp() { |
Ellor1 | 0:6d4f91decc4f | 71 | |
Ellor1 | 0:6d4f91decc4f | 72 | serial.printf("count is %d \n\r" , t_rise); |
Ellor1 | 0:6d4f91decc4f | 73 | serial.printf("count_2 is %d \n\r" , t_fall); |
Ellor1 | 0:6d4f91decc4f | 74 | } |
Ellor1 | 0:6d4f91decc4f | 75 | |
Ellor1 | 0:6d4f91decc4f | 76 | |
Ellor1 | 0:6d4f91decc4f | 77 | int main() |
Ellor1 | 0:6d4f91decc4f | 78 | { |
Ellor1 | 0:6d4f91decc4f | 79 | clock_pulse(); |
Ellor1 | 0:6d4f91decc4f | 80 | |
Ellor1 | 0:6d4f91decc4f | 81 | led.rise(&rise); // set rising edge interrupt and call rise function |
Ellor1 | 0:6d4f91decc4f | 82 | led.fall(&fall); // set falling edge interrupt and call fall function |
Ellor1 | 0:6d4f91decc4f | 83 | //count = 0; |
Ellor1 | 0:6d4f91decc4f | 84 | // } |
Ellor1 | 0:6d4f91decc4f | 85 | |
Ellor1 | 0:6d4f91decc4f | 86 | display.attach(&disp, 2); // ticker function to display results every 2 seconds |
Ellor1 | 0:6d4f91decc4f | 87 | |
Ellor1 | 0:6d4f91decc4f | 88 | while (1) { |
Ellor1 | 0:6d4f91decc4f | 89 | |
Ellor1 | 0:6d4f91decc4f | 90 | // while (led) { |
Ellor1 | 0:6d4f91decc4f | 91 | // if (clkin) { |
Ellor1 | 0:6d4f91decc4f | 92 | // count++; |
Ellor1 | 0:6d4f91decc4f | 93 | // } |
Ellor1 | 0:6d4f91decc4f | 94 | // count = 0; |
Ellor1 | 0:6d4f91decc4f | 95 | //} |
Ellor1 | 0:6d4f91decc4f | 96 | |
Ellor1 | 0:6d4f91decc4f | 97 | // if (led == 1) && (clkoutput == 1) { |
Ellor1 | 0:6d4f91decc4f | 98 | |
Ellor1 | 0:6d4f91decc4f | 99 | // count++; |
Ellor1 | 0:6d4f91decc4f | 100 | |
Ellor1 | 0:6d4f91decc4f | 101 | /* |
Ellor1 | 0:6d4f91decc4f | 102 | |
Ellor1 | 0:6d4f91decc4f | 103 | while (led) { |
Ellor1 | 0:6d4f91decc4f | 104 | |
Ellor1 | 0:6d4f91decc4f | 105 | if (clkin) { |
Ellor1 | 0:6d4f91decc4f | 106 | |
Ellor1 | 0:6d4f91decc4f | 107 | count++; |
Ellor1 | 0:6d4f91decc4f | 108 | } |
Ellor1 | 0:6d4f91decc4f | 109 | |
Ellor1 | 0:6d4f91decc4f | 110 | */ |
Ellor1 | 0:6d4f91decc4f | 111 | |
Ellor1 | 0:6d4f91decc4f | 112 | |
Ellor1 | 0:6d4f91decc4f | 113 | } |
Ellor1 | 0:6d4f91decc4f | 114 | |
Ellor1 | 0:6d4f91decc4f | 115 | //serial.printf("Pulsewidth is %.2f \n\r" , pa.pulsewidth()); |
Ellor1 | 0:6d4f91decc4f | 116 | |
Ellor1 | 0:6d4f91decc4f | 117 | |
Ellor1 | 0:6d4f91decc4f | 118 | |
Ellor1 | 0:6d4f91decc4f | 119 | |
Ellor1 | 0:6d4f91decc4f | 120 | |
Ellor1 | 0:6d4f91decc4f | 121 | } |
Ellor1 | 0:6d4f91decc4f | 122 |