嵌入式 - GPIO编程简介
An Introduction to GPIO Programming
By Jeff Tranter Wednesday, June 12, 2019
编者按:本 2019 年博客系列是 ICS 最受欢迎的系列之一,现已更新(2022 年 12 月),以确保内容仍然准确、相关和有用。
本博客是 Integrated Computer Solutions (ICS) 关于 GPIO 编程的新系列博客的开篇。GPIO 即通用输入/输出,是大多数现代嵌入式计算机硬件的一项功能,也是许多嵌入式系统的关键组件。在本系列中,我将结合理论和实际编程示例,介绍嵌入式编程的这一重要方面。在第一篇文章中,我将概述 GPIO 编程、它的用途以及使用它所需了解的一些基本概念。
在接下来的文章中,我将介绍如何从文件系统或 sysfs 接口、Python(使用各种模块)以及 C 或 C++ 对 GPIO 进行编程。我将介绍一些低成本商用嵌入式板卡的功能,包括 Raspberry Pi 系列和 Toradex 的板卡。除了基本的数字输入和输出功能外,我还将介绍其他协议,如 SPI、I2C、1-Wire 和串行接口。
此外,我还会将一些低级编程与 Qt 联系起来,并展示一两个利用 Qt 功能的示例。虽然具体的代码示例是为 Raspberry Pi 或 Toradex 平台编写的,但这些概念也适用于其他硬件,只要这些硬件运行某种形式的 Linux,如 Raspbian、Yocto 或 boot2qt。
| Editor's note: This 2019 blog series, among ICS' most popular, has been updated (December 2022) to ensure the content is still accurate, relevant and useful. This blog launches Integrated Computer Solutions' (ICS) new series on GPIO programming. GPIO, or General-Purpose Input/Output, is a feature of most modern embedded computer hardware and a key component of many embedded systems. In this series, I'll cover this important aspect of embedded programming, with a mix of theory and practical programming examples. In this first installment I'll present an overview of GPIO programming, what it can be used for and some basic concepts you need to know in order to use it. In upcoming posts I explain how to program GPIO, starting with the file system or sysfs interface, as well as from Python (using various modules) and from C or C++. I'll cover the capabilities of some low-cost commercial embedded boards, including the Raspberry Pi series and boards from Toradex. In addition to basic digital input and output functions, I'll address other protocols, such as SPI, I2C, 1-Wire and serial interfaces. As a bonus, I'll tie some of this low-level programming back to Qt and show an example or two that make use of Qt features. While the specific code examples will be written for the Raspberry Pi or Toradex platforms, the concepts will be applicable to other hardware as long as it is running some form of Linux like Raspbian, Yocto or boot2qt. |
What is GPIO?
GPIO 是集成电路或电路板上的一个信号引脚,可用于执行数字输入或输出功能。根据设计,它没有预定义的用途,硬件或软件开发人员可以使用它来执行自己选择的功能。典型的应用包括控制 LED、读取开关和控制各类传感器。
例如,大多数型号的 Raspberry Pi 都有一个 40 针 GPIO 连接器,可连接约 25 条 GPIO 线路。
| A GPIO is a signal pin on an integrated circuit or board that can be used to perform digital input or output functions. By design it has no predefined purpose and can be used by the hardware or software developer to perform the functions they choose. Typical applications include controlling LEDs, reading switches and controlling various types of sensors. Most models of Raspberry Pi, for example, have a 40-pin GPIO connector that provides access to about 25 GPIO lines. |

GPIO 可由专用集成电路实现,或通常由片上系统 (SoC) 或模块上系统 (SoM) 设备直接支持。
GPIO 引脚最常见的功能包括:
-
可通过软件配置为输入或输出
-
启用或禁用
-
设置数字输出的值
-
读取数字输出的值
-
当输入值发生变化时产生中断
GPIO 引脚是数字引脚,这意味着它们只支持高/低或开/关电平。它们通常不支持具有许多离散电压电平的模拟输入或输出。某些 GPIO 引脚可直接支持标准化通信协议,如串行通信、SPI、I2C、PCM 和 PWM。(我将在今后的文章中介绍这些内容)。
| GPIO may be implemented by dedicated integrated circuits, or more often are directly supported by system on a chip (SoC) or system on a module (SoM) devices. The most common functions of GPIO pins include: * Being configurable in software to be input or output * Being enabled or disabled * Setting the value of a digital output * Reading the value of a digital output * Generating an interrupt when the input changes value GPIO pins are digital, meaning they only support high/low or on/off levels. They generally don't support analog input or output with many discrete voltage levels. Some GPIO pins may directly support standardized communication protocols like serial communication, SPI, I2C, PCM, and PWM. (I'll cover these in future posts). |
Hardware Concepts

如果您想尝试 GPIO 编程,您需要一些硬件和工具。除了 Raspberry Pi 这样的电路板外,还需要一些有用的部件和工具:
-
GPIO 分线或原型板
-
无焊面包板和跳线
-
各种 LED、电阻器和开关
-
低成本数字万用表
这些可从许多供应商(如 AdaFruit)处购买。如果不想组装自己的硬件,也可以控制各种市售电路板。在 ICS,我们设计了一个小型 GPIO 学习板(如下图所示),用于我们的嵌入式编程课程。我将在以后的文章中详细介绍。
| If you want to experiment with GPIO programming, you'll need some hardware and tools. In addition to a board like a Raspberry Pi, some useful parts and tools to have include:
These are available from many vendors such as AdaFruit. There are also various commercially available boards that you can control if you don't want to assemble your own hardware. At ICS, we designed a small GPIO Learning Board (shown below) that we use for our embedded programming classes. I'll describe that in more detail in later posts. |
A Word on Safety / 安全问题

如果你是硬件方面的新手,就必须了解一些基本的安全知识。由于 Raspberry Pi 和类似电路板上的电压很低(5 伏或更低),因此只要避免使用任何电源线电压的电路,触电的风险就很小。
如果你要进行任何焊接或剪切导线引线,都应小心谨慎并戴上护目镜。
最有可能发生的事故是不慎损坏 Raspberry Pi 或其他设备。将 GPIO 或其他引脚连接到错误的电压上很容易损坏电路板,使其无法使用。静电,尤其是在干燥的冬季,也会产生高电压,如果你产生静电并触摸电路板,就会损坏硬件。建议在工作区使用接地的防静电垫和/或腕带。
此外,请注意包括 Raspberry Pi 在内的许多设备使用 3.3 伏逻辑电平,如果连接到 5 伏电压(Arduino 等许多其他设备使用 5 伏电压,GPIO 连接器上也有 5 伏电压),可能会损坏设备。
| If you are new to hardware, a few words on basic safety are in order. Since the voltages on a Raspberry Pi and similar boards are low (5 volts or less), the risk of electric shock is minimal as long as you avoid any circuity that operates on power line voltages. You should use caution and wear eye protection if you do any soldering or clip wire leads. The most likely accident is to inadvertently damage your Raspberry Pi or other device. Connecting a GPIO or other pin to the wrong voltage can easily damage the board and render it unusable. Static electricity, especially during the dry winter months, can also generate high voltages that can damage hardware if you develop a static charge and then touch the board. A grounded anti-static mat and/or wrist strap are recommended for your work area. Also be aware that many devices, including the Raspberry Pi, use 3.3 volt logic levels and can be damaged if connected to 5 volts, which is used by many other devices like Arduino and is present on the GPIO connector. |
One Caveat / 一个注意事项
作为一个经常使用 Qt 框架开发图形用户界面应用程序的人,我想提最后一点注意事项。一般来说,嵌入式应用程序不会在运行图形用户界面的 CPU 上进行大量 GPIO 编程。相反,您通常希望使用另一个处理器或微控制器,尤其是在控制硬件有任何实时要求的情况下。
以下是一些例外情况,您可能需要在与图形用户界面相同的 CPU 上进行 GPIO 编程:
1,低速或很少使用的功能,如电源开关感应。
2,使用实时操作系统 (RTOS),可以从进程或线程运行代码,并保证响应时间和延迟时间的设计。
3,使用 Raspberry Pi 等现成的电路板开发初始原型或概念验证,这种电路板并非用于生产。
4,对性能要求不高的教育或自学应用。
| As someone who often uses the Qt framework for developing applications with graphical user interfaces, I'd like to mention one final caveat. Generally speaking, embedded applications will not do any significant amount of GPIO programming on the same CPU as the one that runs the GUI. Instead, you'll typically want to use another processor or microcontroller, particularly if there are any real-time requirements for controlling the hardware. Some exceptions to this rule where you might want to do GPIO programming on the same CPU as the GUI can include: 1. Low-speed or rarely used functions, such as power on/off switch sensing. 2. A design where you're using a real-time operating system (RTOS) and can run code from a process or thread with guaranteed response and latency times. 3. Developing an initial prototype or proof of concept with an off-the-shelf board like the Raspberry Pi that is not meant for production use. 4. Educational or self-learning applications where performance is not a factor. |
随着本系列文章的展开,我们还将推出更多有关 GPIO 编程的内容。希望您能继续关注,尝试并修改其中的一些程序示例。
关于作者
杰夫-特兰特
Jeff Tranter 是 ICS 的 Qt 咨询经理。他负责监督客户软件系统的架构和高层设计。在杰夫的领导下,组织全球团队开发桌面和嵌入式应用程序。他曾在《电子设计》杂志上发表过文章。 (Home | Electronic Design)
| Look for more on GPIO programming as this series unfolds. I hope you follow along, try out and modify some of the program examples. About the author Jeff Tranter Jeff Tranter is a Qt Consulting Manager at ICS. He oversees the architectural and high-level design of software systems for clients. Jeff’s leadership organizes global teams of developers on desktop and embedded applications. He has been published in Electronic Design magazine. |

参考:
An Introduction to GPIO Programming | ICS
相关文章:
嵌入式 - GPIO编程简介
An Introduction to GPIO Programming By Jeff Tranter Wednesday, June 12, 2019 编者按:本 2019 年博客系列是 ICS 最受欢迎的系列之一,现已更新(2022 年 12 月),以确保内容仍然准确、相关和有用。 本博客是 Integr…...
8种区块链开发者必须知道的顶级编程语言!
我来问你一个问题:请说出一种技术,它以去中心化、不可篡改和透明性等核心特征席卷了全球。 这个问题的答案是,当然是区块链,它在近些年进入大家的视野并颠覆了工商业,没有任何其他技术能够做到这一点。 预计从2020年…...
十三、Redis哨兵模式--Sentinel
上一篇介绍了Redis中的主从复制。我们知道Redis主从中一般只有主节点对外提供写操作,如果主节点发生故障,为了保证Redis的可用性,这时就要在可用的slave节点中,挑选一个作为主节点。这种切换操作如果是人为的操作,那么…...
[力扣题解]1005. K 次取反后最大化的数组和
题目:1005. K 次取反后最大化的数组和 思路 贪心法; 用绝对值大小排序,自己写一个比较函数, static bool compare(int a, int b) {return abs(a) > abs(b); }注意这样写出来是降序排列; 代码 class Solution {…...
Web UI自动化测试--PO模式
没有PO实现的测试用例的问题: 重用性低:登录功能重复可维护性差:数据和代码混合可读性差:元素定位方法杂乱(id、xpath、css混杂)可读性差:不易识别操作的含义(特别是css和xpath语法)可维护性差:如果某个元素的属性改了,你要更改多次PO(Page Object Model)页面对象模型…...
Python进阶之-反射机制详解
✨前言: 什么是反射? Python中的“反射”是一个编程术语,它指的是程序在运行时能够检查和操作其自身状态的能力,特别是通过名称(通常是字符串)来访问对象的属性、方法和其他组成部分。这种机制允许代码动态…...
day05-面向对象内存原理和数组
day05 面向对象内存原理和数组 我们在之前已经学习过创建对象了,那么在底层中他是如何运行的。 1.对象内存图 1.1 Java 内存分配 Java 程序在运行时,需要在内存中分配空间。为了提高运算效率,就对空间进行了不同区域的划分,因为每一片区域…...
从头理解transformer,注意力机制(下)
交叉注意力 交叉注意力里面q和KV生成的数据不一样 自注意力机制就是闷头自学 解码器里面的每一层都会拿着编码器结果进行参考,然后比较相互之间的差异。每做一次注意力计算都需要校准一次 编码器和解码器是可以并行进行训练的 训练过程 好久不见输入到编码器&…...
ORA-609频繁出现在alert.log,如何解决?
ORA-609就alertlog中比较常见的一个报错,虽然并没有太大的影响,但是频繁的出现在alert log也是很让人厌烦的事情,本文介绍如何排查解决ORA-609问题。 1.ORA-609官方定义 could not attach to incoming connection Cause Oracle process cou…...
JVM 类加载机制
JVM 类加载机制分为五个部分:加载,验证,准备,解析,初始化,下面我们就分别来看一下这五个过程。 加载 加载是类加载过程中的一个阶段,这个阶段会在内存中生成一个代表这个类的 java.lang.class 对…...
亲测-wordpress文章实时同步发布修改删除多个站点的WP2WP插件
一款将wordpress文章同步到其他WordPress网站的插件,通过这款插件,可以保持不同博客之间文章发布、修改、删除的同步。 安装步骤: 主站和分站都要上传这个插件 1.把插件上传到wp-content\plugins解压出来wp2wp文件夹,然后启用插…...
npm无法安装node-sass 的问题
安装 node-sass 的问题呈现:4.9.0版本无法下载 Downloading binary from https://github.com/sass/node-sass/releases/download/v4.9.0/win32-x64-72_binding.node Cannot download "https://github.com/sass/node-sass/releases/download/v4.9.0/win32-x64-…...
springboot 引入第三方bean
如何进行第三方bean的定义 参数进行自动装配...
安装Nginx
如果没有gcc环境,需要安装gcc yum install gcc-c安装依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel工作目录 mkdir /opt/nginx && cd /opt/nginx下载安装包 wget http://nginx.org/download/nginx-1.32.1.tar.gz解压安装包…...
爬虫工具you-get
you-get是一个简单易上手的爬虫小工具,可以从网络中爬取多媒体信息,包括图片、音频和视频。you-get的github项目地址为:https://github.com/soimort/you-get 一、安装 以下为相关依赖,需要分别安装: Python 3 (必须…...
hal_stm32_RTC函数
1设置当前时间: 调用 HAL_RTC_SetTime 来设置小时、分钟、秒和亚秒。 调用 HAL_RTC_SetDate 来设置年、月、日和星期。 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format); HAL_StatusTypeDef HAL_RTC_SetDat…...
【大数据·Hadoop】从词频统计由浅入深介绍MapReduce分布式计算的设计思想和原理
一、引入:词频统计问题 假如我们有一亿份文档,需要统计这一亿份文档的词频。我们会怎么做,有以下思路 使用单台PC执行:能不能存的下不说,串行计算,一份一份文档读,然后进行词频统计࿰…...
win10建立共享文件夹和ipad共享文件
win10端设置 查看自己的局域网IP 在任意地方新建一个文件夹 打开文件夹的属性,点到共享的地方 点击高级共享 然后点击应用,确认 再回到之前哪个地方,点击共享 把Everyone的权限改为读取/写入 最后点击共享就欧克了 失败的可能原因 ipad端设置 然后回出现一个要输入用户名和…...
手机在网状态多方面重要性
手机在网状态的重要性体现在多个方面,它是现代社会中人们保持联系、获取信息以及进行日常活动不可或缺的一部分。以下是一些关于手机在网状态重要性的详细解释: 通信联系: 手机是在现代社会中进行通信联系的主要工具。当手机处于在网状态时&…...
Multitouch for Mac:手势自定义,提升工作效率
Multitouch for Mac作为一款触控板手势增强软件,其核心功能在于手势的自定义和与Mac系统的深度整合。通过Multitouch,用户可以轻松设置各种手势,如三指轻点、四指左右滑动等,来执行常见的任务,如打开应用、切换窗口、滚…...
RestClient
什么是RestClient RestClient 是 Elasticsearch 官方提供的 Java 低级 REST 客户端,它允许HTTP与Elasticsearch 集群通信,而无需处理 JSON 序列化/反序列化等底层细节。它是 Elasticsearch Java API 客户端的基础。 RestClient 主要特点 轻量级ÿ…...
零门槛NAS搭建:WinNAS如何让普通电脑秒变私有云?
一、核心优势:专为Windows用户设计的极简NAS WinNAS由深圳耘想存储科技开发,是一款收费低廉但功能全面的Windows NAS工具,主打“无学习成本部署” 。与其他NAS软件相比,其优势在于: 无需硬件改造:将任意W…...
Ubuntu系统下交叉编译openssl
一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机:Ubuntu 20.04.6 LTSHost:ARM32位交叉编译器:arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?
Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以? 在 Golang 的面试中,map 类型的使用是一个常见的考点,其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...
Appium+python自动化(十六)- ADB命令
简介 Android 调试桥(adb)是多种用途的工具,该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具,其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利,如安装和调试…...
练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...
【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)
可以使用Sqliteviz这个网站免费编写sql语句,它能够让用户直接在浏览器内练习SQL的语法,不需要安装任何软件。 链接如下: sqliteviz 注意: 在转写SQL语法时,关键字之间有一个特定的顺序,这个顺序会影响到…...
leetcodeSQL解题:3564. 季节性销售分析
leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...
学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2
每日一言 今天的每一份坚持,都是在为未来积攒底气。 案例:OLED显示一个A 这边观察到一个点,怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 : 如果代码里信号切换太快(比如 SDA 刚变,SCL 立刻变&#…...
Linux离线(zip方式)安装docker
目录 基础信息操作系统信息docker信息 安装实例安装步骤示例 遇到的问题问题1:修改默认工作路径启动失败问题2 找不到对应组 基础信息 操作系统信息 OS版本:CentOS 7 64位 内核版本:3.10.0 相关命令: uname -rcat /etc/os-rele…...
