Notice
Recent Posts
Recent Comments
Today
Total
05-19 00:04
Archives
관리 메뉴

Jeongchul Kim

라즈베리 파이2 GPIO LED 본문

라즈베리파이

라즈베리 파이2 GPIO LED

김 정출 2016. 3. 9. 15:33


라즈베리 파이2 GPIO LED


라즈베리 파이2 GPIO PIN MAP


라즈베리 GPIO PIN MAP이다

BCM은 Broad CoM , 라즈베리파이2는 CPU를 BCM2836 사용


Raspberry PI2 GPIO LED





pi@raspberrypi:~/c_project $ ls /sys/class/gpio/

export  gpiochip0  unexport


pi@raspberrypi:~/c_project $ echo 18 > /sys/class/gpio/export

pi@raspberrypi:~/c_project $ ls /sys/class/gpio/

export  gpio18  gpiochip0  unexport


pi@raspberrypi:~/c_project $ echo out > /sys/class/gpio/gpio18/direction

pi@raspberrypi:~/c_project $ echo 1 > /sys/class/gpio/gpio18/value


불이 켜진다.



pi@raspberrypi:~/c_project $ echo 0  > /sys/class/gpio/gpio18/value

불이 꺼진다.


끝나고 났따면

echo 18 > /sys/class/gpio/unexport


c언어 코딩을 이용한 led 제어

gpio_led 프로젝트를 생성한다.


gpio_led.c

소스파일을 입력한다.

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

#include <string.h>

int main( int argc, char **argv) {

      int gpio_pin = 18;

      int fd;

      char buff[BUFSIZ];

      fd = open("/sys/class/gpio/export", O_WRONLY );

      if (fd == -1) {

             perror("fail to open export." );

             return 1;

      }

      sprintf(buff, "%d" , gpio_pin);

      if (write (fd, buff, sizeof(buff)) == -1) {

             perror("fail to export GPIO" );

             return 1;

      }

      close(fd);

      sprintf(buff, "/sys/class/gpio/gpio%d/direction" , gpio_pin);

      fd = open(buff, O_WRONLY);

      if (fd == -1) {

             perror("fail to open GPIO Direction File" );

             return 1;

      }

      if (write (fd, "out" , 4) == -1) {

             perror("fail to set direction\n" );

             return 1;

      }

      close(fd);

      sprintf(buff, "/sys/class/gpio/gpio%d/value" , gpio_pin);

      fd = open(buff, O_WRONLY);

      int i=0;

      for(i=0; i<10; i++) { // 1oop 열 번 돌면서 깜빡깜빡 거림.

             if (write (fd, "1" , sizeof("1")) == -1) {

                    perror("fail to set value:1" );

                    return 1;

            }

             printf("set gpio %d value as HIGH\n", gpio_pin);

             sleep(1);

             if (write (fd, "0" , sizeof("0")) == -1) {

                    perror("fail to set value:0" );

                    return 1;

            }

             printf("set gpio %d value as LOW\n", gpio_pin);

             sleep(1);

       }

      close(fd);

      fd = open("/sys/class/gpio/unexport" , O_WRONLY );

      sprintf(buff, "%d" , gpio_pin);

      write(fd, buff, strlen(buff));

      close(fd);

      return 0;

}




컴파일 한다.


Run As Configurations에 설정을 들어간다.


Remote Absoulute File Path 시에 디렉터리를 설정하고 C파일 이름 gpio_led를 맞춰야 한다.

실행파일은 c언어 파일 이름이므로 붙여야 한다.


Run을 실행하면 오류가 뜬다. Permission Deny


putty로 돌아와 unexport를 하고, sudo ./gpio_led를 실행한다.

pi@raspberrypi:~/c_project $ echo 18 > /sys/class/gpio/unexport

pi@raspberrypi:~/c_project $ sudo ./gpio_led








Comments