Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

100 rader
2.6 KiB

  1. /*
  2. * isr.c:
  3. * Wait for Interrupt test program - ISR method
  4. *
  5. * How to test:
  6. * Use the SoC's pull-up and pull down resistors that are avalable
  7. * on input pins. So compile & run this program (via sudo), then
  8. * in another terminal:
  9. * gpio mode 0 up
  10. * gpio mode 0 down
  11. * at which point it should trigger an interrupt. Toggle the pin
  12. * up/down to generate more interrupts to test.
  13. *
  14. * Copyright (c) 2013 Gordon Henderson.
  15. ***********************************************************************
  16. * This file is part of wiringPi:
  17. * https://projects.drogon.net/raspberry-pi/wiringpi/
  18. *
  19. * wiringPi is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Lesser General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * wiringPi is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public License
  30. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  31. ***********************************************************************
  32. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <stdlib.h>
  37. #include <wiringPi.h>
  38. // What GPIO input are we using?
  39. // This is a wiringPi pin number
  40. #define BUTTON_PIN 0
  41. // globalCounter:
  42. // Global variable to count interrupts
  43. // Should be declared volatile to make sure the compiler doesn't cache it.
  44. static volatile int globalCounter = 0 ;
  45. /*
  46. * myInterrupt:
  47. *********************************************************************************
  48. */
  49. void myInterrupt (void)
  50. {
  51. ++globalCounter ;
  52. }
  53. /*
  54. *********************************************************************************
  55. * main
  56. *********************************************************************************
  57. */
  58. int main (void)
  59. {
  60. int myCounter = 0 ;
  61. if (wiringPiSetup () < 0)
  62. {
  63. fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
  64. return 1 ;
  65. }
  66. if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0)
  67. {
  68. fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
  69. return 1 ;
  70. }
  71. for (;;)
  72. {
  73. printf ("Waiting ... ") ; fflush (stdout) ;
  74. while (myCounter == globalCounter)
  75. delay (100) ;
  76. printf (" Done. counter: %5d\n", globalCounter) ;
  77. myCounter = globalCounter ;
  78. }
  79. return 0 ;
  80. }