Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 

157 строки
4.3 KiB

  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 set up
  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.
  27. ***********************************************************************
  28. * This file is part of wiringPi:
  29. * https://github.com/WiringPi/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. #include <signal.h>
  51. // What GPIO input are we using?
  52. // This is a wiringPi pin number
  53. #define OUT_PIN 2
  54. #define IN_PIN 3
  55. // globalCounter:
  56. // Global variable to count interrupts
  57. // Should be declared volatile to make sure the compiler doesn't cache it.
  58. static volatile int globalCounter = 0;
  59. static int terminate_process = 0;
  60. //**********************************************************************************************************************
  61. static void Signal_handler(int sig);
  62. /*
  63. * myInterrupt:
  64. *********************************************************************************
  65. */
  66. void myInterrupt (int pin)
  67. {
  68. digitalWrite (OUT_PIN, 1) ;
  69. ++globalCounter;
  70. printf (" %d\n", globalCounter);
  71. digitalWrite (OUT_PIN, 0) ;
  72. }
  73. /*
  74. *********************************************************************************
  75. * main
  76. *********************************************************************************
  77. */
  78. int main (void)
  79. {
  80. int myCounter = 0;
  81. if (wiringPiSetup () < 0)
  82. {
  83. fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
  84. return 1;
  85. }
  86. pinMode (OUT_PIN, OUTPUT);
  87. pinMode (IN_PIN, INPUT);
  88. if (wiringPiISR (IN_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
  89. {
  90. fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
  91. return 1;
  92. }
  93. // Set the handler for SIGTERM (15)
  94. signal(SIGTERM, Signal_handler);
  95. signal(SIGHUP, Signal_handler);
  96. signal(SIGINT, Signal_handler);
  97. signal(SIGQUIT, Signal_handler);
  98. signal(SIGTRAP, Signal_handler);
  99. signal(SIGABRT, Signal_handler);
  100. signal(SIGALRM, Signal_handler);
  101. signal(SIGUSR1, Signal_handler);
  102. signal(SIGUSR2, Signal_handler);
  103. while (!terminate_process)
  104. {
  105. printf ("Waiting ...\n");
  106. fflush (stdout);
  107. while (!terminate_process && (myCounter == globalCounter))
  108. {
  109. delayMs(250);
  110. }
  111. printf ("Done. counter: %2d: %2d\n", globalCounter, globalCounter - myCounter);
  112. digitalWrite (OUT_PIN, 1);
  113. delayMs(500);
  114. digitalWrite (OUT_PIN, 0);
  115. myCounter = globalCounter;
  116. }
  117. return 0;
  118. }
  119. //**********************************************************************************************************************
  120. /**
  121. * Intercepts and handles signals
  122. * This function is called when the SIGTERM signal is raised
  123. */
  124. void Signal_handler(int sig) {
  125. printf("Received signal %d\n", sig);
  126. // Signal process to exit.
  127. terminate_process = 1;
  128. }
  129. //**********************************************************************************************************************