chiark / gitweb /
Fixed the requirement for -lm
[wiringPi.git] / examples / isr-osc.c
1 /*
2  * isr-osc.c:
3  *      Wait for Interrupt test program - ISR method - interrupt oscillator
4  *
5  *      How to test:
6  *
7  *      IMPORTANT: To run this test we connect 2 GPIO pins together, but
8  *      before we do that YOU must make sure that they are both setup
9  *      the right way. If they are set to outputs and one is high and one low,
10  *      then you connect the wire, you'll create a short and that won't be good.
11  *
12  *      Before making the connection, type:
13  *              gpio mode 0 output
14  *              gpio write 0 0
15  *              gpio mode 1 input
16  *      then you can connect them together.
17  *
18  *      Run the program, then:
19  *              gpio write 0 1
20  *              gpio write 0 0
21  *
22  *      at which point it will trigger an interrupt and the program will
23  *      then do the up/down toggling for itself and run at full speed, and
24  *      it will report the number of interrupts recieved every second.
25  *
26  *      Copyright (c) 2013 Gordon Henderson. projects@drogon.net
27  ***********************************************************************
28  * This file is part of wiringPi:
29  *      https://projects.drogon.net/raspberry-pi/wiringpi/
30  *
31  *    wiringPi is free software: you can redistribute it and/or modify
32  *    it under the terms of the GNU Lesser General Public License as published by
33  *    the Free Software Foundation, either version 3 of the License, or
34  *    (at your option) any later version.
35  *
36  *    wiringPi is distributed in the hope that it will be useful,
37  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
38  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39  *    GNU Lesser General Public License for more details.
40  *
41  *    You should have received a copy of the GNU Lesser General Public License
42  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
43  ***********************************************************************
44  */
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <stdlib.h>
50 #include <wiringPi.h>
51
52
53 // What GPIO input are we using?
54 //      This is a wiringPi pin number
55
56 #define OUT_PIN         0
57 #define IN_PIN          1
58
59 // globalCounter:
60 //      Global variable to count interrupts
61 //      Should be declared volatile to make sure the compiler doesn't cache it.
62
63 static volatile int globalCounter = 0 ;
64
65 /*
66  * myInterrupt:
67  *********************************************************************************
68  */
69
70 void myInterrupt (void)
71 {
72   digitalWrite (OUT_PIN, 1) ;
73   ++globalCounter ;
74   digitalWrite (OUT_PIN, 0) ;
75 }
76
77
78 /*
79  *********************************************************************************
80  * main
81  *********************************************************************************
82  */
83
84 int main (void)
85 {
86   int myCounter   = 0 ;
87   int lastCounter = 0 ;
88
89   if (wiringPiSetup () < 0)
90   {
91     fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
92     return 1 ;
93   }
94
95   pinMode (OUT_PIN, OUTPUT) ;
96   pinMode (IN_PIN,  INPUT) ;
97
98   if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
99   {
100     fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
101     return 1 ;
102   }
103
104   for (;;)
105   {
106     printf ("Waiting ... ") ; fflush (stdout) ;
107
108     while (myCounter == globalCounter)
109       delay (1000) ;
110
111     printf (" Done. counter: %6d: %6d\n",
112                 globalCounter, myCounter - lastCounter) ;
113     lastCounter = myCounter ;
114     myCounter   = globalCounter ;
115   }
116
117   return 0 ;
118 }