wiringPI库

Source of reprint: http://www.cnblogs.com/lulipro/

When using the wiringPi library, you need to include the header file #include<wiringPi.h>. All programs that write wiringPi include this header file.

Hardware initialization function

When using wiringPi, you must initialize the Raspberry Pi before performing any operations, otherwise the program will not work properly.

You can call one of the following functions to initialize, and they will all return an int, and return -1 to indicate that the initialization failed.

 

int wiringPiSetup (void) Return: execution status, -1 means failure

When using this function to initialize the Raspberry Pi pins, the program uses the wiringPi pin number table. The number of pins is 0~16

Need root privileges

int wiringPiSetupGpio (void) Returns the execution status, -1 means failure

When using this function to initialize the Raspberry Pi pins, the program uses the BCM GPIO pin number table.

Need root privileges

wiringPiSetupPhys(void)  Not commonly used, no introduction  /
wiringPiSetupSys (void) ;  Not commonly used, no introduction  /

General GPIO control function

void pinMode (int pin, int mode)

pin: configured pin

mode: the IO mode of the specified pin

Possible values: INPUT, OUTPUT, PWM_OUTPUT, GPIO_CLOCK


Function: Configure the IO mode of the pin.
Note: Only pin 1 under the wiringPi pin number (pin 18 under BCM) supports PWM output

Only 7 under the wiringPi number (No. 4 under BCM) supports GPIO_CLOCK output

void digitalWrite (int pin, int value)

pin: controlled pin

value: The level value output by the pin.

 Possible values: HIGH, LOW represent high and low levels respectively

Let a pin that has been configured as an output mode output a specified level signal
int digitalRead (int pin)

pin: read pin

Return: The level on the pin, which can be one of LOW HIGH

Read the level value of a pin LOW HIGH, return
void analogWrite(int pin, int value)

pin: pin

value: analog output

Analog output

The pins of the Raspberry Pi do not support AD conversion, that is, analog APIs cannot be used.

Need to add another module

int analogRead (int pin)

pin: pin

Return: the analog value read on the pin

Analog input

The pins of the Raspberry Pi do not support AD conversion, that is, analog APIs cannot be used.

Need to add another module

void pwmWrite (int pin, int value)

pin: pin

value: The value written to the PWM register, ranging from 0 to 1024.

Output a value to the PWM register to control the PWM output.
The pin can only be pin 1 under the wiringPi pin number (pin 18 under BCM)

void pullUpDnControl (int pin, int pud)

 pin: pin

pud: pull-resistance mode

Possible values: PUD_OFF does not enable any pull-up resistors. Turn off the pull-up resistor.
             PUD_DOWN enable pull-down resistor, the pin level is pulled to GND
             PUD_UP enable pull-up resistor, the pin level is pulled to 3.3v

Set the pull-up resistance mode for an input pin that sets the IO mode to INPUT.

Unlike Arduino, Raspberry Pi supports more pull-resistance modes.

The pull-up resistance inside the Raspberry Pi reaches 50K ohms

Time control function

unsigned int millis (void)
This function returns the number of milliseconds that have elapsed since your program executed the wiringPiSetup initialization function (or wiringPiSetupGpio) to the current time.
The return type is unsigned int, and the maximum recordable time is about 49 days in milliseconds.
unsigned int micros (void) 这个函数返回 一个 从你的程序执行 wiringPiSetup  初始化函数(或者wiringPiSetupGpio ) 到 当前时间 经过的 微秒数。
返回类型是unsigned int,最大可记录 大约71分钟的时长。
void delay (unsigned int howLong) 将当前执行流暂停 指定的毫秒数。因为Linux本身是多线程的,所以实际暂停时间可能会长一些。参数是unsigned int 类型,最大延时时间可达49天
void delayMicroseconds (unsigned int howLong) 将执行流暂停 指定的微秒数(1000微秒 = 1毫秒 = 0.001秒)。
因为Linux本身是多线程的,所以实际暂停时间可能会长一些。参数是unsigned int 类型,最大延时时间可达71分钟

中断

wiringPi提供了一个中断处理注册函数,它只是一个注册函数,并不处理中断。他无需root权限。

 

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

返回值:返回负数则代表注册失败

pin:接受中断信号的引脚

edgeType:触发的方式。

 INT_EDGE_FALLING:下降沿触发
 INT_EDGE_RISING:上升沿触发
 INT_EDGE_BOTH :上下降都会触发
 INT_EDGE_SETUP:编程时用不到。       

    

function:中断处理函数的指针,它是一个无返回值,无参数的函数。

注册的函数会在中断发生时执行

和51单片机不同的是:这个注册的中断处理函数会和main函数并发执行(同时执行,谁也不耽误谁)

当本次中断函数还未执行完毕,这个时候树莓派又触发了一个中断,那么这个后来的中断不会被丢弃,它仍然可以被执行。但是wiringPi最多可以跟踪并记录后来的仅仅1个中断,如果不止1个,则他们会被忽略,得不到执行。

多线程

wiringPi提供了简单的Linux系统下的通用的 Posix threads线程库接口来支持并发。

 

int piThreadCreate(name)

name:被包装的线程执行函数

返回:状态码。返回0表示成功启动,反之失败。

源代码:
int piThreadCreate (void *(*fn)(void *))
{
  pthread_t myThread ;

  return pthread_create (&myThread, NULL, fn, NULL) ;
}

包装一个用PI_THEEAD定义的函数为一个线程,并启动这个线程。

首先你需要通过以下方式创建一个特特殊的函数,这个函数中的代码就是在新的线程中将执行的代码。,myTread是你自己线程的名字,可自定义。


PI_THREAD (myThread)
{
   //在这里面写上的代码会和主线程并发执行。
}

在wiringPi.h中,我发现这样一个宏定义:#define PI_THREAD(X) void *X (void *dummy)
那么,被预处理后我们写的线程函数会变成下面这个样子,请注意返回值,难怪我每次写都会警告,因为没有返回一个指针,
那么,以后注意返回NULL,或者 (void*)0
void *myThread (void *dummy)
{
 //在这里面写上的代码会和主线程并发执行。
}


piLock(int keyNum) keyNum:0-3的值,每一个值代表一把锁

使能同步锁。wiringPi只提供了4把锁,也就是keyNum只能取0~3的值,官方认为有这4把锁就够了。

keyNum:0,1,2,3 每一个数字就代表一把锁。

源代码:

void piLock (int keyNum)
{
  pthread_mutex_lock (&piMutexes [keyNum]) ;
}

piUnlock(int keyNum) keyNum:0-3的值,每一个值代表一把锁

解锁,或者说让出锁。

源代码:

void piUnlock (int key)
{
  pthread_mutex_unlock (&piMutexes [key]) ;
}

piUnlock(int keyNum) keyNum:0-3的值,每一个值代表一把锁

解锁,或者说让出锁。

源代码:

void piUnlock (int key)
{
  pthread_mutex_unlock (&piMutexes [key]) ;
}

int piHiPri (int priority)

priority:优先级指数,0~99

返回值:0,成功

         -1:,失败

设定线程的优先级,设定线程的优先级变高,不会使程序运行加快,但会使这个线程获得相当更多的时间片。priority是相对的。比如你的程序只用到了主线程,

和另一个线程A,主线程设定优先级为1,A线程设定为2,那也代表A比main线程优先级高。

        凡是涉及到多线程编程,就会涉及到线程安全的问题,多线程访问同一个数据,需要使用同步锁来保障数据操作正确性和符合预期。
        当A线程锁上 锁S 后,其他共用这个锁的竞争线程,只能等到锁被释放,才能继续执行。
        成功执行了piLock 函数的线程将拥有这把锁。其他线程想要拥有这把锁必须等到这个线程释放锁,也就是这个线程执行piUnlock后。
        同时要扩展的知识是:volatile 这个C/C++中的关键字,它请求编译器不缓存这个变量的数据,而是每次都从内存中读取。特别是在多线程下共享放变量,必须使用volatile关键字声明才是保险的。

softPwm,软件实现的PWM

树莓派硬件上支持的PWM输出的引脚有限,为了突破这个限制,wiringPi提供了软件实现的PWM输出API。

需要包含头文件:#include <softPwm.h>

编译时需要添pthread库链接  -lpthread

 

int softPwmCreate (int pin, int initialValue, int pwmRange)

pin:用来作为软件PWM输出的引脚

initalValue:引脚输出的初始值

pwmRange:PWM值的范围上限

建议使用100.

返回:0表示成功。

使用一个指定的pin引脚创建一个模拟的PWM输出引脚
void softPwmWrite (int pin, int value)

pin:通过softPwmCreate创建的引脚

value:PWM引脚输出的值

更新引脚输出的PWM值

 

串口通信

使用时需要包含头文件:#include <wiringSerial.h>

int serialOpen (char *device, int baud)

device:串口的地址,在Linux中就是设备所在的目录。

默认一般是"/dev/ttyAMA0",我的是这样的。

 baud:波特率

返回:正常返回文件描述符,否则返回-1失败。

打开并初始串口

void serialClose (int fd)
fd:文件描述符 关闭fd关联的串口
void  serialPutchar (int fd, unsigned char c)

fd:文件描述符

c:要发送的数据

发送一个字节的数据到串口
void  serialPuts (int fd, char *s)

fd:文件描述符

s:发送的字符串,字符串要以'\0'结尾

发送一个字符串到串口
void  serialPrintf (int fd, char *message, …)

fd:文件描述符

message:格式化的字符串

像使用C语言中的printf一样发送数据到串口
int   serialDataAvail (int fd)

fd:文件描述符

返回:串口缓存中已经接收的,可读取的字节数,-1代表错误

 获取串口缓存中可用的字节数。
int serialGetchar (int fd)

fd:文件描述符

返回:读取到的字符

从串口读取一个字节数据返回。

如果串口缓存中没有可用的数据,则会等待10秒,如果10后还有没,返回-1

所以,在读取前,做好通过serialDataAvail判断下。

void serialFlush (int fd)

fd:文件描述符

刷新,清空串口缓冲中的所有可用的数据。

*size_t write (int fd,const void * buf,size_t count)

fd:文件描述符

buf:需要发送的数据缓存数组

count:发送buf中的前count个字节数据

返回:实际写入的字符数,错误返回-1

这个是Linux下的标准IO库函数,需要包含头文件#include <unistd.h>

当要发送到的数据量过大时,wiringPi建议使用这个函数。

*size_t read(int fd,void * buf ,size_t count);

fd:文件描述符

buf:接受的数据缓存的数组

count:接收的字节数.

返回:实际读取的字符数。

这个是Linux下的标准IO库函数,需要包含头文件#include <unistd.h>

当要接收的数据量过大时,wiringPi建议使用这个函数。

shift移位寄存器芯片API

需要包含头文件  #include <wiringShift.h>

void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)

dPin:移位芯片的串行数据入口引脚,比如74HC595的SER脚

cPin:移位芯片的时钟引脚。如74HC595的11脚

order:

   LSBFIRST 先发送数据的低位

   MSBFIRST先发送数据的高位

     

val:要发送的8位数据

将val串化,通过芯片转化为并行输出

如常见的74HC595

 uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order)

同上。  

将并行数据,通过芯片转化为串行输出。

树莓派硬件平台特有的API

并没有列全,我只是列出了相对来说有用的,其他的,都基本不会用到。

pwmSetMode (int mode)

mode:PWM运行模式

设置PWM的运行模式。

pwm发生器可以运行在2种模式下,通过参数指定:
     PWM_MODE_BAL   :树莓派默认的PWM模式
     PWM_MODE_MS    :传统的pwm模式,

pwmSetRange (unsigned int range)

range,范围的最大值

0~range

设置pwm发生器的数值范围,默认是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.
piBoardRev (void)

返回:树莓派板子的版本编号

1或者2

/

Guess you like

Origin blog.csdn.net/zouchengzhi1021/article/details/113860913