[树莓派]控制发光二极管

RaspberryPi[树莓派]控制发光二极管

作者:LinuxHub   发布:2013-04-20 04:26   分类:RaspberryPi    

RaspberryPi[树莓派]控制LED灯
通过Raspberry Pi(树莓派)的GPIO接口控制发光二极管

视频: http://v.youku.com/v_show/id_XNTQ1MzgwMDY0.html

1.安装些必要的库文件

1

2

3

pi@raspberrypi /home/apinglai $ sudo apt-get install libi2c-dev

pi@raspberrypi /home/apinglai $ sudo apt-get install i2c-tools

pi@raspberrypi /home/apinglai $ sudo apt-get install python-smbus

2.下载安装wiringPi

1

2

3

4

5

6

pi@raspberrypi /home/apinglai/down $ wget http://project-downloads.drogon.net/files/wiringPi.tgz

pi@raspberrypi /home/apinglai/down $ tar xf wiringPi.tgz

pi@raspberrypi /home/apinglai/down $ cd wiringPi/wiringPi/

pi@raspberrypi /home/apinglai/down/wiringPi/wiringPi $ sudo make uninstall

pi@raspberrypi /home/apinglai/down/wiringPi/wiringPi $ sudo make

pi@raspberrypi /home/apinglai/down/wiringPi/wiringPi $ sudo make install

3.编写C程序

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

pi@raspberrypi /home/apinglai $ sudo vim led.c

 

#include

#include

#include

 

int main (int argc,char* argv[])

{

  if (argc < 2) {

    printf("Usage example: ./led 4 \n");

    return 1;

  }

  int pinNumber = atoi(argv[1]);

 

  if (-1 == wiringPiSetup()) {

    printf("Setup wiringPi failed!");

    return 1;

  }

 

  pinMode(pinNumber, OUTPUT); // set mode to output

  while(1) {

    digitalWrite(pinNumber, 1); // output a high level

    delay(800);

    digitalWrite(pinNumber, 0); // output a low level

    delay(800);

  }

 

  return 0;

}

4.编译

1

2

3

pi@raspberrypi /home/apinglai $ gcc led.c -o led -lwiringPi

pi@raspberrypi /home/apinglai $ ls

led  led.c

5.运行程序

1

pi@raspberrypi /home/apinglai $ sudo ./led 4

 

转载于:https://my.oschina.net/u/4047409/blog/3051597

你可能感兴趣的:(开发工具,raspberry,pi)