RPI学习--wiringpi_API

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

Functions (API)

Some of the functions in
the WiringPi library are designed to mimic
those in the Arduino Wiring system. There are relatively easy to use and should
present no problems for anyone used to the Arduino system, or C programming
in-general.

The main difference is that unlike the Arduino system, the main loop of the
program is not provided for you – you need to write it yourself. This is often
desirable in a Linux system anyway as it can give you access to command-line
arguments and so on. See the examples page
for some simple examples and a Makefile to use.

Before using the WiringPi library, you
need to include its header file:

#include <wiringPi.h>

You may also need to add

-I/usr/local/include -L/usr/local/lib -lwiringPi

to the compile line of your program depending on the environment you are
using. The important one is -lwiringPi

Setup Functions

There are three ways to
initialise wiringPi.

  • int wiringPiSetup (void) ;

  • int wiringPiSetupGpio (void) ;

  • int wiringPiSetupSys (void) ;

One of the setup functions must be called
at the start of your program. If it returns -1 then
the initialisation of the GPIO has failed, and you should consult the
global errno to see why.

The differences between the setup functions are as follows:

  • wiringPiSetup(void) ;

This initialises the wiringPi system and assumes that the calling program is
going to be using the wiringPi pin numbering
scheme. This is a simplified numbering scheme which provides a mapping from
virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin
numbers. See the pins page for a table which maps
the wiringPi pin number to the Broadcom GPIO
pin number to the physical location on the edge connector.

This function needs to be called with root privileges.

  • wiringPiSetupGpio(void) ;

This is identical to above, however it allows the calling programs to use the
Broadcom GPIO pin numbers directly with no re-mapping.

As above, this function need to be called with root priveledges

  • wiringPiSetupSys(void)

This initialises the wiringPi system but uses the /sys/class/gpio interface
rather than accessing the hardware directly. This can be called as a non-root
user provided the GPIO pins have been exported before-hand using
the gpio program. Pin number in this mode is
the native Broadcom GPIO numbers.

Note: In this mode you can only use the pins which have been
exported via the /sys/class/gpio interface. You must export these pins before
you call your program. You can do this in a separate shell-script, or by using
the system() function from inside your program.

Also note that some functions (noted below) have no effect when using this
mode as they’re not currently possible to action unless called with root
privileges.

General wiring functions


  • void pinMode (int pin, int mode) ;

This sets the mode of a pin to
either INPUTOUTPUT,
or PWM_OUTPUT. Note that
onlywiringPi pin 1 (BCM_GPIO 18) supports PWM
output. The pin number is the number obtained from the pins table.

This function has no effect when in Sys mode.

  • void digitalWrite (int pin, int value) ;

Writes the
value HIGH or LOW (1 or 0)
to the given pin which must have been previously set as an output.

  • void digitalWriteByte (int value) ;

This writes the 8-bit byte supplied to the 8 GPIO pins. It’s the fastest way
to set all 8 bits at once to a particular value, although it still takes twi
write operations to the GPIO hardware.

  • void pwmWrite (int pin, int value) ;

Writes the value to the PWM register for the given pin. The value must be
between 0 and 1024. (Again, note that only pin 1 (BCM_GPIO 18) supports PWM)

This function has no effect when in Sys mode (see above)

  • int digitalRead (int pin) ;

This function returns the value read at the given pin. It will
be HIGH or LOW (1 or 0)
depending on the logic level at the pin.

  • void pullUpDnControl (int pin, int pud) ;

This sets the pull-up or pull-down resistor mode on the given pin, which
should be set as an input. Unlike the Arduino, the BCM2835 has both pull-up an
down internal resistors. The parameter pud should
be; PUD_OFF, (no pull
up/down), PUD_DOWN (pull to ground)
orPUD_UP (pull to 3.3v)

This function has no effect when in Sys mode. If you need to activate a
pull-up/pull-down, then you can do it with
the gpio program in a script before you
start your program.

PWM Control

PWM can not be controlled when running in Sys mode.

  • pwmSetMode (int mode) ;

The PWM generator can run in 2 modes – “balanced” and “mark:space”. The
mark:space mode is traditional, however the default mode in the Pi is
“balanced”. You can switch modes by supplying the
parameter: PWM_MODE_BAL or PWM_MODE_MS.

  • pwmSetRange (unsigned int range) ;

This sets the range register in the PWM generator. The default is 1024.

  • pwmSetClock (int divisor) ;

This sets the divisor for the PWM clock.

To understand more about the PWM system, you’ll need to read the Broadcom ARM
peripherals manual.

Timing functions


  • unsigned int millis (void)

This returns a number representing the number if milliseconds since your
program called one of
the wiringPiSetup functions. It returns an
unsigned 32-bit number which wraps after 49 days.

  • void delay (unsigned int howLong)

This causes program execution to pause for at
least howLong milliseconds. Due
to the multi-tasking nature of Linux it could be longer. Note that the maximum
delay is an unsigned 32-bit integer or approximately 49 days.

  • void delayMicroseconds (unsigned int howLong)

This causes program execution to pause for at
least howLong microseconds. Due
to the multi-tasking nature of Linux it could be longer. Note that the maximum
delay is an unsigned 32-bit integer microseconds or approximately 71
minutes.

Program/Thread Priority


  • int piHiPri (int priority) ;

This attempts to shift your program (or thread in a multi-threaded program)
to a higher priority and enables a real-time scheduling.
The priority parameter should be from 0 (the default)
to 99 (the maximum). This won’t make your program go any faster, but it will
give it a bigger slice of time when other programs are running. The priority
parameter works relative to others – so you can make one program priority 1 and
another priority 2 and it will have the same effect as setting one to 10 and the
other to 90 (as long as no other programs are running with elevated
priorities)

The return value is 0 for success and -1 for error. If an error is returned,
the program should then consult the errno global variable, as
per the usual conventions.

Note: Only programs running as root can change their
priority. If called from a non-root program then nothing happens.

Interrupts

With a newer kernel patched with the GPIO interrupt handling code, (ie. any
kernel after about June 2012), you can now wait for an interrupt in your
program. This frees up the processor to do other tasks while you’re waiting for
that interrupt. The GPIO can be set to interrupt on a rising, falling or both
edges of the incoming signal.

Note: Jan 2013: The waitForInterrupt() function is
deprecated – you should use the newer and easier to use wiringPiISR() function
below.

  • int waitForInterrupt (int pin, int timeOut) ;

When called, it will wait for an interrupt event to happen on that pin and
your program will be stalled. The timeOut parameter
is given in milliseconds, or can be -1 which means to wait forever.

The return value is -1 if an error occurred
(and errno will be set appropriately), 0 if it timed out, or
1 on a successful interrupt event.

Before you call waitForInterrupt, you must first initialise the GPIO pin and
at present the only way to do this is to use the gpio program, either in a
script, or using the system() call from inside your program.

e.g. We want to wait for a falling-edge interrupt on GPIO pin 0, so to setup
the hardware, we need to run:

gpio edge 0 falling

before running the program.

  • int wiringPiISR (int pin, int edgeType,  void
    (*function)(void)) ;

This function registers a function to received interrupts on the specified
pin. The edgeType parameter is
either INT_EDGE_FALLINGINT_EDGE_RISINGINT_EDGE_BOTH orINT_EDGE_SETUP.
If it is INT_EDGE_SETUP then no initialisation of the
pin will happen – it’s assumed that you have already setup the pin elsewhere
(e.g. with the gpio program), but if you
specify one of the other types, then the pin will be exported and initialised as
specified. This is accomplished via a suitable call to
the gpio utility program, so it need to be
available.

The pin number is supplied in the current mode – native wiringPi, BCM_GPIO or
Sys modes.

This function will work in any mode, and does not need root privileges to
work.

The function will be called when the interrupt triggers. When it is
triggered, it’s cleared in the dispatcher before calling your function, so if a
subsequent interrupt fires before you finish your handler, then it won’t be
missed. (However it can only track one more interrupt, if more than one
interrupt fires while one is being handled then they will be ignored)

This function is run at a high priority (if the program is run using sudo, or
as root) and executes concurrently with the main program. It has full access to
all the global variables, open file handles and so on.

See the isr.c example program for more details on how to
use this feature.

Concurrent Processing (multi-threading)

wiringPi has a simplified interface to the
Linux implementation of Posix threads, as well as a (simplified) mechanisms to
access mutex’s (Mutual exclusions)

Using these functions you can create a new process (a function inside your
main program) which runs concurrently with your main program and using the mutex
mechanisms, safely pass variables between them.

  • int piThreadCreate (name) ;

This function creates a thread which is another function in your program
previously declared using the PI_THREAD declaration.
This function is then run concurrently with your main program. An example may be
to have this function wait for an interrupt while your program carries on doing
other tasks. The thread can indicate an event, or action by using global
variables to communicate back to the main program, or other threads.

Thread functions are declared as follows:

PI_THREAD (myThread)
{
.. code here to run concurrently with
the main program, probably in an
infinite loop
}

and would be started in the main program with:

x = piThreadCreate (myThread) ;
if (x != 0)
printf ("it didn‘t start\n")

This is really nothing more than a simplified interface to the Posix threads
mechanism that Linux supports. See the manual pages on Posix threads (man
pthread) if you need more control over them.

  • piLock (int keyNum) ;

  • piUnlock (int keyNum) ;

These allow you to synchronise variable updates from your main program to any
threads running in your program. keyNum is a number from 0 to 3 and represents a
“key”. When another process tries to lock the same key, it will be stalled until
the first process has unlocked the same key.

You may need to use these functions to ensure that you get valid data when
exchanging data between your main program and a thread – otherwise it’s possible
that the thread could wake-up halfway during your data copy and change the data
– so the data you end up copying is incomplete, or invalid. See the wfi.c
program in the examples directory for an example.

Misc. Functions


  • piBoardRev (void) ;

This returns the board revision of the Raspberry Pi. It will be either 1 or
2. Some of the BCM_GPIO pins changed number and function when moving from board
revision 1 to 2, so if you are using BCM_GPIO pin numbers, then you need to be
aware of the differences.

  • wpiPinToGpio (int wPiPin) ;

This returns the BCM_GPIO pin number of the supplied wiringPi pin. It takes
the board revision into account.

  • setPadDrive (int group, int value) ;

This sets the “strength” of the pad drivers for a particular group of pins.
There are 3 groups of pins and the drive strength is from 0 to 7. Do not use
this unless you know what you are doing.

RPI学习--wiringpi_API,布布扣,bubuko.com

时间: 2024-09-30 15:40:08

RPI学习--wiringpi_API的相关文章

树莓派学习笔记(1):入手树莓派

转载请注明:@小五义http://www.cnblogs.com/xiaowuyi      玩了一段时间的arduino后,想再学习一下树莓派,看到网上每天都有那么多人在讨论,自己出有些手痒了.于是,从朋友那里借来一套树莓派,准备先学习一段时间后,再自己入手买一套.一.认识树莓派      Raspberry Pi(中文名为“树莓派”,简写为RPi,或者RasPi/RPi),由注册于英国的慈善组织“Raspberry Pi 基金会”开发,Eben·Upton/埃·厄普顿为项目带头人.2012年

树莓派学习笔记——Model B Model B+ Compute Module Dev Kit的区别和联系

0 前言 最近浏览器树莓派官方发现树莓派推出了两款新Model--一款名为树莓派 model B+,一款名为树莓派 Compute Module Dev Kit.带着欣喜和恐惧查阅了相关资料,并通过淘宝和RS中国了解开发板价格.欣喜的感觉来自于树莓派的功能得到了增强,恐惧来自于树莓派的改变带来新的学习成本.经过几天的资料收集,所以整理成博文和大家分享. 1 横向比较 [共性比较] 表1 三款树莓派横向比较 区别 Model B Model B+ Compute Module Dev Kit 芯片

RPi 2B apache2 mysql php5 and vsftp

/************************************************************************* * RPi 2B apache2 mysql php5 and vsftp * 声明: * 本文主要记录RPi 2B如何安装Apache2.mysql.php5.vsftp服务器,并对其进行 * 测试,为后续工作做准备,其中遇到SD卡空间不足的问题. * * 2016-2-19 深圳 南山平山村 曾剑锋 **********************

树莓派学习笔记(5):成功实现NAS家庭服务器(流媒体播放、文件共享及下载机)

转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 一.家庭服务器实现的主要功能 1.流媒体播放服务:利用DLNA实现电视.手机.电脑播放其上面的媒体文件. 2.文件共享:利用samba实现手机.电脑等终端与服务器的文件共享. 3.自动下载:利用aria2c实现自动下载. 先上几张效果图: 用orico的包装盒做了个机箱. 内部效果,线还是有些凌乱 放在桌上,感觉还不错,呵呵 二.准备工作 1.树莓派B+ 2.安装raspbian系统,具体

MQTT学习笔记——树莓派MQTT客户端 使用Mosquitto和paho-python

0 前言 本文说明如何在树莓派上安装Mosquitto.本文通过两个简单的例子说明树莓派中如何使用MQTT协议实现消息订阅,这些例子包括Mosquitto_sub指令实现消息订阅和paho-python扩展库实现GPIO端口的远程控制.本文中使用了两个工具--Mosquitto paho-python,其中Mosquitto是一款实现了 MQTT v3.1 协议的开源消息代理软件,提供轻量级的,支持发布/订阅的的消息推送模式,使设备对设备之间的消息通信简单易用:另外,paho-python是一个

MQTT学习笔记——Yeelink MQTT服务 使用mqtt.js和paho-mqtt

0 前言 2014年8月yeelink推出基于MQTT协议的开关类型设备控制API,相比于基于HTTP RESTful的轮训方式,通过订阅相关主题消息,可以远程控制类应用实时性更好.本文使用两种方式实现开关类型设备的远程控制,一种是基于nodeJS的MQTT.js扩展库,另一种是基于python的paho-mqtt扩展库. [相关博文--MQTT] [MQTT学习笔记--MQTT协议体验 Mosquitto安装和使用] [MQTT学习笔记--树莓派MQTT客户端 使用Mosquitto和paho

树莓派学习笔记——SQLite操作简述

0 前言 本文介绍如何在树莓派中利用SQLite数据库保存CPU温度数据.SQLite是一款轻量级零配置数据库,非常适合在树莓派和其他嵌入式系统中使用.SQLite文档详细资料丰富,本文不会详细解释SQLite数据库操作的方方面面,只能结合具体场景按需说明.本文介绍的SQLite技巧也可以在其他平台使用,并不局限于树莓派. 本文继续折腾树莓派温度,需要从中可以玩出新花样. [相关博文] [树莓派学习笔记--索引博文]--更多博文请关注. [树莓派学习笔记--获取树莓派CPU温度] [树莓派学习笔

PHP再学习5——RESTFul框架 远程控制LED

0.前言 去年(2013年)2月第一次接触yeelink平台,当时该平台已经运行了一些时间也吸引了不少极客.试想自己也将投身IoT(物联网)行业,就花了些时间研究了它.陆陆续续使用和研究了一年,大致围绕两个问题展开——1.yeelink平台如何使用,2.如何构造一个功能简单些的yeelink平台.    [PHP学习笔记——索引博文] 本文将讨论如何构造一个简单restful架构平台(该平台有点像yeelink,不过功能比yeelink少的多),并结合树莓派实现LED的远程控制(网络控制).构建

物联网学习笔记——构建RESTFul平台1

0.前言 前些时间顺着Yeelink学习了RESTFUL,使用PHP和Slim框架尝试实现简单的REST API,树莓派可通过GET方法获得JSON数据包,通过这种方式实现了树莓派和服务器(我的PC)的互动.但是由于没有WEB前端,所以只能使用cURL工具或直接修改数据库的方式改变LED状态,体验非常差. REST API入门体验可参考 [PHP再学习4—— slim框架学习和使用] [PHP再学习5——RESTFul框架 远程控制LED] 受到[开源IOT——一个最小的物联网系统设计方案及源码