test interruptIn feature with 555 timer

Dependencies:   ClockControl PowerControl mbed

Fork of Frequency_counter by Satoru Yoshida

Test interruptIn function with using LMC555 timer IC

LPC 1768 自身が持つタイマー割り込み機能を使わずに、 あえて外部のタイマー IC 555 からの信号で周期的に割り込みするテスト。

下記の回路例では、555 の output 端子が 約 10 秒周期のうち、0.3 秒間 Low になるはず。 ただし、コンデンサーおよび抵抗の誤差の影響があり、実測値は約7秒周期

C を 47μ から 330 μ に変えると約1分前後で周期的に割り込みがかかります。

/media/uploads/strysd/int555_10s.png

なるべく配線が交差しないように部品配置してみた例

/media/uploads/strysd/555test.jpg

Revision:
4:64d9c0bed75b
Parent:
3:bf0a5effbed2
Child:
5:9be3080ca8ba
--- a/main.cpp	Mon Jun 03 12:36:37 2013 +0000
+++ b/main.cpp	Mon Jun 03 15:04:08 2013 +0000
@@ -1,37 +1,44 @@
 #include "mbed.h"
 
 //see mbed.org/users/strysd/notebook/measure_humidity_with_hs15p_and_lmc555/
-#define ohm_constant  7.2 //if use 0.1 micro F
-#define in_limit 20 //limit of interrupt times
+#define CONV_KILO_OHM 0.0072 //if use 0.1 micro F
+#define IN_TIMES  10 //interrupt times
+#define READ_TIMER 3 //rest times when reads timer
+#define WAIT_IN 200 //wait interrupts in msec
+#define WAIT_NEXT 4800 //wait next measurement in msec
+#define MAX_INTERVAL 100000 //  > 0.1 sec ( < 10 Hz)  in microseconds
+#define MIN_INTERVAL 20 // < 20 micro sec ( > 50 kHz) in microseconds
+#define ONESEC_BY_MICRO 1000000//   1 sec             in microseconds
 
-InterruptIn in(p8);
-DigitalOut vcc555(p9);
+InterruptIn in(p8);   //connect to the Discharge of 555
+DigitalOut run555(p9);//connect to the Reset of 555
 DigitalOut led(LED1);
 Timer t1;
 
-float t_period = 0;//between interrupts in microseconds
+float my_interval = 0;//between interrupts in microseconds
 float my_freq = 0;//Hz
-float my_ohm = 0;//ohm for HS15P
-int   in_count = 0;//interrupt times
+float my_ohm  = 0;//ohm for HS15P
+int   rest_in = 0;//rest of interrupt times
 
 void flip(void)
 {
-    if(in_count > 0){
-        if(in_count > 15){
-            t_period = t1.read_us();// Get time since last interrupt
+    if(rest_in > 0){
+        if(rest_in < READ_TIMER){
+            my_interval = t1.read_us();// Get time since last interrupt
         }
         t1.reset();// Reset timer and wait for next interrupt
-        in_count--;
-    } else {
+        rest_in--;
+    }
+    
+    if(rest_in == 0){
         //No use interrupt
-        vcc555 = led = 0;
+        run555 = led = 0;
         t1.stop();
     }
 }
 
-
 int main() {
-    vcc555 = led = 0;
+    run555 = led = 0;
     in.mode(PullUp);
     in.rise(&flip);// Set up the interrupt for rising edge
 
@@ -39,24 +46,21 @@
       
     while (1) {
         //Use interrupt
-        t_period = 0;
+        my_interval = 0;
         t1.start();
-        in_count = in_limit;
-        vcc555 = led = 1;
-        wait_ms(200);
+        rest_in = IN_TIMES;
+        run555 = led = 1;
+        wait_ms(WAIT_IN);
 
-        if (t_period < 20 || t_period > 100000){
-            //< 20 micro sec (> 50 kHz)
-            //> 0.1 sec (< 10 Hz)
+        if (my_interval < MIN_INTERVAL || my_interval > MAX_INTERVAL){
             printf("\r not ready\n");
-            wait_ms(5000);
         } else {
-            my_freq = 1000000/t_period;// Convert to Hz
-            my_ohm  = t_period * ohm_constant / 1000;// Convert to kilo ohm
+            my_freq = ONESEC_BY_MICRO / my_interval;  // Convert to Hz
+            my_ohm  = my_interval     * CONV_KILO_OHM;// Convert to kilo ohm
             printf("\r %d Hz, %d micro sec, %4.1f kilo ohm \n",
-                   (int)my_freq, (int)t_period, my_ohm);
+                   (int)my_freq, (int)my_interval, my_ohm);
         }
 
-         wait_ms(4800);
+         wait_ms(WAIT_NEXT);
     }
 }
\ No newline at end of file