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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/10819.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/10819.shtml
英文地址,請注明出處:http://en.pswp.cn/web/10819.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

實體類和Entity Class之間有什么聯系

實體類(Entity Class)和Entity Class在本質上是相同的,它們都是面向對象編程(OOP)中用于表示具有業務邏輯意義的實體的類。 具體來說,實體類通常被設計用于代表真實世界中的對象或概念,這些對象…

PWRWER

編譯燒錄完代碼之后,按下復位鍵屏幕會進行刷新,數據不會丟失 如果按下按鍵,進行頁擦除,之后再按下復位鍵,發現屏幕不會再進行刷新,原因是程序已經被擦除,損毀,無法運行,此…

2024OD機試卷-查找接口成功率最優時間段 (java\python\c++)

題目:查找接口成功率最優時間段 題目描述 服務之間交換的接口成功率作為 服務調用 關鍵質量特性,某個時間段內的接口失敗率使用一個數組表示, 數組中每個元素都是單位時間內失敗率數值,數組中的數值為0~100的整數, 給定一個數值(minAverageLost)表示某個時間段內平均失敗…

圖片轉word如何轉換?

要將圖片轉換為Word文檔,你可以使用以下方法之一: 以上這些方法都可以幫助你將圖片中的文本轉換為可編輯的Word文檔,你可以根據自己的喜好和需求選擇其中一種方法來操作。 使用OCR軟件或在線工具:有許多OCR(Optical Ch…

【數據庫】為何選擇B+樹作為索引?與紅黑樹、B樹的對比

摘要: 數據庫索引是數據庫系統中至關重要的組成部分,影響著數據檢索的效率和性能。本文將探討為何數據庫選擇B樹作為索引的原因,并分別分析紅黑樹和B樹在此場景中的劣勢。 介紹: 數據庫索引是數據庫系統中的重要組成部分&#xf…

實戰LangChain(六):深入LangGraph的高級功能與最佳實踐

實戰LangChain(六):深入LangGraph的高級功能與最佳實踐 實戰LangChain(一):構建您的第一個聊天機器人_langchai 機器人 實戰LangChain(二):探索RAG——為聊天機器人注入知識-CSDN博客 實戰LangChain(三):深化交互——利用Neo4j提升聊天機器人的對話能力 實戰La…

電子資源|基于SSM+vue的電子資源管理系統(源碼+數據庫+文檔)?

電子資源管理系統 目錄 基于SSMvue的電子資源管理系統 一、前言 二、系統設計 三、系統功能設計 1系統功能模塊 2管理員功能模塊 5.2.1管理員功能模塊 5.2.2用戶功能模塊 四、數據庫設計 五、核心代碼 六、論文參考 七、最新計算機畢設選題推薦 八、源碼獲取&am…

【Qt 學習筆記】Qt常用控件 | 多元素控件 | Tree Widget的說明及介紹

博客主頁:Duck Bro 博客主頁系列專欄:Qt 專欄關注博主,后期持續更新系列文章如果有錯誤感謝請大家批評指出,及時修改感謝大家點贊👍收藏?評論? Qt常用控件 | 多元素控件 | Tree Widget的說明及介紹 文章編號&#x…

python代碼實現TF-IDF

1、TF-IDF解釋 TF-IDF(Term frequency–inverse document frequency),中文翻譯就是詞頻 - 逆文檔頻率,是一種用來計算關鍵詞的傳統方法。 TF(Term Frequency):TF 的意思就是詞頻,是…

云計算的優勢與未來發展

隨著數字化轉型的蓬勃發展,云計算作為信息技術應用的基礎設施,逐漸成為企業的首選。云計算以其諸多優勢和未來發展趨勢,為企業帶來了更高效、靈活和創新的IT解決方案,助力企業實現數字化轉型和業務發展。 云計算的優勢 首先&…

C#中的隱式類型轉換和顯式類型轉換

在C#中,類型轉換分為隱式類型轉換(Implicit Type Conversion)和顯式類型轉換(Explicit Type Conversion),也稱為隱式轉換和強制轉換。 隱式類型轉換(Implicit Type Conversion) 隱…

SQL Server共享功能目錄顯示灰色無法自行選擇

SQL Server共享功能目錄顯示灰色無法自行調整 一、 將之前安裝SQL Server卸載干凈 二、 清空注冊表 1. 打開注冊表,winR,輸入regedit 2. 注冊表-》編輯-》查找,輸入C:\Program Files\Microsoft SQL Server\ 3. 注冊表-》編輯-》查找&#x…

算法小記(二分)

題目描述: 輸入 𝑛n 個不超過 109109 的單調不減的(就是后面的數字不小于前面的數字)非負整數 𝑎1,𝑎2,…,𝑎𝑛a1?,a2?,…,an?,然后進行 𝑚m 次詢問。對于每次詢問&a…

docker+nginx+Jenkins自動構建

文章目錄 前言一、實操記錄問下AI:jenkins 配置新增一個mobilegit配置Build TriggersBuild EnvironmentBuild StepsPost-build Actions 上面一頓配置下來,構建 -- FAILURE 總結 前言 在已有docker-Jenkins-nginx 部署方案上,在另外一臺測試…

C++實現一個簡單的控制cpu利用率的程序

寫一個程序&#xff0c;讓控制cpu利用率在20%左右 思路很簡單&#xff1a;每個循環控制sleep的時間占比 #include <iostream> #include <chrono> #include <unistd.h>int main() {int ratio 20;int base_time 1000;int sleeptime base_time * (100-ratio…

【計算機網絡篇】數據鏈路層(10)在物理層擴展以太網

文章目錄 &#x1f354;擴展站點與集線器之間的距離&#x1f6f8;擴展共享式以太網的覆蓋范圍和站點數量 &#x1f354;擴展站點與集線器之間的距離 &#x1f6f8;擴展共享式以太網的覆蓋范圍和站點數量 以太網集線器一般具有8~32個接口&#xff0c;如果要連接的站點數量超過了…

10分鐘入門pandas(一)

pandas 是基于python語言的數據分析處理庫,使用廣泛。本文主要參考pandas的官方入門指導,并結合自己入門使用的一些常用操作進行說明。 pandas通常和numpy結合使用,一般通過如下語句導入numpy和pandas庫。 import numpy as np import pandas as pd一. pandas 數據結構 pan…

基于SSM的計算機課程實驗管理系統的設計與實現(源碼)

| 博主介紹&#xff1a;?程序員徐師兄、8年大廠程序員經歷。全網粉絲15w、csdn博客專家、掘金/華為云/阿里云/InfoQ等平臺優質作者、專注于Java技術領域和畢業項目實戰? &#x1f345;文末獲取源碼聯系&#x1f345; &#x1f447;&#x1f3fb; 精彩專欄推薦訂閱&#x1f44…

大屏分辨率適配插件v-scale-screen

前言&#xff1a;大屏分辨率適配繁多&#xff0c;目前我認為最簡單且問題最少的的方案就是使用v-scale-screen插件&#xff0c;無需考慮單位轉換&#xff0c;position定位也正常使用。 1. 效果 填充滿屏幕的效果 保持寬高比的效果 2. 插件原理 原理是通過css transfom 實現…

macOS12安裝 php8.1和apache

1. 安裝php 8.1 macOS12不再自帶php brew tap shivammathur/php 查看可安裝版本 brew search php 安裝指定版本 brew install php8.1 環境配置 vim ~/.zshrc export PATH"/usr/local/opt/php8.1/bin:$PATH" export PATH"/usr/local/opt/php8.1/sbin:$PAT…