回想一下 iPhone 出现之前的时代,当时你唯一一次触摸手机微小的黑白 LCD 屏幕就是将其擦拭干净。当时,诺基亚的 3310 和 5110 手机就使用了这些微型 LCD。
随着技术的进步,这些显示器在 DIY 世界中找到了新的位置。由于尺寸小(约 1.5 英寸)、成本低、易于使用、功耗低以及能够显示文本和位图,它们很快就受到了爱好者的欢迎。
硬件概述
PCD8544液晶驱动器
该模块的核心是来自飞利浦的强大单芯片低功耗 CMOS LCD 驱动器控制器 – PCD8544。
PCD8544 使用类似 SPI 的串行总线接口与微控制器通信,设计用于操作84×48像素图形显示器。
PCD8544 控制器的片上 LCD 电源和偏置电压生成可降低功耗,使其适合功耗敏感的应用。LCD 通常仅消耗 6 至 7 mA 的电流。
根据数据表,该芯片的工作电压范围为 2.7 至 3.3 V,并具有 3V 通信电平。因此,为了将其与 Arduino 等 5V 逻辑微控制器连接,需要某种逻辑电平转换。
液晶背光
LCD 具有四种不同颜色的背光源:红、绿、蓝和白。
背光由四个分布在显示屏边缘的 LED 组成。要更改 LCD 的背光,请推动背面的金属夹,将 LCD 从板上拆下。您会注意到显示屏边缘焊接有四个 LED。只需将 LED 替换为所需颜色的 LED 即可。
诺基亚 5110 LCD 内存映射
PCD8544 LCD 驱动器包括 504 字节的图形显示数据 RAM (GDDRAM),用于存储要在屏幕上显示的位模式。该内存区域分为 6 个存储体(从 0 到 5)。每个库有 84 个列/段(从 0 到 83)。每列可以存储8位数据(从0到7)。这无疑证明我们有:
6 个存储体 x 84 个段 x 8 位数据 = 4032 位 = 504 字节
下面突出显示了整个内存,包括存储体、段和数据。
每个位代表屏幕上的一个像素,可以通过编程方式打开或关闭该像素。
技术规格
以下是规格:
显示技术 | 点阵液晶屏 |
单片机接口 | SPI |
屏幕尺寸 | 1.5英寸宽 |
解决 | 84×48像素 |
工作电压 | 2.7V – 3.3V |
工作电流 | 最大 50mA |
可视角度 | 180° |
诺基亚 5110 LCD 显示模块引脚分配
诺基亚 5110 LCD 显示模块的引脚排列如下:
快速恢复时间引脚用于重置显示。它是一个低电平有效引脚,这意味着通过将其拉低,可以重置显示屏。通过将此引脚连接到Arduino的复位端,屏幕将自动复位。
CE(芯片启用)引脚用于选择共享同一 SPI 总线的多个连接模块之一。它也是一个低电平有效引脚。
D/C(数据/命令)库使用它来将命令(例如将光标设置到特定位置、清除屏幕等)与数据分开。
DIN是串行数据引脚。
时钟脉冲是串行时钟引脚。
电压控制电路引脚为LCD供电,应连接至3.3V。
BL(背光)引脚控制显示器的背光。通过将此引脚连接到任何支持 PWM 的 Arduino 引脚或使用电位计,可以控制亮度。
接地是接地引脚。
将诺基亚 5110 LCD 显示屏连接到 Arduino
让我们将 LCD 连接到 Arduino。
连接非常简单。串行时钟(CLK)、串行数据(DIN)、数据/命令(DC)、芯片使能(CE)和复位(RST)引脚从Arduino上的引脚7连接到引脚3。
不幸的是,LCD的最大输入电压为3.6V,因此我们无法将其直接连接到标准5V Arduino。我们需要改变水平。
转换电平的最简单方法之一是添加与数据引脚串联的电阻。只需添加与 CLK、DIN、D/C 和 RST 引脚串联的 10k 电阻器,以及与 CE 引脚串联的 1k 电阻器。
最后,背光(BL)引脚通过330Ω限流电阻连接至3.3V。如果您想控制亮度,请添加一个电位器或将此引脚连接到任何支持 PWM 的 Arduino 引脚。
下图显示了接线。
库安装
PCD8544 LCD 控制器具有灵活但复杂的驱动器。要使用 PCD8544 控制器,需要广泛的存储器寻址知识。幸运的是,Adafruit PCD8544 Nokia 5110 LCD 库的编写隐藏了 PCD8544 控制器的复杂性,使我们能够通过简单的命令来控制显示。
要安装库,请导航到Sketch > Include Library > Manage Libraries...等待库管理器下载库索引并更新已安装库的列表。
输入“ nokia ”来过滤您的搜索。应该有几个条目。查找 Adafruit PCD8544 诺基亚 5110 LCD 库。单击该条目,然后选择安装。
该库是一个特定于硬件的库,用于处理较低级别的功能。要显示点、线、圆和矩形等图形基元,必须与Adafruit GFX 库配对。也安装这个库。
在内部,Adafruit PCD8544 库利用Adafruit 总线 IO 库。因此,在库管理器中查找 Adafruit BusIO 并安装它。
Arduino 示例代码 1 – 显示文本
现在到了令人兴奋的部分!这是一个简单的草图,它将执行以下操作:
- 显示简单文本
- 显示反转文本
- 显示数字
- 显示带基数的数字(十六进制、十进制)
- 显示 ASCII 符号
- 旋转文本
该草图将使您全面了解如何操作诺基亚 5110 LCD 显示屏,并可以作为更多实际实验和项目的基础。尝试一下草图,然后我们将详细讨论它。
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Declare LCD object for software SPI
// Adafruit_PCD8544(CLK,DIN,D/C,CE,RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
int rotatetext = 1;
void setup() {
Serial.begin(9600);
//Initialize Display
display.begin();
// you can change the contrast around to adapt the display for the best viewing!
display.setContrast(57);
// Clear the buffer.
display.clearDisplay();
// Display Text
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Hello world!");
display.display();
delay(2000);
display.clearDisplay();
// Display Inverted Text
display.setTextColor(WHITE, BLACK); // 'inverted' text
display.setCursor(0,0);
display.println("Hello world!");
display.display();
delay(2000);
display.clearDisplay();
// Scaling Font Size
display.setTextColor(BLACK);
display.setCursor(0,0);
display.setTextSize(2);
display.println("Hello!");
display.display();
delay(2000);
display.clearDisplay();
// Display Numbers
display.setTextSize(1);
display.setCursor(0,0);
display.println(123456789);
display.display();
delay(2000);
display.clearDisplay();
// Specifying Base For Numbers
display.setCursor(0,0);
display.print("0x"); display.print(0xFF, HEX);
display.print("(HEX) = ");
display.print(0xFF, DEC);
display.println("(DEC)");
display.display();
delay(2000);
display.clearDisplay();
// Display ASCII Characters
display.setCursor(0,0);
display.setTextSize(2);
display.write(3);
display.display();
delay(2000);
display.clearDisplay();
// Text Rotation
while(1)
{
display.clearDisplay();
display.setRotation(rotatetext); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further.
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Text Rotation");
display.display();
delay(1000);
display.clearDisplay();
rotatetext++;
}
}
void loop() {
}
这就是输出的样子。
代码说明:
该草图首先包含三个库:SPI.h、Adafruit_GFX.h 和 Adafruit_PCD8544.h。
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
下一步是创建 Adafruit_PCD8544.h 的对象。Adafruit_PCD8544 构造函数接受五个 Arduino 引脚号,它们连接到显示器的 CLK、DIN、D/C、CE 和 RST 引脚。还rotatetext
定义了一个变量,其意义稍后就会清楚。
// Initialize LCD object for software SPI
// Adafruit_PCD8544(CLK,DIN,D/C,CE,RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
int rotatetext = 1;
在setup函数中,我们使用该begin()
函数初始化LCD对象。我们还使用该函数设置显示器的对比度setContrast(value)
,value
范围为 0 到 100。但是,值 50-60 会产生出色的效果。
之后,我们在将第一条消息打印到屏幕之前清除缓冲区。
//Initialize Display
display.begin();
// you can change the contrast around to adapt the display for the best viewing!
display.setContrast(57);
// Clear the buffer.
display.clearDisplay();
显示简单文本(Hello World)
// Display Text
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Hello world!");
display.display();
delay(2000);
display.clearDisplay();
要在屏幕上显示文本,我们必须首先设置字体大小。setTextSize()
这可以通过调用并传递一个非负数(从 1 开始)作为参数来完成。
接下来,我们通过调用函数设置字体颜色setTextColor()
。分为BLACK
深色背景和WHITE
明亮背景。
在打印消息之前,我们必须首先通过调用该setCursor(X,Y)
函数设置光标位置。屏幕上的像素由其水平 (X) 和垂直 (Y) 坐标引用。原点(0,0)
位于左上角,X正值向右递增,Y正值向下递增。
要在屏幕上打印消息,我们可以使用print(" ")
或println(" ")
函数,类似于在串行监视器上打印数据的方式。请记住,这println()
会将光标移动到下一行。
最后一步是使用display()
命令指示库将屏幕缓冲区批量传输到 PCD8544 控制器的内部存储器,并将内容显示在 LCD 上。
显示反转文本
// Display Inverted Text
display.setTextColor(WHITE, BLACK);
display.setCursor(0,0);
display.println("Hello world!");
display.display();
delay(2000);
display.clearDisplay();
为了显示反转的文本,我们使用该setTextColor(FontColor, BackgroundColor)
函数。如果您仔细观察,您会注意到我们之前只向该函数传递了一个参数,但现在我们传递了两个参数。这是由于函数重载而可能发生的。
在这种情况下,使用setTextColor(BLACK, WHITE)
填充背景上的黑色文本会产生结果。
缩放字体大小
// Scaling Font Size
display.setTextColor(BLACK);
display.setCursor(0,0);
display.setTextSize(2);
display.println("Hello!");
display.display();
delay(2000);
display.clearDisplay();
在本教程的前面,我们使用该setTextSize()
函数来设置字体大小。您可以通过将任何非负整数传递给此函数来缩放字体。
角色以 5:7 的比例呈现。换句话说,传递字体大小 1 会以每个字符 5×7 像素呈现文本,传递字体大小 2 会以每个字符 10×14 像素呈现文本,依此类推。
显示数字
// Display Numbers
display.setTextSize(1);
display.setCursor(0,0);
display.println(123456789);
display.display();
delay(2000);
display.clearDisplay();
print()
或函数println()
可用于在 LCD 上显示数字。由于这些函数的重载实现接受 32 位无符号 int 值,因此只能显示 0 到 4,294,967,295 范围内的数字。
指定数字的基数
// Specifying Base For Numbers
display.setCursor(0,0);
display.print("0x"); display.print(0xFF, HEX);
display.print("(HEX) = ");
display.print(0xFF, DEC);
display.println("(DEC)");
display.display();
delay(2000);
display.clearDisplay();
和函数有一个可选的第二个参数,print()
用于println()
指定基数(格式);有效值为 BIN(二进制,或基数 2)、OCT(八进制,或基数 8)、DEC(十进制,或基数 10)和 HEX(十六进制,或基数 16)。对于浮点数,此参数指定要使用的小数位数。例如:
- print(78, BIN) 打印“1001110”
- print(78, OCT) 打印“116”
- print(78, DEC) 打印“78”
- print(78, HEX) 打印“4E”
- println(1.23456, 0) 打印“1”
- println(1.23456, 2) 打印“1.23”
- println(1.23456, 4) 打印“1.2346”
显示 ASCII 符号
// Display ASCII Characters
display.setCursor(0,0);
display.setTextSize(2);
display.write(3);
display.display();
delay(2000);
display.clearDisplay();
和函数将print()
数据println()
以人类可读的 ASCII 文本形式发送到显示器,而 函数write()
将二进制数据发送到显示器。因此,该函数可用于显示 ASCII 符号。例如,发送3
会显示一个心形符号。
文本旋转
// Text Rotation
while(1)
{
display.clearDisplay();
display.setRotation(rotatetext);
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("Text Rotation");
display.display();
delay(1000);
display.clearDisplay();
rotatetext++;
}
您可以使用该setRotation()
功能旋转显示的内容。它允许您以纵向或横向模式查看显示屏。
该函数仅采用一个参数,对应于四次基本旋转。这可以是任何从 0 开始的非负整数。当您增加该值时,显示内容将逆时针旋转 90 度。例如:
- 0 – 保持屏幕的标准横向方向。
- 1 – 将屏幕向右旋转 90 度。
- 2 – 将屏幕上下翻转。
- 3 – 将屏幕向左旋转 90 度。
Arduino 示例代码 2 – 基本绘图
这是一个简单的草图,演示了如何绘制矩形、圆形和三角形等基本形状。尝试一下草图,然后我们将详细讨论它。
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Declare LCD object for software SPI
// Adafruit_PCD8544(CLK,DIN,D/C,CE,RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
void setup() {
Serial.begin(9600);
//Initialize Display
display.begin();
// you can change the contrast around to adapt the display for the best viewing!
display.setContrast(57);
// Clear the buffer.
display.clearDisplay();
// Draw Rectangle
display.drawRect(0, 0, 60, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Rectangle
display.fillRect(0, 0, 60, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Round Rectangle
display.drawRoundRect(0, 0, 60, 40, 8, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Round Rectangle
display.fillRoundRect(0, 0, 60, 40, 8, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Circle
display.drawCircle(20, 20, 20, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Circle
display.fillCircle(20, 20, 20, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Triangle
display.drawTriangle(20, 0, 0, 40, 40, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Triangle
display.fillTriangle(20, 0, 0, 40, 40, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
}
这就是输出的样子。
大部分代码(设置显示)与上面的代码示例相同,除了以下绘制基本形状的代码片段。
绘制矩形
// Draw Rectangle
display.drawRect(0, 0, 60, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Rectangle
display.fillRect(0, 0, 60, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
该drawRect()
函数可用于在屏幕上绘制一个矩形。该函数接受五个参数:X 和 Y 坐标、宽度、高度和颜色。该函数实际上绘制了一个带有 1 像素边框的空心矩形。该fillRect()
函数可用于绘制填充矩形。
绘制圆角矩形
//Draw Round Rectangle
display.drawRoundRect(0, 0, 60, 40, 8, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Round Rectangle
display.fillRoundRect(0, 0, 60, 40, 8, BLACK);
display.display();
delay(2000);
display.clearDisplay();
该drawRoundRect()
函数可用于在屏幕上绘制圆角矩形。该函数接受与 相同的参数drawRect()
,但有一个附加参数 - 圆角半径除外。该函数实际上绘制了一个带有 1 像素边框的空心圆矩形。该fillRoundRect()
函数可用于绘制填充的圆角矩形。
画圆
//Draw Circle
display.drawCircle(20, 20, 20, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Circle
display.fillCircle(20, 20, 20, BLACK);
display.display();
delay(2000);
display.clearDisplay();
该drawCircle()
函数可用于在屏幕上画一个圆。该函数接受四个参数:中心的 X 坐标、中心的 Y 坐标、半径和颜色。该函数绘制一个带有 1 像素边框的空心圆。该fillCircle()
函数可用于绘制实心圆。
绘制三角形
//Draw Triangle
display.drawTriangle(20, 0, 0, 40, 40, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
//Draw Filled Triangle
display.fillTriangle(20, 0, 0, 40, 40, 40, BLACK);
display.display();
delay(2000);
display.clearDisplay();
该drawTriangle()
函数可用于在屏幕上绘制三角形。该函数接受七个参数:三角形顶点的三个 X 和 Y 坐标(x0、y0、x1、y1、x2 和 y2)以及颜色。(X0, y0) 是顶部顶点,(x1, y1) 是左侧顶点,(x2, y2) 是右侧顶点。
该函数绘制一个带有 1 像素边框的空心三角形。该fillTriangle()
函数可用于绘制实心三角形。
Arduino 示例代码 3 – 显示位图
我们的最后一个示例展示了如何在诺基亚 5110 LCD 显示屏上绘制位图图像。当显示徽标、精灵、信息图表或图标等内容时,这会派上用场。
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Declare LCD object for software SPI
// Adafruit_PCD8544(CLK,DIN,D/C,CE,RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
// 'Marilyn Monroe 84x48', 84x48px
const unsigned char MarilynMonroe [] PROGMEM = {
0x00, 0x00, 0x00, 0x7f, 0x00, 0x02, 0xfe, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x00,
0x00, 0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x1f, 0xe1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
0x00, 0x00, 0x0f, 0xf1, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xd8, 0xe0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x80, 0x00, 0x07, 0xe0, 0x70, 0x00, 0x00, 0x00, 0x00, 0x03,
0x3f, 0xe0, 0x00, 0x07, 0xf0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x70, 0x00, 0x0f, 0xee,
0x7c, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x0f, 0xf7, 0x1c, 0x00, 0x00, 0x00, 0x00,
0x07, 0x80, 0x00, 0x0f, 0xc7, 0xf3, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x0f, 0xf3,
0xdf, 0x7f, 0x80, 0x00, 0x00, 0x00, 0x07, 0xfe, 0x00, 0x08, 0x7d, 0xef, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x7f, 0xff, 0x80, 0x30, 0x0f, 0xfc, 0xe0, 0xc0, 0x00, 0x00, 0x01, 0x9e, 0x73, 0xc0, 0xe0,
0x07, 0xf8, 0xc1, 0xc0, 0x00, 0x00, 0x03, 0xfc, 0x00, 0x01, 0xc0, 0x0f, 0xfd, 0xe1, 0x80, 0x00,
0x00, 0x03, 0xf8, 0x00, 0x01, 0x9c, 0x0f, 0xff, 0xc1, 0xc0, 0x00, 0x00, 0x02, 0xc0, 0x00, 0x01,
0x9f, 0xbf, 0xfe, 0x01, 0x40, 0x00, 0x00, 0x02, 0x60, 0x00, 0x03, 0x07, 0xef, 0xff, 0x01, 0x40,
0x00, 0x00, 0x00, 0x60, 0x00, 0x07, 0x01, 0xf7, 0xff, 0x80, 0xc0, 0x00, 0x00, 0x00, 0x50, 0x01,
0xdf, 0x00, 0x7f, 0xff, 0x1c, 0x80, 0x00, 0x00, 0x00, 0x40, 0x01, 0xff, 0x00, 0x1f, 0xff, 0x1e,
0xe0, 0x00, 0x00, 0x02, 0x08, 0x00, 0x3f, 0x80, 0x07, 0xef, 0x03, 0xe0, 0x00, 0x00, 0x06, 0x08,
0x00, 0x03, 0xc0, 0x07, 0xdf, 0x07, 0xc0, 0x00, 0x00, 0x06, 0x08, 0x0f, 0x81, 0x80, 0x1f, 0xdf,
0x1f, 0x80, 0x00, 0x00, 0x03, 0x08, 0x1f, 0x98, 0x00, 0x3f, 0xfe, 0x19, 0x80, 0x00, 0x00, 0x18,
0x08, 0x3f, 0xfe, 0x00, 0x7f, 0xfe, 0x3f, 0x00, 0x00, 0x00, 0x08, 0x08, 0x30, 0x3f, 0x00, 0xff,
0xff, 0x3f, 0x00, 0x00, 0x00, 0x01, 0xe0, 0x76, 0x0f, 0x89, 0xff, 0xff, 0x9f, 0x00, 0x00, 0x00,
0x03, 0xe0, 0x7f, 0xc3, 0x81, 0xff, 0xfe, 0x9f, 0x80, 0x00, 0x00, 0x03, 0xf0, 0x7f, 0xf3, 0xc3,
0xff, 0xfe, 0x1f, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x7f, 0xfd, 0xc3, 0xff, 0xfe, 0x5e, 0x00, 0x00,
0x00, 0x03, 0xf0, 0x7f, 0xff, 0xc3, 0xff, 0xf3, 0x1e, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x71, 0xff,
0x87, 0xff, 0xe3, 0xff, 0x00, 0x00, 0x00, 0x07, 0xf0, 0x7c, 0x3f, 0x87, 0xff, 0xe3, 0xfe, 0x00,
0x00, 0x00, 0x0f, 0xf0, 0x3c, 0xff, 0x05, 0xff, 0xf3, 0xfc, 0x00, 0x00, 0x00, 0x0f, 0xf0, 0x0f,
0xfe, 0x09, 0xff, 0xf7, 0xfc, 0x00, 0x00, 0x00, 0x08, 0xf8, 0x01, 0xfc, 0x19, 0xff, 0xff, 0xf8,
0x00, 0x00, 0x00, 0x0c, 0x78, 0x00, 0x00, 0x13, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0e, 0x78,
0x00, 0x00, 0x23, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x0e, 0xf8, 0x00, 0x00, 0x47, 0xff, 0xff,
0xf0, 0x00, 0x00, 0x00, 0x0c, 0xfa, 0x00, 0x01, 0x8f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x08,
0x7b, 0x00, 0x03, 0x3f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x0c, 0x3b, 0xf8, 0x0f, 0xff, 0xff,
0xff, 0xe0, 0x00, 0x00, 0x00, 0x0f, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00,
0x07, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x71, 0xff, 0xff, 0xff,
0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x41, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x00
};
void setup() {
Serial.begin(9600);
//Initialize Display
display.begin();
// you can change the contrast around to adapt the display for the best viewing!
display.setContrast(57);
// Clear the buffer.
display.clearDisplay();
// Display bitmap
display.drawBitmap(0, 0, MarilynMonroe, 84, 48, BLACK);
display.display();
// Invert Display
//display.invertDisplay(1);
}
void loop() {
}
这就是输出的样子。
代码说明:
该drawBitmap()
函数用于在 LCD 上显示位图图像。该函数接受六个参数:左上角 X 坐标、左上角 Y 坐标、单色位图字节数组、位图宽度(以像素为单位)、位图高度(以像素为单位)和颜色。
我们示例中的位图图像大小为 84×48。因此,X 和 Y 坐标设置为 0,而宽度和高度设置为 84 和 48。
// Display bitmap
display.drawBitmap(0, 0, MarilynMonroe, 84, 48, BLACK);
display.display();
但是,在使用该drawBitmap()
功能之前,我们需要绘制图像。请记住,显示器的屏幕分辨率为 84×48 像素,因此大于该分辨率的图像将无法正常显示。要获得正确大小的图像,请打开您最喜欢的绘图程序,例如 Inkscape、Photoshop 或 MS Paint,并将画布大小设置为 84×48 像素。
我们以玛丽莲·梦露的照片为例。我们在 MS Paint 中将其更改为 84×48 像素,并将其保存为 .bmp 文件。
获得位图后,必须将其转换为 PCD8544 控制器可以理解的数组。这可以通过两种方式完成:使用 image2cpp(在线)或使用 LCD Assistant(离线)。
在线位图数组生成器 – image2cpp
有一个名为image2cpp 的在线工具,可以将图像转换为数组。该工具允许您:
- 同时转换多个图像。
- 缩放图像文件 – 拉伸/缩放以适合/原始
- 调整黑色和白色之间的亮度阈值。
- 重新垂直和/或水平居中图像。
- 反转图像颜色
首先,在浏览器中打开 image2cpp 并选择要在屏幕上显示的图像。
图像的尺寸将显示在图像设置下的画布尺寸选项中。如果您的图像大于 84×48,请通过选择适当的缩放选项将其更改为 84×48。您可以在预览部分看到结果。
如有必要,您可以更改背景颜色或反转图像颜色。
最后,更改最重要的设置,亮度阈值。当您设置阈值时,高于该水平的像素将为白色,低于该水平的像素将为黑色。在我们的例子中,我们将其设置为 171 以获得一些不错的细节。
此预览反映了您对设置所做的任何更改。您可以在密切关注的同时进行更改。
当您对结果感到满意时,您可以继续生成数据数组。只需选择 Arduino Code 作为代码输出格式,然后按“生成代码”按钮。
供您参考,有一个名为“绘图模式”的设置。它实际上根据显示器的扫描模式生成图像。如果屏幕上的图像出现扭曲,请尝试更改模式。
就这样。将创建位图的字节数组。您可以直接将输出与我们的示例代码一起使用。只要确保给它一个正确的名称即可。然后,在drawBitmap()
函数中使用您的数组。
离线位图数组生成器-LCD助手
还有一个名为LCD Assistant的 Windows 应用程序,可以将位图图像转换为数据数组。它不像 image2cpp 那样强大,但仍然被爱好者广泛使用。
首先,将图像转换为 84×48 1 位单色位图。您可以在您最喜欢的绘图程序中完成此操作,例如 Inkscape、Photoshop 或 MS Paint,就像我们在 MS Paint 中所做的那样。
打开 MS Paint 并将文件大小调整为 84×48 像素。
现在,将文件另存为位图。保存文件时,选择单色位图(*.bmp;*.dib)。这将生成一个 1 位/二进制位图图像,每个像素只有两个可能的值:0(黑色)或 1(白色)。
这种方法的唯一缺点是您无法设置亮度阈值级别。默认设置为 50% 并且无法更改。
现在,下载 LCD Assistant 程序。打开可执行文件并从“文件”菜单中选择您的位图。
只需转到“文件”菜单并选择“保存输出”选项即可。将文件另存为文本文档。
供您参考,有一个名为“字节方向”的设置。它实际上根据显示器的扫描模式生成图像。如果屏幕上的图像出现扭曲,请尝试更改模式。
就这样。创建数组后,将其复制并粘贴到代码中。只要确保给它一个正确的名称即可。然后,在drawBitmap()
函数中使用您的数组。