当前位置: 首页 > news >正文

嵌入式 - 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:

  • GPIO breakout cable or prototyping board

  • Solderless breadboard and jumper wires

  • Variety of LEDs, resistors, and switches

  • Low-cost digital multimeter

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执行:能不能存的下不说,串行计算,一份一份文档读,然后进行词频统计&#xff0…...

win10建立共享文件夹和ipad共享文件

win10端设置 查看自己的局域网IP 在任意地方新建一个文件夹 打开文件夹的属性,点到共享的地方 点击高级共享 然后点击应用,确认 再回到之前哪个地方,点击共享 把Everyone的权限改为读取/写入 最后点击共享就欧克了 失败的可能原因 ipad端设置 然后回出现一个要输入用户名和…...

手机在网状态多方面重要性

手机在网状态的重要性体现在多个方面,它是现代社会中人们保持联系、获取信息以及进行日常活动不可或缺的一部分。以下是一些关于手机在网状态重要性的详细解释: 通信联系: 手机是在现代社会中进行通信联系的主要工具。当手机处于在网状态时&…...

Multitouch for Mac:手势自定义,提升工作效率

Multitouch for Mac作为一款触控板手势增强软件,其核心功能在于手势的自定义和与Mac系统的深度整合。通过Multitouch,用户可以轻松设置各种手势,如三指轻点、四指左右滑动等,来执行常见的任务,如打开应用、切换窗口、滚…...

IDEA创建springboot项目时不能选择java 8或者java 11等等版本的问题,解决方案

文章目录 1. Project JDK 和 Java 的区别2. 没有 java 8 或 java 11 等版本2.1 方案一2.2 方案二2.3 方案三 1. Project JDK 和 Java 的区别 我们在利用 idea 创建 spring boot 项目时,会有以上两个选项,这两个选项有什么区别? 答&#xff…...

SpringCloudAlibaba:4.3云原生网关higress的JWT 认证

概述 简介 JWT是一种用于双方之间传递安全信息的简洁的、URL安全的声明规范。 定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信息,特别适用于分布式站点的单点登录(SSO)场景 session认证的缺点 1.安…...

【机器学习】Reinforcement Learning-强化学习基本概念

1、Q值与V值 1.1 Q值和V值的定义 Q值:也称为动作价值函数,评估动作的价值,它代表了智能体选择这个动作后,一直到最终状态奖励总和的期望,表示为Q(s, a),其中 s是状态,a是动作。 V值&#xff…...

vim编辑器---(1)vim编辑器介绍?

(1)vim编辑器介绍? 1 目录 (a)IC简介 (b)vim简介 (c)Verilog简介 (d)vim编辑器介绍? (e)结束 1 IC简介…...

解密 Unix 中的 “rc“ 后缀:自定义你的工作环境

在文件名中,rc 通常表示 “run commands”(运行命令)或者 “runtime configuration”(运行时配置)。这种命名惯例源自早期的 Unix 系统,用于指示这些文件包含了一系列要在程序运行时执行的命令或配置选项。…...

Java使用csv导出多字段大数据文件(无需写实体映射,自动遍历)

csv工具类CsvUtils 此处使用LinkedHashMap链表哈希表,实现键值中值为空时仍存在数据以及保证顺序与sql顺序一致。 package com.xxx.xxx.utils;import lombok.val; import org.springframework.util.CollectionUtils; import javax.servlet.http.HttpServletRespons…...

Redis 本机无法访问

问题 我在服务器上有两个 Redis 实例,服务端口号分别是 6379 和 6380,Redis 服务器地址假设为 10.0.0.12。其中 6379 这个实例不需要密码即可访问,6380 需要密码访问。 在正常使用几天后,本机突然无法访问 6379 这个实例&#x…...

【论文笔记】Training language models to follow instructions with human feedback B部分

Training language models to follow instructions with human feedback B 部分 回顾一下第一代 GPT-1 : 设计思路是 “海量无标记文本进行无监督预训练少量有标签文本有监督微调” 范式;模型架构是基于 Transformer 的叠加解码器(掩码自注意…...

stm32——OLED篇

技术笔记! 一、OLED显示屏介绍(了解) 1. OLED显示屏简介 二、OLED驱动原理(熟悉) 1. 驱动OLED驱动芯片的步骤 2. SSD1306工作时序 三、OLED驱动芯片简介(掌握) 1. 常用SSD1306指令 2. …...

重卡生产流程的可视化管理与优化

重卡车间可视化是一个将车间内部生产流程、设备状态及人员配置直观展现的技术手段,确保制造过程的每个环节都在最优状态下运行。 在重卡制造领域,从底盘组装、车身焊接、涂装到最终的总装和检验,每一个工作过程都至关重要,对于保…...