isr-osc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <stdio.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <stdlib.h>
  49. #include <wiringPi.h>
  50. // What GPIO input are we using?
  51. // This is a wiringPi pin number
  52. #define OUT_PIN 0
  53. #define IN_PIN 1
  54. // globalCounter:
  55. // Global variable to count interrupts
  56. // Should be declared volatile to make sure the compiler doesn't cache it.
  57. static volatile int globalCounter = 0 ;
  58. /*
  59. * myInterrupt:
  60. *********************************************************************************
  61. */
  62. void myInterrupt (void)
  63. {
  64. digitalWrite (OUT_PIN, 1) ;
  65. ++globalCounter ;
  66. digitalWrite (OUT_PIN, 0) ;
  67. }
  68. /*
  69. *********************************************************************************
  70. * main
  71. *********************************************************************************
  72. */
  73. int main (void)
  74. {
  75. int myCounter = 0 ;
  76. int lastCounter = 0 ;
  77. if (wiringPiSetup () < 0)
  78. {
  79. fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
  80. return 1 ;
  81. }
  82. pinMode (OUT_PIN, OUTPUT) ;
  83. pinMode (IN_PIN, INPUT) ;
  84. if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
  85. {
  86. fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
  87. return 1 ;
  88. }
  89. for (;;)
  90. {
  91. printf ("Waiting ... ") ; fflush (stdout) ;
  92. while (myCounter == globalCounter)
  93. delay (1000) ;
  94. printf (" Done. counter: %6d: %6d\n",
  95. globalCounter, myCounter - lastCounter) ;
  96. lastCounter = myCounter ;
  97. myCounter = globalCounter ;
  98. }
  99. return 0 ;
  100. }