라즈베리파이/GPIO

C언어를 이용한 GPIO 출력 (wiringPi )

youjin.A 2016. 2. 8. 04:52

Gordons Projects”라는 블로그에서 C언어를 지원하는  “wiringPi” 라이브라리를 사용할 것이다. 

내가 이것을 배운 출처는 ​http://www.rasplay.org/?p=3241

 

 

 

1. wiringPi 라이브러리 설치하기

소스관리툴 git 다운로드

sudo apt-get install git-core

wiringPi 프로젝트를 통째로 받아온다.

git clone git://git.drogon.net/wiringPi

빌드 및 설치 진행

cd wiringPi

./build 

설치가 잘 되었는지 확인

gpio -v gpio readall

 

2. 작업 폴더 및 .c파일 생성

File manager에서 /home/pi에 c_lang이라는 디렉터리를 만들었다.

c_lang 디렉터리 안에 c_lang_GPIO_OUTPUT.c라는 파일을 만들었다.

c파일 안에 다음 코드를 넣는데, 이 코드는 4번(#23) 5번(#24) 출력의 LED를 켜도록 한다.


Colored By Color Scripter

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
#include <stdio.h>
#include <wiringPi.h>
 
#define LED1 4 // BCM_GPIO 23
#define LED2 5 // BCM_GPIO 24
 
int main (void)
{
  if (wiringPiSetup () == -1)
  return 1 ;
 
  pinMode (LED1, OUTPUT) ;
  pinMode (LED2, OUTPUT) ;
 
  for (;;)
  {
    digitalWrite (LED1, 1) ; // On
    digitalWrite (LED2, 1) ; // On
 
    delay (1000) ; // ms
 
    digitalWrite (LED1, 0) ; // Off
    digitalWrite (LED2, 0) ; // Off
 
    delay (1000) ;
  }
  return 0 ;
}

 

 

 

3. 프로그램 컴파일

파일을 저장한 디렉터리로 들어가서

cd /home/pi/c_lang

다음 컴파일 명령을 실행해 주면 c_lang_GPIO_OUTPUT 이라는 실행파일이 생성된다. 

gcc -o c_lang_GPIO_OUTPUT c_lang_GPIO_OUTPUT​.c -lwiringPi 

 

 

4. 프로그램 실행

sudo ./c_lang_GPIO_OUTPUT

프로그램 종료는 Ctrl + C