标签:void targe use and str c语言 document color int
Raspberr Pi 官方提供了GPIO的usage,具体位置 https://www.raspberrypi.org/documentation/usage/gpio/
这里整理了一下使用c语言做开发的实际例子。
树莓派内核中已经编译自带了 gpio 的驱动,我们常通过一些第三方写好的库函数来完成具体的操作,比较常见的操作库函数有:
python GPIO
wiringPi
参考1 官方网站 http://wiringpi.com/ 参考2 官方安装教程 http://wiringpi.com/download-and-install/ 参考3 源码下载 https://git.drogon.net/
git clone git://git.drogon.net/wiringPi cd wiringPi ./build
简单测试: wiringPi 包括一套 gpio 命令,使用 gpio 命令可以控制树莓派上的各种接口,通过以下指令可以测试wiringPi 是否安装成功
pi@raspberrypi:~/workplace/raspberry-test $ gpio -v gpio version: 2.46 Copyright (c) 2012-2018 Gordon Henderson This is free software with ABSOLUTELY NO WARRANTY. For details type: gpio -warranty Raspberry Pi Details: Type: Pi 3+, Revision: 03, Memory: 1024MB, Maker: Sony * Device tree is enabled. *--> Raspberry Pi 3 Model B Plus Rev 1.3 * This Raspberry Pi supports user-level GPIO access. pi@raspberrypi:~/workplace/raspberry-test $ gpio readall +-----+-----+---------+------+---+---Pi 3+--+---+------+---------+-----+-----+ | 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 | 0 | IN | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | OUT | 1 | 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 3+--+---+------+---------+-----+-----+
code 如下,操作gpio 0
#include <wiringPi.h> int main(void) { wiringPiSetup() ; pinMode (0, OUTPUT) ; for(;;) { digitalWrite(0, HIGH) ; delay (500) ; digitalWrite(0, LOW) ; delay (500) ; } }
在树莓派上运行,注意以下两点:
gcc -Wall -o test test.c -lwiringPi sudo ./test
实验中,我用手头一个型号为FC-49的蜂鸣报警器,I/O 口接入树莓派 GPIO 0,VCC 接树莓派5v, GND 接树莓派 GDN,蜂鸣报警器会周期响,符合预期。
本文部分内容参考:http://wiki.jikexueyuan.com/project/raspberry-pi/gpio.html
标签:void targe use and str c语言 document color int
原文地址:https://www.cnblogs.com/dream-2017/p/9501901.html