精帖  树莓派OLCD液晶屏0.96寸使用资料Python3例程显示屏

By 管理员6

2023-05-23 22:32:52

浏览量2675

已赞9

Image

 ➥ 0.96显示屏模块购买链接 电路设计原理图及软件在教程最后请自行下载


 ➥ 树莓派pico使用0.96寸显示屏例程


下面演示如何在树莓派3B/4B上使用显示屏

一:启用树莓派I2C功能
更新库输入命令: 

sudo apt-get install -y python-imaging python-smbus i2c-tools

开启 I2C 功能开启输入命令:

sudo raspi-config
ImageImage


开启I2C后,请将树莓派关机进行断电。

输入命令进行关机,关机后拔掉电源线。

sudo shutdown -h now 

二:接线方法

1.注意:接线之前请保证树莓派已经断电关机。显示屏很脆弱,切记不要接反了正负极,VCC是正极,GND负极,接反了必定会烧坏屏幕,不可逆转。

显示屏GND —— 树莓派GND
显示屏VCC —— 树莓派5V
显示屏SDA —— 树莓派SDA (在 IIC 通信中为数据管脚)
显示屏SCL —— 树莓派SCL ( IIC 通信中为时钟管脚)


2.我们先检测一下是否识别到显示屏。通过下面命令查询是否有 I2C 设备。如果显示下图这个3c,显示屏就是被正常识别到了。不同的显示屏名称可能有差异,只是通过这个步骤确认一下是否成功连上。 如果你这里没有显示,建议重新插拔线。我建议不要用排线+扩展板的方式将GPIO扩展出来,显示屏接扩展板会导致无法识别出来。

sudo i2cdetect -y 1
Image


三:克隆库

克隆库,按顺序依次输入下面的四条命令

git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git
cd Adafruit_Python_SSD1306
sudo python setup.py install
sudo pip install Adafruit-BBIO
四:显示屏测试运行

cd examples/
sudo python stats.py
如果正常会显示IP地址等如下图:Image

五:代码程序演示

按Ctrl+C结束上面的测试,然后新建一个py文件输入命令

sudo nano raspicc.py
然后编辑程序到文件

import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used

# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Make sure to create image with mode '1' for 1-bit color.
width = disp.width #128
height = disp.height #64
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

#定义Y位置,也就是哪一行
y = -2

# 设置默认字体
font = ImageFont.load_default()
# 也可以输入指定位置的字体文件和字体大小
font16 = ImageFont.truetype('/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBd.ttf', 16)

#程序循环开始
while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    my_date=time.strftime("%Y-%m-%d",time.localtime()) #2020-09-23
    my_time=time.strftime("%H:%M:%S",time.localtime()) #13:15:03
    
    # Write two lines of text.
    draw.text((28, y),    my_time ,  font=font16, fill=255)
    draw.text((16, y+16), my_date ,  font=font16, fill=255)
    draw.text(( 0, y+32), "raspicc" ,  font=font, fill=255)
    draw.text(( 0, y+40), "raspi" ,  font=font, fill=255)
    draw.text(( 0, y+56), "1234567890 1234567890 1234567890" ,  font=font, fill=255)

    # 画多个点:[(点1x,点1y),(点2x,点2y)]
    draw.point([(1,1),(2,2), (3, 3)],fill=255)
    # 画线: 起点x,起点y,终点x,终点y
    draw.line((0, 0, 50, 20), fill=255)
    # 画椭圆: 左上角x,左上角y, 右下角x,右下角y
    draw.ellipse((1, 1 , 50, 30), outline=255, fill=0)
    # 画长方形: 左上角x,左上角y, 右下角x,右下角y
    draw.rectangle((10, 10, 40, 20), outline=255, fill=0)
    # 画三角形::角1x,y,角2x,y,角3x,y
    draw.polygon([(0, 0), (0, 40), (30, 40)], outline=255, fill=0)
    
# Display image.
    disp.image(image)
    disp.display()
    time.sleep(0.2)

然后执行命令运行程序

sudo python raspicc.py
没有问题就会刷新内容显示日期数字等等。

如何显示中文?

上面这个程序只是英文和数字的显示。如果我们要显示中文。需要用取模软件建立字模来实现,软件在教程下面自己下载即可,然后通过软件将每个中文字转成16进制编码就可以了显示出来了。下面的这个代码是我已经编译过的可以粘贴使用。和上面一样的方法,新建一个py文件写入程序进行运行测试。

import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

# Raspberry Pi pin configuration:
RST = None
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width #128
height = disp.height #64
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)


# 调用默认字体,或者自行设置指定字体
font = ImageFont.load_default()
font16 = ImageFont.truetype('/usr/share/fonts/truetype/ttf-bitstream-vera/VeraBd.ttf', 16)

###################################
## 汉字部分
###################################

#1、先建立字模,可以用 pc2lcd2002完美版 软件,设置如下,即可生成字模,注意字模选项要如图设置
#2、将每个中文字转成16进制编码, 可用下面的模块【txt2hex】生成对应关系 如(我= 0xe68891)
#3、把字模每行开头的【改此处】替换为这个汉字的16进制数,
#4、最终字模结果如下:
fonts= {
    0xe6989f: [0x00,0x1F,0x10,0x1F,0x10,0x1F,0x01,0x11,0x1F,0x21,0x41,0x1F,0x01,0x01,0x7F,0x00,0x00,0xF0,0x10,0xF0,0x10,0xF0,0x00,0x00,0xF8,0x00,0x00,0xF0,0x00,0x00,0xFC,0x00],#星0
    0xe69c9f: [0x22,0x22,0x7F,0x22,0x22,0x3E,0x22,0x22,0x3E,0x22,0x22,0xFF,0x04,0x22,0x41,0x82,0x00,0x7C,0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x7C,0x44,0x44,0x84,0x84,0x14,0x08],#期1
    0xe4b880: [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],#一2
    0xe4ba8c: [0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00],#二3
    0xe4b889: [0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00],#三4
    0xe59b9b: [0x00,0x00,0x7F,0x44,0x44,0x44,0x44,0x44,0x48,0x48,0x50,0x60,0x40,0x7F,0x40,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,0x44,0x44,0x3C,0x04,0x04,0x04,0xFC,0x04,0x00],#四5
    0xe4ba94: [0x00,0x7F,0x02,0x02,0x02,0x02,0x3F,0x04,0x04,0x04,0x04,0x08,0x08,0x08,0xFF,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0xFE,0x00],#五6
    0xe585ad: [0x02,0x01,0x00,0x00,0x00,0xFF,0x00,0x00,0x04,0x04,0x08,0x08,0x10,0x20,0x40,0x00,0x00,0x00,0x80,0x80,0x00,0xFE,0x00,0x00,0x40,0x20,0x10,0x08,0x08,0x04,0x04,0x00],#六7
    0xe697a5: [0x00,0x1F,0x10,0x10,0x10,0x10,0x10,0x1F,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0x10,0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0xF0,0x10],#日8
    0xe68891: [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],#我9
    0xe5bc80: [0x00,0x7F,0x08,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0x10,0x10,0x20,0x40,0x00,0xFC,0x20,0x20,0x20,0x20,0x20,0xFE,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20],#开10
    0xe5bf83: [0x00,0x02,0x01,0x00,0x00,0x04,0x04,0x24,0x24,0x24,0x44,0x44,0x84,0x04,0x03,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x08,0x04,0x04,0x02,0x02,0x12,0x10,0x10,0xF0,0x00],#心11
}

#在屏幕上显示汉字, txt2lcd("字符串内容",屏幕x坐标,屏幕y坐标),支持中英文混显
def txt2lcd(ch_str, xx, yy): 
    for k in ch_str:
        print(k)
        if k in ("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlkmnopqrstuvwxyz,.!()"):    #如果当前字符是英文、数字或标点,则直接调用自身字库显示,内容可以自己修改
            draw.text((xx,yy), k,font=font16, fill=255) 
            xx=xx+12
        else:    #如果检测到当前字符是中文,则直接调用自定义的字模显示
            code = 0x00  # 将中文转成16进制编码 
            data_code = k.encode("utf-8")
            code |= data_code[0] << 16 
            code |= data_code[1] << 8
            code |= data_code[2]
            byte_data = fonts[code]
            for y in range(0, 16):
               left8 = bin(byte_data[y]).replace('0b', '')
               left8 = ('00000000'+left8)[-8:]    #如果不足8位,直接补齐8位,如从0010,补齐成00000010,代表汉字每一行左半部分8个像素点。0代表像素点不亮,1代表像素点亮
                   
               right8 = bin(byte_data[y+16]).replace('0b', '')
               right8 = ('00000000'+right8)[-8:]    #代表汉字每一行右半部分8个像素点
                   
               all16 = left8+right8   #拼成每一行16个像素点
               
               #print(all16)
               for x in range(0, 16):      #开始循环检测,如果是1,则在这个位置点亮像素点
                   if all16[x] == "1" :
                       weizhi=(xx+x,yy+y)
                       draw.point(weizhi,fill=255)
            xx=xx+16      #显示完一个汉字,显示下一个时,x位置向右移16像素 


# txt2hex为建字模用的。前期的时候可以用到,后期可以注释掉
# 将中文转成16进制编码,对应关系如(我= 0xe68891)
def txt2hex(ch_str):
   for k in ch_str:
       code = 0x00   
       data_code = k.encode("utf-8")
       code |= data_code[0] << 16 
       code |= data_code[1] << 8
       code |= data_code[2]
       print(k+"  "+hex(code))
txt2hex('星期一二三四五六日我开心')        

#########################
# 主程序循环开始
#########################
while True:
    # 先清理屏幕
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    # 获取日期、时间、星期几
    my_date=time.strftime("%Y-%m-%d",time.localtime())   #获取日期如 2020-09-23
    my_time=time.strftime("%H:%M:%S",time.localtime())   #获取时间如 13:15:03
    my_week=time.strftime("%w",time.localtime())               #获取周几,返回0-6, 0代表周日,1代表周一
    
    # 显示日期、时间、周几(数字格式的)
    draw.text((28, 0),   my_time ,  font=font16, fill=255)
    draw.text((16, 20),  my_date ,  font=font16, fill=255)
    draw.text((88, 40),  "("+my_week+")" ,  font=font16, fill=255)

    # 显示中文的星期几(为了好理解,用的简单易懂的代码)
    if my_week == "0":
        txt2lcd('星期日',40,40)
    elif  my_week=="1":   
        txt2lcd('星期一',40,40)
    elif  my_week=="2":   
        txt2lcd('星期二',40,40)
    elif  my_week=="3":   
        txt2lcd('星期三',40,40)
    elif  my_week=="4":   
        txt2lcd('星期四',40,40)
    elif  my_week=="5":   
        txt2lcd('星期五',40,40)
    elif  my_week=="6":   
        txt2lcd('星期六',40,40)        

    # 刷新屏幕,显示内容
    disp.image(image)
    disp.display()
    
    # 每隔几秒刷新一次
    time.sleep(1)
显示效果展示:

Image

六:PCtoLCD取模软件

取模软件2003完美(中文版)软件下载需1个金币,注册账号签到就自带金币了哈~还是要走下形式emotion

在OLED显示屏上显示字符,和在点阵屏上显示图像是一样的逻辑,都是通过区域内的像素点的亮暗来呈现图案以1608字体码表为例,代码中表现如下,可以下载取模软件自己去获得想要显示的内容代码。

const unsigned char asc2_1608[95][16]={   

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/

{0x00,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x00,0x00},/*""",2*/

{0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x00,0x00},/*"#",3*/

{0x00,0x00,0x0E,0x18,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00,0x00,0x00},/*"$",4*/

{0x0F,0x00,0x10,0x84,0x0F,0x38,0x00,0xC0,0x07,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/

{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x24,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/

{0x08,0x00,0x68,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/

{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/

{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/

}

Image

段前缀:fonts= {

段后缀:}

注释前缀:,#

注释后缀:

数据前缀:0x

数据后缀:,

行前缀:改此处:[

行后缀:

行尾缀:]


教程结束:剩下的就自己下载一个取模软件去研究想显示什么内容吧。

应用案例,最后在网上查到了一个将DHT11温湿度传感器读取的数据来显示到0.96寸显示屏上面,大家可以下载来作为参考

dht11在0.96寸上显示温湿度测试程序

Image


0.96寸OLED显示模块硬件原理图

PCtoLCD2002取模软件



本文章最后由 超级版主2023-07-08 02:52 编辑
发表评论
请先 注册/登录 后参与评论

已有1 发布

默认   热门   正序   倒序
查看更多评论
(9) 分享
分享

扫二维码或复制链接分享该篇文章

取消
已有1次打赏
超级版主打赏给楼主5金币,2023-07-08 02:55:10
本站免责声明
1、本站资源,均来自网络或个人用户发布,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !

侵权删除请致信 E-Mail:cxg88@qq.com