1 Star 6 Fork 2

zeruns / 基于STM32的OLED屏驱动程序,支持软件或硬件I2C(HAL库)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0

基于STM32的OLED屏驱动程序,支持软件或硬件I2C(HAL库)

基于STM32G474的0.96寸OLED(SSD1306)显示屏驱动程序(4针脚I2C接口),支持硬件IIC/软件IIC,HAL库版。

这款驱动程序比较完善,可以实现 英文、整数、浮点数、汉字、图像、二进制数、十六进制数 等内容显示,可以画点、直线、矩形、圆、椭圆、三角形等,支持多种字体,差不多相当于一个简易版图形库了。

该程序是基于江协科技的代码二次修改的,原版程序是基于STM32F103的,且只支持软件I2C,我修改后支持硬件I2C,也可以修改宏定义改成使用软件I2C。

测试硬件为NUCLEO-G474RE开发板

关于OLED的驱动原理,以及驱动程序的使用教程可以看江协科技的视频:https://url.zeruns.tech/L7j6y

电子/单片机技术交流群:820537762

效果图

I2C协议简介

I2C 通讯协议(Inter-Integrated Circuit)是由 Phiilps 公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要 USART、CAN 等通讯协议的外部收发设备(那些电平转化芯片),现在被广泛地使用在系统内多个集成电路(IC)间的通讯。

I2C只有一跟数据总线 SDA(Serial Data Line),串行数据总线,只能一位一位的发送数据,属于串行通信,采用半双工通信。

半双工通信:可以实现双向的通信,但不能在两个方向上同时进行,必须轮流交替进行,其实也可以理解成一种可以切换方向的单工通信,同一时刻必须只能一个方向传输,只需一根数据线。

对于I2C通讯协议把它分为物理层和协议层物理层规定通讯系统中具有机械、电子功能部分的特性(硬件部分),确保原始数据在物理媒体的传输。协议层主要规定通讯逻辑,统一收发双方的数据打包、解包标准(软件层面)。

I2C物理层

I2C 通讯设备之间的常用连接方式

(1) 它是一个支持多设备的总线。“总线”指多个设备共用的信号线。在一个 I2C 通讯总线中,可连接多个 I2C 通讯设备,支持多个通讯主机及多个通讯从机。

(2) 一个 I2C 总线只使用两条总线线路,一条双向串行数据线SDA(Serial Data Line ),一条串行时钟线SCL(Serial Clock Line )。数据线即用来表示数据,时钟线用于数据收发同步

(3) 总线通过上拉电阻接到电源。当 I2C 设备空闲时会输出高阻态,而当所有设备都空闲,都输出高阻态时,由上拉电阻把总线拉成高电平

I2C通信时单片机GPIO口必须设置为开漏输出,否则可能会造成短路。

关于更多STM32的I2C相关信息和使用方法可以看这篇文章:https://url.zeruns.tech/JC0Ah

还有江协科技的STM32入门教程:https://www.bilibili.com/video/BV1th411z7sn?p=31

我这里就不详细讲解了。

使用说明

工程使用Keil5创建,用Vscode+EIDE开发,两个软件都可以打开此工程。

工程文件全部使用UTF-8编码,如果打开显示乱码需要修改编辑器编码为UTF-8。

硬件I2C

STM32CubeMX配置,找到你要用的I2C外设的引脚,并设置引脚功能为SCL和SDA,如下图所示是I2C3的SCL。

接着配置I2C外设,启用对应的I2C外设,速度模式设置为 Fast Mode Plus,速度改成 1000,其他默认就行。

配置GPIO,上面设置完后会自动把那两个引脚配置为复用开漏输出模式,接着只需要把IO输出速度改成 Very High ,还有GPIO标签(User Label)定义分别改成I2C3_SCLI2C3_SDA就行,如果是用的别的I2C也可以设置成别的值,代码对应处要修改一下。改完后点击生成代码。

OLED.c文件里,将 #define OLED_USE_SW_I2C 注释掉,将 #define OLED_USE_HW_I2C 取消注释,如果你用的是别的引脚作为I2C引脚,并且定义了别的名字那就将代码里的 I2C3_SCLI2C3_SDA 也改一下。

软件I2C

STM32CubeMX配置,设置两个引脚作为I2C的SCL和SDA信号线,修改IO口的 User Lable 分别为I2C3_SCLI2C3_SDA,如果改成别的需要到代码里修改一下,IO模式设置为开漏输出,默认输出电平高电平,上拉输出,速度设置到最高,如下图所示。改为后点击生成代码。

OLED.c文件里,将 #define OLED_USE_HW_I2C 注释掉,将 #define OLED_USE_SW_I2C 取消注释,如果你用的是别的引脚作为I2C引脚,并且定义了别的名字那就将代码里的 I2C3_SCLI2C3_SDA 也改一下。

需要用的元件

江协科技的STM32入门套件:https://s.click.taobao.com/NTn9Txt

推荐阅读

GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

基于STM32 HAL库的0.96寸OLED(SSD1306)屏驱动程序,支持软件/硬件IIC 展开 收起
C 等 5 种语言
LGPL-3.0
取消

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/zeruns/STM32-HAL-OLED-I2C.git
git@gitee.com:zeruns/STM32-HAL-OLED-I2C.git
zeruns
STM32-HAL-OLED-I2C
基于STM32的OLED屏驱动程序,支持软件或硬件I2C(HAL库)
master

搜索帮助