wiringPi is a great IO control library for Raspberry Pi. It is developed in C language and provides a wealth of interfaces: GPIO control, interrupt, multithreading, etc. To
check if wiringPi is installed on the Raspberry Pi, type in the Raspberry Pi terminal:
gpio -v // 会在终端中输出相关 wiringPi 的信息,否则没有安装
You need to add libraries when using the gcc compiler:
gcc xxx.c -lwiringPi
Common APIs of wiringPi library:
1. Raspberry Pi pin diagram:
gpio readall // 在树莓派终端输入指令查看树莓派 io 口引脚图
+-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
2. Hardware initialization function:
When using wiringPi, you must initialize the Raspberry Pi before performing any operations, otherwise the program will not work normally
Function prototype | Description |
---|---|
int wiringPiSetup (void); | When using this function to initialize the Raspberry Pi pins, the program uses the wiringPi pin number table, the pin numbers are 0~16, and root privileges are required. When successfulreturnExecution status, when failedreturn -1 |
int wiringPiSetupGpio (void); | When using this function to initialize the Raspberry Pi pins, the program uses the BCM GPIO pin number table, which requires root privileges. When successfulreturnExecution status, when failedreturn -1 |
3. General GPIO control function:
Function prototype | parameter | Description |
---|---|---|
void pinMode (int pin, int mode); | int pin: Pin number;int mode: Configured IO mode (INPUT, OUTPUT, PWM_OUTPUT, GPIO_CLOCK) | Set the pin input and output mode (IO mode) and PWM input and output mode (IO mode). Only wPi1 (BCM18) supports PWM output, and only wPi7 (BCM4) supports GPIO_CLOCK output |
void digitalWrite (int pin, int value); | int pin: Pin number;int value: Configured level (HIGH, LOW) | Output a specified level signal to a pin that has been configured as an output mode |
int digitalRead (int pin); | int pin: Pin number;returnThe level on the pin (HIGH, LOW) | Read the level value of a pin |
4. Time control function:
Function prototype | Description |
---|---|
unsigned int millis (void); | This function returns an elapsed time from the program execution wiringPiSetup(); or wiringPiSetupGpio(); initialization function to the current timemillisecondNumber, the return value type is unsigned int, the maximum recordable is about 49 daysmillisecondduration |
unsigned int micros (void); | This function returns an elapsed time from the program execution wiringPiSetup(); or wiringPiSetupGpio(); initialization function to the current timeMicrosecondNumber, the return value type is unsigned int, the maximum recordable is about 71 minutesMicrosecondduration |
void delay (unsigned int howLong); | Pause the current execution flow to the specifiedmillisecondNumber, the maximum delay time can reach 49 days |
void delayMicroseconds (unsigned int howLong); | Suspend the execution flow for the specifiedMicrosecondCount, the maximum delay time can reach 71 minutes |
1 second (s) = 1000 milliseconds (ms) | 1 second (s) = 1000000 microseconds (μs) |
---|
5. Serial communication:
#include <wiringSerial.h> // 以下 API 包含该头文件
Function prototype | parameter | Description |
---|---|---|
int serialOpen(char *device, int baud); | char *device: The address of the serial port. In Linux, it is the directory where the device is located. The default is usually "/dev/ttyAMA0" ;int baud: Baud rate;return value: Return the file descriptor on success, return -1 on failure |
Open and initialize the serial port |
void serialClose(int fd); | inf fd: File descriptor | Close the serial port associated with fd |
void serialPutchar(int fd, unsigned char c); | int fd: File descriptor;unsigned char c: The data to be sent | Send one byte of data to the serial port |
void serialPuts(int fd, char *s); | inf fd: File descriptor;char *s: The data to be sent | Send a string to the serial port |
void serialPrintf(int fd, char *message, …); | int fd: File descriptor;char *message: Formatted string | Send a string to the serial port (similar to the printf(); function in the C language standard library) |
int serialDataAvail(int fd); | int fd: File descriptor;return value: Return the number of bytes on success, -1 on failure | Get the number of bytes available in the serial buffer |
int serialGetchar(int fd); | int fd:文件描述符;返回值:读取的数据 | 从串口读取一个字节数据并返回,如果串口缓冲区中没有可用的数据,则会等待10 秒,若 10 秒后仍无数据,则返回-1,所以读取前最好调用serialDataAvail(); 判断,读取多个字符则采用地址偏移的方式读取 |
void serialFlush(int fd); | int fd:文件描述符 | 清空串口缓冲区的数据 |
头文件:
#include <wiringSerial.h>
#include <unistd.h>
函数原型 | 说明 |
---|---|
ssize_t read(int fd, void *buf, size_t count); | Linux下的标准IO库函数,向fd中读取count个字节的数据,存放到buf中 |
ssize_t write(int fd, const void *buf, size_t count); | Linux下的标准IO库函数,往fd中写入count个字节的数据,内容为buf的前count个字节的数据 |
6.点击了解更多.