玩转树莓派(六)使用C语言 通过BCM2835库控制GPIO
目录
- 玩转树莓派(六)使用C语言 通过BCM2835库控制GPIO
- 一、创建环境
- 二、编写代码
- 三、编译运行
- 四、查看效果
- 五、BCM2835库简单介绍
- 六、漂亮的Ending
一、创建环境
因为BCM2835 Library在Raspberry Pi OS系统中没有内置,所以需要用户自行下载。
- 新建CWorkSpace文件夹
- 新建bcm2835文件夹
pi@raspberrypi:~ $ mkdir CWorkSpace
pi@raspberrypi:~ $ cd CWorkSpace/
pi@raspberrypi:~/CWorkSpace $ mkdir bcm2835
pi@raspberrypi:~/CWorkSpace $ cd bcm2835/
- 下载bcm2835-1.50.tar.gz(BCM2835 Library)
pi@raspberrypi:~/CWorkSpace/bcm2835 $ wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz
--2021-02-04 00:08:38-- http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz
正在解析主机 www.airspayce.com (www.airspayce.com)... 192.185.48.187
正在连接 www.airspayce.com (www.airspayce.com)|192.185.48.187|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:252178 (246K) [application/x-gzip]
正在保存至: “bcm2835-1.50.tar.gz”bcm2835-1.50.tar.gz 100%[=====================================================================>] 246.27K 7.96KB/s 用时 23s 2021-02-04 00:09:03 (10.5 KB/s) - 已保存 “bcm2835-1.50.tar.gz” [252178/252178])pi@raspberrypi:~/CWorkSpace/bcm2835 $ ls
bcm2835-1.50.tar.gz
pi@raspberrypi:~/CWorkSpace/bcm2835 $ tar -zxvf bcm2835-1.50.tar.gz
pi@raspberrypi:~/CWorkSpace/bcm2835 $ cd bcm2835-1.50/
pi@raspberrypi:~/CWorkSpace/bcm2835/bcm2835-1.50 $ ls
aclocal.m4 ChangeLog config.h.in configure COPYING doc INSTALL ltmain.sh Makefile.in NEWS src
AUTHORS config.guess config.sub configure.ac depcomp examples install-sh Makefile.am missing README test-driver
pi@raspberrypi:~/CWorkSpace/bcm2835/bcm2835-1.50 $ ./configure && make
pi@raspberrypi:~/CWorkSpace/bcm2835/bcm2835-1.50 $ sudo make install
二、编写代码
#include
#include #define PIN 18
int main(void)
{int i&#61;0;if (!bcm2835_init())return 1;bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);for(i&#61;0;i<10;i&#43;&#43;){printf("gpio out: %d\r\n", i);bcm2835_gpio_write(PIN, i%2);delay(3000);}bcm2835_close();printf("gpio out end\r\n");return 0;
}
三、编译运行
- 编译&#xff0c;编译时要连接bcm2835的库
- 运行
pi&#64;raspberrypi:~/CWorkSpace/bcm2835 $ vim gpio_out_bcm2835.c
pi&#64;raspberrypi:~/CWorkSpace/bcm2835 $ gcc -o gpio_out_bcm2835 gpio_out_bcm2835.c -l bcm2835
pi&#64;raspberrypi:~/CWorkSpace/bcm2835 $ ./gpio_out_bcm2835
gpio out: 0
gpio out: 1
gpio out: 2
gpio out: 3
gpio out: 4
gpio out: 5
gpio out: 6
gpio out: 7
gpio out: 8
gpio out: 9
gpio out end
四、查看效果
用万用表量下BCM编号18的GPIO.1的引脚&#xff0c;可以发现其间隔3s的高低电平变换了10次。
pi&#64;raspberrypi:~/PythonWorkSpace $ gpio readall&#43;-----&#43;-----&#43;---------&#43;------&#43;---&#43;---Pi 3B--&#43;---&#43;------&#43;---------&#43;-----&#43;-----&#43;| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |&#43;-----&#43;-----&#43;---------&#43;------&#43;---&#43;----&#43;&#43;----&#43;---&#43;------&#43;---------&#43;-----&#43;-----&#43;| | | 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 | IN | 0 | 11 || 12 | 1 | OUT | 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 |&#43;-----&#43;-----&#43;---------&#43;------&#43;---&#43;----&#43;&#43;----&#43;---&#43;------&#43;---------&#43;-----&#43;-----&#43;| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |&#43;-----&#43;-----&#43;---------&#43;------&#43;---&#43;---Pi 3B--&#43;---&#43;------&#43;---------&#43;-----&#43;-----&#43;
五、BCM2835库简单介绍
函数 | int bcm2835_init(void) |
---|
说明 | Initialise this library. |
返回 | 1&#xff1a;成功&#xff1b;0&#xff1a;失败 |
函数 | int bcm2835_close(void) |
---|
说明 | Close this library and deallocate everything |
返回 | 1&#xff1a;成功&#xff1b;0&#xff1a;失败 |
函数 | void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode) |
---|
说明 | Each control register has 10 sets of 3 bits per GPIO pin |
输入 | pin&#xff1a;BCM系统的引脚编号 |
—— | mode&#xff1a; Each control register has 10 sets of 3 bits per GPIO pin&#xff08;如下&#xff09; |
函数 | void bcm2835_gpio_write(uint8_t pin, uint8_t on) |
---|
说明 | Set the state of an output |
输入 | pin&#xff1a;BCM系统的引脚编号 |
—— | on&#xff1a; 1&#xff1a;高电平&#xff1b;0&#xff1a;低电平 |
六、漂亮的Ending
觉得好&#xff0c;就打个赏呗
觉得好&#xff0c;就一键三连呗