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 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项目
第二部分:库的初始化和引脚重定义机制
库的初始化函数可以是以下三个中的一个
-
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)
// 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
#.
/led
4
实例二
最简单的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
-
Serial
port handling (both on-board and USB) -
LCD
library -
Software
PWM library -
Shift
library -
Simplified
SPI handling library -
Simplified
I2C handling library - GPIO Interrupt Handling
The PiFace board:
WiringPi fully
supports the PiFace
board too. See this
page for more details.
Other wiringPi resources:
-
Thanks to Gadgetoid there
are now wrappers for Ruby, Python and Perl and these can all befound
here. -
Thanks to Jeroen
Kransen there are wrappers for Java which can be found
here. -
Thanks to Dave Boulton for creating a TCL wrapper which can be found
here. -
Pi4J is another Java
project that uses WiringPi. It has a Github
repository here.
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.
-
See the pins
page for a description of the WiringPi pin
to GPIO connector mappings, and take note of the… -
…
special pin functions too. -
The Functions page
lists and documents what’s available, and -
There are some
examples here too. -
Download
and Install
Additional information can be found on the Raspberry Pi Wiki pages.