持续创作,加速成长!这是我参与「日新计划 6 月更文挑战」的第16天,点击查看活动详情

玩转树莓派(六)使用C语言 通过BCM2835库控制GPIO

@TOC

一、创建环境

因为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/
  • 下载bcappearm2835-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
  • 解压approach缩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

二、编写代码

  • 编辑gpio_out_bcm2835.c
#include <bcm2835.h>
#include <stdio.h>
#define PIN 18
int main(void)
{
	int i=0;
	if (!bcm2835_init())
		return 1;
	bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
	for(i=0;i<10;i++)
	{
		printf("gpio out: %drn", i);
		bcm2835_gpio_write(PIN, i%2);
		delay(3000);
	}
	bcm2835_close();
	printf("gpio out endrn");
	return 0;
}

三、编译运行

  • 编译,编译时要连接bcm2835的库
  • 运行
pi@raspberrypi:~/CWorkSpace/bcm2835 $ vim gpio_out_bcm2835.c
pi@raspberrypi:~/CWorkSpace/bcm2835 $ gcc -o gpio_out_bcm2835 gpio_out_bcm2835.c -l bcm2835
pi@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

http代理、查看效果

application万用表量下BCM编号18的GPIO.1的引脚,可以发现其间隔3s的高低电平变换了10次。APP

pi@raspberrypi:~/PythonWorkSpace $ gpio readall
 +-----+-----+---------+------+---+---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  | 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  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+

五、BCM2835库简单介绍

函数 int bcm2835_init(void)
说明 Initialise this library.
返回 1:成功;0:失败
函数 int bcm2835_close(void)
说明 Clpython安装教程ose this library and deallocate everything
返回 1:成功;0:失败
函数 void bcm28appstore35_gpio_fsel(uint8_t pin, uint8_t mode)
说明https和http的区别 Each control register has 10 sets of 3 bits per GPIO pin
输入 pin:BCM系统的引脚编号
—— mode: Eachpython可以做什么工作 control register has 10 sets of 3 bits per GPIO pin(如下)
//      000 = GPIO Pin X is an input
//      001 = GPIO Pin X is an output
//      100 = GPIO Pin X takes alternate function 0
//      101 = GPIO Pin X takes alternate function 1
//      110 = GPIO Pin X takes alternate function 2
//      111 = GPIO Pin X takes alternate function 3
//      011 = GPIO Pin X takes alternate function 4
//      010 = GPIO Pin X takes alternate function 5
http 404 void bcm2835_gpio_write(uint8_t pin, uint8_t on)
说明 Seappetitet the state of an output
输入 pin:BCM系统的引脚编号
—— on: 1:高电平;http 3020:低电平