• 通过Raspberry Pi(树莓派)的GPIO接口控制发光二极管


    原文:http://www.codelast.com/?p=5155

    参考:http://blog.csdn.net/liang890319/article/details/8643069

    下载安装包,解压,编译,安装:

    mkdir temp
    cd temp
    wget http://project-downloads.drogon.net/files/wiringPi.tgz
    tar xf wiringPi.tgz
    cd wiringPi/wiringPi/
    make
    make install

    下面写程序测试:

    // led.c
     
    #include <wiringPi.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    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;
    }

    编译程序:

    gcc led.c -o led -lwiringPi

    运行程序:
    Run the program:

    ./led 4

    可以看到LED开始闪烁了

    可能遇见的问题:

    GPIO测试编译时出现了这个问题

    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_write_byte'
    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_read_byte'
    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_write_byte_data'
    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_write_word_data'
    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_read_word_data'
    /usr/lib/gcc/arm-linux-gnueabi/4.6/http://www.cnblogs.com/../libwiringPi.so:undefined reference to `i2c_smbus_read_byte_data'
    collect2: ld returned 1 exit status

     

    官方论坛上说是少装了库的原因

    安装这几个库: i2c-tools,libi2c-dev ,python-smbus

    安装参考 sudo apt-get install libi2c-dev

    然后重新

    make clean

    sudo make uninstall

     

    make

    sudo make install

    重新编译下你的.c文件

    gcc led.c -o led -lwiringPi

    sudo ./led 4

    虽然输入的是4

    实际上是GPIO23不停输出高低电平

  • 相关阅读:
    跨浏览器的事件处理程序(javascript高级程序设计第二版第十二章)
    json
    html5 本地存储Web Storage
    sicily 6497. 字符统计
    sicily 6415. linear correlation
    sicily 1154. Easy sort
    sicily 6496. 二维数组
    sicily 6423. 反向输出数字
    sicily 1636. show me the money
    sicily 1324. Score
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2964087.html
Copyright © 2020-2023  润新知