Callum Ellor
/
freq_2_pins_calc
frequency calculation of incoming signal using timers
main.cpp@0:ab61f1a6b20b, 2014-11-13 (annotated)
- Committer:
- Ellor1
- Date:
- Thu Nov 13 13:23:26 2014 +0000
- Revision:
- 0:ab61f1a6b20b
Freq_calc using timers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ellor1 | 0:ab61f1a6b20b | 1 | #include "mbed.h" |
Ellor1 | 0:ab61f1a6b20b | 2 | #include "USBSerial.h" |
Ellor1 | 0:ab61f1a6b20b | 3 | |
Ellor1 | 0:ab61f1a6b20b | 4 | //this program uses timers to time how long the signal is high and how long the signal is low. this program manages to accurately read the signal within 1-2us on each pulsewidth |
Ellor1 | 0:ab61f1a6b20b | 5 | // giving around a 3us loss in accuracy for the whole period. |
Ellor1 | 0:ab61f1a6b20b | 6 | // the program starts the timer on the rising edge of the signal and stops the timer on teh falling edge. I found that |
Ellor1 | 0:ab61f1a6b20b | 7 | // my readings are more accurate if i read the negative pulsewidth on a seperate pin and (int_pin_2) and I put this through a NOT gate. |
Ellor1 | 0:ab61f1a6b20b | 8 | // the NOT gate would liekly introduce some propagation delay but after monitoring the signal on the scope this didnt seem to be the case. |
Ellor1 | 0:ab61f1a6b20b | 9 | // Ideally if I could take a sample of the timer values and take an average my results would be better. After trying this, adding more code |
Ellor1 | 0:ab61f1a6b20b | 10 | // decreased teh accuracy of the results as more lines of code needed to be executed. |
Ellor1 | 0:ab61f1a6b20b | 11 | |
Ellor1 | 0:ab61f1a6b20b | 12 | USBSerial serial; |
Ellor1 | 0:ab61f1a6b20b | 13 | InterruptIn int_pin(P1_14); |
Ellor1 | 0:ab61f1a6b20b | 14 | InterruptIn int_pin_2(P1_24); |
Ellor1 | 0:ab61f1a6b20b | 15 | Timer t1; |
Ellor1 | 0:ab61f1a6b20b | 16 | Timer t2; |
Ellor1 | 0:ab61f1a6b20b | 17 | Ticker display; |
Ellor1 | 0:ab61f1a6b20b | 18 | |
Ellor1 | 0:ab61f1a6b20b | 19 | int t_period = 0; // This is the period between interrupts in microseconds |
Ellor1 | 0:ab61f1a6b20b | 20 | int t_period_rise = 0; |
Ellor1 | 0:ab61f1a6b20b | 21 | int t_period_fall = 0; |
Ellor1 | 0:ab61f1a6b20b | 22 | int t_freq = 0; |
Ellor1 | 0:ab61f1a6b20b | 23 | int results[30]; |
Ellor1 | 0:ab61f1a6b20b | 24 | int i =0; |
Ellor1 | 0:ab61f1a6b20b | 25 | float k =0; |
Ellor1 | 0:ab61f1a6b20b | 26 | |
Ellor1 | 0:ab61f1a6b20b | 27 | void rise() |
Ellor1 | 0:ab61f1a6b20b | 28 | { |
Ellor1 | 0:ab61f1a6b20b | 29 | t1.start(); // start rising edge timer |
Ellor1 | 0:ab61f1a6b20b | 30 | } |
Ellor1 | 0:ab61f1a6b20b | 31 | |
Ellor1 | 0:ab61f1a6b20b | 32 | void fall() |
Ellor1 | 0:ab61f1a6b20b | 33 | { |
Ellor1 | 0:ab61f1a6b20b | 34 | t1.stop(); // stop rising edge timer |
Ellor1 | 0:ab61f1a6b20b | 35 | t_period_rise = t1.read_us(); // read timer in us |
Ellor1 | 0:ab61f1a6b20b | 36 | t1.reset(); |
Ellor1 | 0:ab61f1a6b20b | 37 | } |
Ellor1 | 0:ab61f1a6b20b | 38 | |
Ellor1 | 0:ab61f1a6b20b | 39 | void rise_2() |
Ellor1 | 0:ab61f1a6b20b | 40 | { |
Ellor1 | 0:ab61f1a6b20b | 41 | t2.start(); //start falling edge timer (on rising edge of pulse after inverted by NOT gate) |
Ellor1 | 0:ab61f1a6b20b | 42 | } |
Ellor1 | 0:ab61f1a6b20b | 43 | void fall_2() |
Ellor1 | 0:ab61f1a6b20b | 44 | { |
Ellor1 | 0:ab61f1a6b20b | 45 | t2.stop(); |
Ellor1 | 0:ab61f1a6b20b | 46 | t_period_fall = t2.read_us(); |
Ellor1 | 0:ab61f1a6b20b | 47 | t2.reset(); |
Ellor1 | 0:ab61f1a6b20b | 48 | } |
Ellor1 | 0:ab61f1a6b20b | 49 | |
Ellor1 | 0:ab61f1a6b20b | 50 | void disp() |
Ellor1 | 0:ab61f1a6b20b | 51 | { |
Ellor1 | 0:ab61f1a6b20b | 52 | |
Ellor1 | 0:ab61f1a6b20b | 53 | // did have serial print here |
Ellor1 | 0:ab61f1a6b20b | 54 | |
Ellor1 | 0:ab61f1a6b20b | 55 | } |
Ellor1 | 0:ab61f1a6b20b | 56 | |
Ellor1 | 0:ab61f1a6b20b | 57 | |
Ellor1 | 0:ab61f1a6b20b | 58 | |
Ellor1 | 0:ab61f1a6b20b | 59 | |
Ellor1 | 0:ab61f1a6b20b | 60 | |
Ellor1 | 0:ab61f1a6b20b | 61 | int main() { |
Ellor1 | 0:ab61f1a6b20b | 62 | |
Ellor1 | 0:ab61f1a6b20b | 63 | |
Ellor1 | 0:ab61f1a6b20b | 64 | int_pin.rise(&rise); // Set up the interrupt for rising edge |
Ellor1 | 0:ab61f1a6b20b | 65 | int_pin.fall(&fall); |
Ellor1 | 0:ab61f1a6b20b | 66 | int_pin_2.rise(&rise_2); |
Ellor1 | 0:ab61f1a6b20b | 67 | int_pin_2.fall(&fall_2); |
Ellor1 | 0:ab61f1a6b20b | 68 | //diaplay.attach(&disp, 2) |
Ellor1 | 0:ab61f1a6b20b | 69 | |
Ellor1 | 0:ab61f1a6b20b | 70 | while (1) { |
Ellor1 | 0:ab61f1a6b20b | 71 | i = ((t_period_rise) + (t_period_fall)); |
Ellor1 | 0:ab61f1a6b20b | 72 | k = (1/(float)i)*1000000; |
Ellor1 | 0:ab61f1a6b20b | 73 | wait(3); |
Ellor1 | 0:ab61f1a6b20b | 74 | |
Ellor1 | 0:ab61f1a6b20b | 75 | serial.printf("\rPeriod is %d \n", i); |
Ellor1 | 0:ab61f1a6b20b | 76 | serial.printf("\rFrequency is %.2f \n", k); |
Ellor1 | 0:ab61f1a6b20b | 77 | serial.printf("\rPeriod_rise is %d \n", t_period_rise); |
Ellor1 | 0:ab61f1a6b20b | 78 | serial.printf("\rPeriod_fall is %d \n", t_period_fall); |
Ellor1 | 0:ab61f1a6b20b | 79 | |
Ellor1 | 0:ab61f1a6b20b | 80 | } |
Ellor1 | 0:ab61f1a6b20b | 81 | |
Ellor1 | 0:ab61f1a6b20b | 82 | } |
Ellor1 | 0:ab61f1a6b20b | 83 | |
Ellor1 | 0:ab61f1a6b20b | 84 | //} |