test interruptIn feature with 555 timer
Dependencies: ClockControl PowerControl mbed
Fork of Frequency_counter by
Test interruptIn function with using LMC555 timer IC
LPC 1768 自身が持つタイマー割り込み機能を使わずに、 あえて外部のタイマー IC 555 からの信号で周期的に割り込みするテスト。
下記の回路例では、555 の output 端子が 約 10 秒周期のうち、0.3 秒間 Low になるはず。 ただし、コンデンサーおよび抵抗の誤差の影響があり、実測値は約7秒周期
C を 47μ から 330 μ に変えると約1分前後で周期的に割り込みがかかります。
なるべく配線が交差しないように部品配置してみた例
main.cpp@6:7f90a61e1a69, 2013-08-11 (annotated)
- Committer:
- strysd
- Date:
- Sun Aug 11 08:34:00 2013 +0000
- Revision:
- 6:7f90a61e1a69
- Parent:
- 5:9be3080ca8ba
- Child:
- 7:42e7f18361c8
separate defines
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
strysd | 6:7f90a61e1a69 | 1 | //see mbed.org/users/strysd/notebook/measure_humidity_with_hs15p_and_lmc555/ |
Neel | 0:c5bcf643a8b1 | 2 | #include "mbed.h" |
strysd | 6:7f90a61e1a69 | 3 | #include "main.h" |
strysd | 1:e2034bcdb101 | 4 | |
strysd | 4:64d9c0bed75b | 5 | InterruptIn in(p8); //connect to the Discharge of 555 |
strysd | 5:9be3080ca8ba | 6 | DigitalOut run555(p9);//connect to the Reset of 555 |
Neel | 0:c5bcf643a8b1 | 7 | DigitalOut led(LED1); |
Neel | 0:c5bcf643a8b1 | 8 | Timer t1; |
Neel | 0:c5bcf643a8b1 | 9 | |
strysd | 4:64d9c0bed75b | 10 | float my_interval = 0;//between interrupts in microseconds |
strysd | 1:e2034bcdb101 | 11 | float my_freq = 0;//Hz |
strysd | 4:64d9c0bed75b | 12 | float my_ohm = 0;//ohm for HS15P |
strysd | 4:64d9c0bed75b | 13 | int rest_in = 0;//rest of interrupt times |
Neel | 0:c5bcf643a8b1 | 14 | |
Neel | 0:c5bcf643a8b1 | 15 | void flip(void) |
Neel | 0:c5bcf643a8b1 | 16 | { |
strysd | 6:7f90a61e1a69 | 17 | if(rest_in < READ_TIMER){ |
strysd | 6:7f90a61e1a69 | 18 | my_interval = t1.read_us();// Get time since last interrupt |
strysd | 4:64d9c0bed75b | 19 | } |
strysd | 5:9be3080ca8ba | 20 | |
strysd | 6:7f90a61e1a69 | 21 | t1.reset();// Reset timer and wait for next interrupt |
strysd | 6:7f90a61e1a69 | 22 | rest_in--; |
strysd | 6:7f90a61e1a69 | 23 | |
strysd | 6:7f90a61e1a69 | 24 | if(rest_in <= 0){ |
strysd | 1:e2034bcdb101 | 25 | //No use interrupt |
strysd | 4:64d9c0bed75b | 26 | run555 = led = 0; |
strysd | 1:e2034bcdb101 | 27 | t1.stop(); |
strysd | 1:e2034bcdb101 | 28 | } |
Neel | 0:c5bcf643a8b1 | 29 | } |
Neel | 0:c5bcf643a8b1 | 30 | |
Neel | 0:c5bcf643a8b1 | 31 | int main() { |
strysd | 4:64d9c0bed75b | 32 | run555 = led = 0; |
strysd | 5:9be3080ca8ba | 33 | // Set up the interrupt |
strysd | 2:c4d4f0f9d77c | 34 | in.mode(PullUp); |
strysd | 6:7f90a61e1a69 | 35 | in.rise(&flip); |
strysd | 1:e2034bcdb101 | 36 | |
strysd | 3:bf0a5effbed2 | 37 | printf("\rStarting measure\n"); |
strysd | 5:9be3080ca8ba | 38 | |
Neel | 0:c5bcf643a8b1 | 39 | while (1) { |
strysd | 1:e2034bcdb101 | 40 | //Use interrupt |
strysd | 4:64d9c0bed75b | 41 | my_interval = 0; |
strysd | 1:e2034bcdb101 | 42 | t1.start(); |
strysd | 4:64d9c0bed75b | 43 | rest_in = IN_TIMES; |
strysd | 4:64d9c0bed75b | 44 | run555 = led = 1; |
strysd | 4:64d9c0bed75b | 45 | wait_ms(WAIT_IN); |
strysd | 6:7f90a61e1a69 | 46 | run555 = led = 0;//force stop |
strysd | 3:bf0a5effbed2 | 47 | |
strysd | 4:64d9c0bed75b | 48 | if (my_interval < MIN_INTERVAL || my_interval > MAX_INTERVAL){ |
strysd | 3:bf0a5effbed2 | 49 | printf("\r not ready\n"); |
strysd | 1:e2034bcdb101 | 50 | } else { |
strysd | 4:64d9c0bed75b | 51 | my_freq = ONESEC_BY_MICRO / my_interval; // Convert to Hz |
strysd | 4:64d9c0bed75b | 52 | my_ohm = my_interval * CONV_KILO_OHM;// Convert to kilo ohm |
strysd | 6:7f90a61e1a69 | 53 | printf("\r %.0f Hz, %.0f micro sec, %.1f kilo ohm \n", |
strysd | 6:7f90a61e1a69 | 54 | my_freq, my_interval, my_ohm); |
strysd | 1:e2034bcdb101 | 55 | } |
strysd | 3:bf0a5effbed2 | 56 | |
strysd | 4:64d9c0bed75b | 57 | wait_ms(WAIT_NEXT); |
Neel | 0:c5bcf643a8b1 | 58 | } |
Neel | 0:c5bcf643a8b1 | 59 | } |