개발자/Raspberry Pi

라즈베리파이 GPIO 포트를 인터럽트로 사용하기

지구빵집 2015. 1. 29. 20:07
반응형



라즈베리파이 GPIO 포트를 인터럽트로 사용하는 예제소스코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
D. Thiebaut
based on isr.c from the WiringPi library, authored by Gordon Henderson
https://github.com/WiringPi/WiringPi/blob/master/examples/isr.c
Compile as follows:
gcc -o isr4pi isr4pi.c -lwiringPi
Run as follows:
sudo ./isr4pi
*/
 
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h>
// Use GPIO Pin 17, which is Pin 0 for wiringPi library
#define BUTTON_PIN 0
 
// the event counter 
volatile int eventCounter = 0;
 
// -------------------------------------------------------------------------
// myInterrupt:  called every time an event occurs
 
void myInterrupt(void) {
   eventCounter++;
}
 
// -------------------------------------------------------------------------
// main
 
int main(void) {
  // sets up the wiringPi library
  if (wiringPiSetup () < 0) {
      fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno));
      return 1;
  }
 
  // set Pin 17/0 generate an interrupt on high-to-low transitions
  // and attach myInterrupt() to the interrupt
  if ( wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0 ) {
      fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno));
      return 1;
  }
 
  // display counter value every second.
 
  while ( 1 ) {
    printf"%d\n", eventCounter );
    eventCounter = 0;
    delay( 1000 ); // wait 1 second
  }
  return 0;
}
 
cs



반응형