探索Wiring Pi

wIring pi ,专门为树莓派打造的类似于arduino下的wiring驱动库

安装好这个库后可以直接调用函数配置和控制GPIO功能

(无论是用c shell  python都能控制GPIO了)


树莓派有个26pin的扩展端子(树莓派V2旁边还有个8pin的没焊接的端口也可自己焊接使用)

其中电源包含3.3v  5v  Gnd

可以控制的资源有

SPI,IIC,UART,通用GPIO.(其中一个可做PWM)



第一部分,库的情况和获取

快速安装(c开发库)

mkdir temp
cd temp
wget http://project-downloads.drogon.net/files/wiringPi.tgz
tar xf wiringPi.tgz
cd wiringPi/wiringPi/
make
make install
官方给的安装方式: https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/


wiringpi

 

1.1.0

WiringPi library wrapper for the Raspberry Pi only. Wraps up the Arduino wiring-like WiringPi library into a convinient Ruby gem. Currently includes GPIO functionality, serial and shiftOut/shiftIn support. Credit to Gordon for the WiringPi library, which can be found here: http://projects.drogon.net/raspberry-pi/wiringpi/


        wiring pi项目网站https://projects.drogon.net/raspberry-pi/wiringpi/


          树莓派官方:http://www.raspberrypi.org/


在Github项目

https://github.com/WiringPi



第二部分:库的初始化和引脚重定义机制

库的初始化函数可以是以下三个中的一个

  • int wiringPiSetup (void) ;                //使用IO映射,可以更方便的管理IO   需要root权限
  • int wiringPiSetupGpio (void) ;      //不用映射直接用物理IO编号                需要root权限
  • int wiringPiSetupSys (void) ;         //不需要root权限
  • 另外库还支持命令行模式直接用shell脚本就可以控制GPIO

如果你用

  • wiringPiSetup (void) 初始化

  • 则引脚被重新定义/映射(DNC的意思是不要连接)

具体可参考https://projects.drogon.net/raspberry-pi/wiringpi/pins/


  • P5: The auxilliary GPIO connector present on Rev. 2 boards only:
    Board Revisions
    : Please note the differences between board revisions 1 and 2 (R1 and R2 above)



左边是wiring库封装后对应的引脚    右边是树莓派的物理真实引脚 



所以比如下面的例子,你用wiring库中的函数操作gpio4 其实是操作物理端口的GPIO23


第三部分:一个简单的实例

.c引入头文件

初始化库

控制IO口


另外:编译时加上库

(用wiring库操作LED的实例http://www.codelast.com/?p=5155


实例一 
用C语言控制

// 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


#./led4




实例二

最简单的python控制

$ sudo python
>>> import RPi.GPIO as GPIO
>>> GPIO.setup(18, GPIO.OUT)
>>> GPIO.output(18, False)



实例三

用python控制

import RPi.GPIO as GPIO
import time

# Use physical pin numbers
GPIO.setmode(GPIO.BOARD)
# Set up header pin 11 (GPIO17) as an input
print "Setup Pin 11"
GPIO.setup(11, GPIO.OUT)

var=1
print "Start loop"
while var==1 :
  print "Set Output False"
  GPIO.output(11, False)
  time.sleep(1)
  print "Set Output True"
  GPIO.output(11, True)
  time.sleep(1)


实例4 PWM输出

/*
 * test2.c:
 *	Simple test program to test the wiringPi functions
 *	PWM test
 */

#include <wiringPi.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main (void)
{
  int bright ;

  printf ("Raspberry Pi wiringPi PWM test program\n") ;

  if (wiringPiSetup () == -1)
    exit (1) ;

  pinMode (1, PWM_OUTPUT) ;

  for (;;)
  {
    for (bright = 0 ; bright < 1024 ; ++bright)
    {
      pwmWrite (1, bright) ;
      delay (1) ;
    }

    for (bright = 1023 ; bright >= 0 ; --bright)
    {
      pwmWrite (1, bright) ;
      delay (1) ;
    }
  }

  return 0 ;
}



第四部分:文件夹内容以及api函数

text

git:git版本管理

examples:实例包括延时测试。PWM,(test2)GPIO(test1控制测试所有IO),LCD,串口,中断等,

gpio:UTILITY模式下控制GPIO test.sh

wiringpi:库函数实现




https://projects.drogon.net/raspberry-pi/wiringpi/

WiringPi Resources

Additional Libraries

The PiFace board:

WiringPi fully supports the PiFace board too. See this page for more details.

Other wiringPi resources:

Pin numbering

WiringPi supports both an Arduino style pin numbering scheme which numbers the pins sequentially from 0 upwards, as well as the Raspberry Pi’s native BCM_GPIO pin numbering scheme.

  • Note that when using the BCM_GPIO numbering scheme, you must take into account the board revision! Some pins changed their meaning and numbers from revision 1 to revision 2. Using the wiringPi pin numbering scheme caters for these changes and progams will run un-changed on their board revision.

WiringPi normally uses a very low-level mechanism to access the underlying hardware – this results in very fast access, however the down-side is that your programs need to be run as root, so in addition to this, wiringPi has the ability to use the traditional /sys/class/gpio/ style interface, and if the GPIO pins have been exported and had their ownership changed appropriately, then applications using the wiringPi library, can be run without root privileges.

A supporting program, gpio, allows you to export and unexport the devices through the/sys/class/gpio/ interface. This program is a set-uid program and can be run as a normal user. The gpio program can be used to control the GPIO pins in its own right, allowing easy testing of the GPIO interface from the command-line or simple shell scripts.

Additional information can be found on the Raspberry Pi Wiki pages.



  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WiringPi是一款专门为树莓派设计的C库,提供了方便的GPIO控制和PWM控制功能。在使用树莓派控制电机、灯光等外设时,PWM控制是非常重要的,可以通过调整PWM信号的占空比,实现对电机、灯光等外设的精确控制。 我个人使用WiringPi PWM的历程如下: 1.安装WiringPi库 首先需要在树莓派上安装WiringPi库,可以通过以下命令进行安装: ``` sudo apt-get update sudo apt-get install wiringpi ``` 2.导入WiringPi库 在C/C++程序中使用WiringPi库,需要在程序中导入该库,可以使用以下指令: ``` #include <wiringPi.h> ``` 3.初始化PWM控制 在程序中需要对PWM控制进行初始化,可以使用以下函数: ``` int pwmSetMode (int mode); int pwmSetRange (unsigned int range); int pwmSetClock (int divisor); ``` 其中,mode参数指定PWM波形的模式,range参数指定PWM波形的周期,divisor参数指定PWM波形的时钟频率。例如,设置PWM波形的模式为Mark Space模式,周期为2000,时钟频率为192,可以使用以下代码进行初始化: ``` pwmSetMode (PWM_MODE_MS); pwmSetRange (2000); pwmSetClock (192); ``` 4.设置PWM输出引脚 在程序中需要指定PWM输出的引脚,可以使用以下函数: ``` void pinMode (int pin, int mode); ``` 其中,pin参数指定GPIO引脚的编号,mode参数指定GPIO引脚的模式,可以是输入或输出。需要注意的是,只有支持PWM输出的GPIO引脚才能进行PWM控制。 例如,设置GPIO18引脚为PWM输出模式,可以使用以下代码: ``` pinMode (18, PWM_OUTPUT); ``` 5.控制PWM输出 在程序中需要控制PWM输出的占空比,可以使用以下函数: ``` void pwmWrite (int pin, int value); ``` 其中,pin参数指定PWM输出的GPIO引脚编号,value参数指定PWM输出的占空比,取值范围为0~range,其中range为初始化时设置的PWM波形周期值。 例如,设置GPIO18引脚的PWM输出占空比为50%,可以使用以下代码: ``` pwmWrite (18, 1000); ``` 以上就是我使用WiringPi PWM的历程,希望可以对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值