2017年1月19日木曜日

Raspberry Pi ( C言語でビルド & 実行 )

Raspberry Pi で Shell より Lチカを実行しましたが、exsample には C で書かれたソースもあり、また、makefile までありますので、コンパイルして実行してみます。(私はこの方が慣れている。)

まず、C は GCC 使用しますので、導入されてるかの確認をします。


pi@raspberrypi:~/wiringPi/examples $ gcc --version
gcc (Raspbian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.



バージョン 4.9.2 が入っています。
Makefile の中身を見ると、個別(ファイル毎)にコンパイル設定があるので、 blink.c のみ
コンパイルしてみます。
make の後に blink と入力すれば、blink.c のみコンパイルされ実行ファイルが出来上がります。

pi@raspberrypi:~/wiringPi/examples $ make blink
[CC] blink.c
[link]

pi@raspberrypi:~/wiringPi/examples $ ls -l
合計 180
-rw-r--r-- 1 pi pi 7651  1月 18 22:25 COPYING.LESSER
drwxr-xr-x 2 pi pi 4096  1月 18 22:25 Gertboard
-rw-r--r-- 1 pi pi 3946  1月 18 22:25 Makefile
drwxr-xr-x 2 pi pi 4096  1月 18 22:25 PiFace
drwxr-xr-x 2 pi pi 4096  1月 18 22:25 PiGlow
-rw-r--r-- 1 pi pi  353  1月 18 22:25 README.TXT
-rwxr-xr-x 1 pi pi 6600  1月 19 16:18 blink
-rw-r--r-- 1 pi pi 1459  1月 18 23:42 blink.c
-rw-r--r-- 1 pi pi 1304  1月 19 16:18 blink.o


blink (実行ファイル)と blink.o (オブジェクト)が出来上がっています。

配線はShell で Lチカ と同じです。

./blink と入力すれば LED が点滅を始めます。
ソースプログラムは以下の通りです。

/*
 * blink.c:
 * Standard "blink" program in wiringPi. Blinks an LED connected
 * to the first GPIO pin.
 *
 * Copyright (c) 2012-2013 Gordon Henderson. 
 ***********************************************************************
 * This file is part of wiringPi:
 * https://projects.drogon.net/raspberry-pi/wiringpi/
 *
 *    wiringPi is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    wiringPi is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with wiringPi.  If not, see .
 ***********************************************************************
 */

#include <stdio.h>
#include <wiringpi.h>

// LED Pin - wiringPi pin 0 is BCM_GPIO 17.

#define LED 0

int main (void)
{
  printf ("Raspberry Pi blink\n") ;

  wiringPiSetup () ;
  pinMode (LED, OUTPUT) ;

  for (;;)
  {
    digitalWrite (LED, HIGH) ; // On
    delay (500) ;  // mS
    digitalWrite (LED, LOW) ; // Off
    delay (500) ;
  }
  return 0 ;
}

wiringPiSetup() 、pinMode() 関数はそれぞれ wiringPi / wiringPi.c の中で定義され、呼び出されています。基板の種別毎に設定が行われ、pinMode ではピンの動作(入力、出力、PWM ) 等が設定されています。(詳細はチェック中......)

0 件のコメント:

コメントを投稿