树莓派驱动ssd1306 oeld屏幕显示温度,ip地址,日期

话不多说,先上图在这里插入图片描述

我的oled屏幕是ssd 1306 芯片驱动的,4针脚,只支持iic,不支持spi,双色的。

# Copyright (c) 2017 Adafruit Industries
# Author: Tony DiCola & James DeVito
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
import Adafruit_DHT

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

import subprocess

p=0
sensor=Adafruit_DHT.DHT11
gpio=27
humidity,temperature = Adafruit_DHT.read_retry(sensor,gpio)


# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0


disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)


disp.begin()

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

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

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

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0



font = ImageFont.load_default()


while True:

    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "hostname -I | cut -d\' \' -f1"
    IP = subprocess.check_output(cmd, shell = True )
    cmd = "top -bn2 -d 0.1 | grep %Cpu | awk 'NR==2{printf \"CPU: %.2f%%\",100-$8}'"
    CPU = subprocess.check_output(cmd, shell = True )
    cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
    MemUsage = subprocess.check_output(cmd, shell = True )
    cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
    Disk = subprocess.check_output(cmd, shell = True )
    humidity,temperature = Adafruit_DHT.read_retry(sensor,gpio)
    cmd = "date | awk '{printf \"%s %s %s %s \", $1,$2,$3,$4}'"
    date = subprocess.check_output(cmd, shell = True )
    cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"%.1f\", $0/1000}'"
    cput = subprocess.check_output(cmd, shell= True)



    # Write two lines of text.
    draw.text((x, top),       "IP: " + str(IP),  font=font, fill=255)
    draw.text((x, top+8),     str(CPU), font=font, fill=255)
    draw.text((x+80, top+8),  str(cput),font=font, fill=255)
    draw.text((x, top+16),    str(MemUsage),  font=font, fill=255)
    draw.text((x, top+25),    str(Disk),  font=font, fill=255)
    draw.text((x, top+34),    "temperature: " + str(temperature),font=font,fill=255)
    draw.text((x, top+43),    "humidity: " + str(humidity),font=font,fill=255)
    draw.text((x, top+54),    str(date),  font=font, fill=255)

    disp.image(image)
    disp.display()
    time.sleep(.1)


在驱动示例文件上,我加上了DHT11模块,来显示温度和湿度,屏幕的scl和sda引脚连接到了树莓派的3号和5号引脚,DHT11的out引脚连接到13号引脚

在这里插入图片描述

树莓派要驱动iic,必须要现在配置界面开启iic通信!

用树莓派配置工具开启iic

raspi-config

在这里插入图片描述

选择图中的选项

在这里插入图片描述

选择I2C

在这里插入图片描述

选择yes

在这里插入图片描述

可以看到 I2C已经开启了!

sudo i2cdetect -y 1

可以用这个命令来查看是否已经开启。在这里插入图片描述

树莓派我只连接了一个屏幕,从上图可以看到。

可以设置为开机启动。

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于STM32F103C8T6驱动SSD1306 OLED显示图像的代码: ```c #include "stm32f10x.h" #include "stm32f10x_spi.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #define OLED_CS_H() GPIO_SetBits(GPIOB, GPIO_Pin_12) #define OLED_CS_L() GPIO_ResetBits(GPIOB, GPIO_Pin_12) #define OLED_DC_H() GPIO_SetBits(GPIOB, GPIO_Pin_10) #define OLED_DC_L() GPIO_ResetBits(GPIOB, GPIO_Pin_10) #define OLED_RST_H() GPIO_SetBits(GPIOB, GPIO_Pin_11) #define OLED_RST_L() GPIO_ResetBits(GPIOB, GPIO_Pin_11) void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); } void OLED_Init(void) { OLED_RST_L(); delay_ms(1000); OLED_RST_H(); delay_ms(1000); OLED_CS_L(); OLED_DC_L(); SPI1_Send(0xAE); OLED_DC_H(); SPI1_Send(0x40); SPI1_Send(0xB0); SPI1_Send(0xC8); SPI1_Send(0x81); SPI1_Send(0xff); SPI1_Send(0xa1); SPI1_Send(0xa6); SPI1_Send(0xa8); SPI1_Send(0x3f); SPI1_Send(0xd3); SPI1_Send(0x00); SPI1_Send(0xd5); SPI1_Send(0x80); SPI1_Send(0xd9); SPI1_Send(0xf1); SPI1_Send(0xda); SPI1_Send(0x12); SPI1_Send(0xdb); SPI1_Send(0x40); SPI1_Send(0x20); SPI1_Send(0x00); SPI1_Send(0x21); SPI1_Send(0x00); SPI1_Send(0x7f); SPI1_Send(0x22); SPI1_Send(0x00); SPI1_Send(0x07); OLED_DC_L(); SPI1_Send(0xA4); OLED_DC_H(); SPI1_Send(0xAF); OLED_CS_H(); } void OLED_Display_On(void) { OLED_CS_L(); OLED_DC_L(); SPI1_Send(0xAE); OLED_DC_H(); SPI1_Send(0xAF); OLED_CS_H(); } void OLED_Display_Off(void) { OLED_CS_L(); OLED_DC_L(); SPI1_Send(0xAE); OLED_CS_H(); } void OLED_Clear(void) { uint8_t i, j; OLED_CS_L(); for (i = 0; i < 8; i++) { OLED_DC_L(); SPI1_Send(0xb0 + i); SPI1_Send(0x00); SPI1_Send(0x10); OLED_DC_H(); for (j = 0; j < 128; j++) { SPI1_Send(0); } } OLED_CS_H(); } void OLED_DrawPixel(uint8_t x, uint8_t y) { uint8_t page = y / 8; uint8_t shift = y % 8; OLED_CS_L(); OLED_DC_L(); SPI1_Send(0xb0 + page); SPI1_Send(0x00 + (x & 0x0f)); SPI1_Send(0x10 + ((x >> 4) & 0x0f)); OLED_DC_H(); SPI1_Send(1 << shift); OLED_CS_H(); } void OLED_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) { uint8_t steep = abs(y2 - y1) > abs(x2 - x1); uint8_t dx, dy; int8_t err, ystep; if (steep) { swap(x1, y1); swap(x2, y2); } if (x1 > x2) { swap(x1, x2); swap(y1, y2); } dx = x2 - x1; dy = abs(y2 - y1); err = dx / 2; if (y1 < y2) { ystep = 1; } else { ystep = -1; } for (; x1 <= x2; x1++) { if (steep) { OLED_DrawPixel(y1, x1); } else { OLED_DrawPixel(x1, y1); } err -= dy; if (err < 0) { y1 += ystep; err += dx; } } } void OLED_DrawRect(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2) { OLED_DrawLine(x1, y1, x2, y1); OLED_DrawLine(x1, y1, x1, y2); OLED_DrawLine(x2, y1, x2, y2); OLED_DrawLine(x1, y2, x2, y2); } void OLED_DrawCircle(int16_t x0, int16_t y0, int16_t r) { int16_t x = 0, y = r; int16_t d = 3 - 2 * r; while (x <= y) { OLED_DrawPixel(x0 + x, y0 + y); OLED_DrawPixel(x0 + x, y0 - y); OLED_DrawPixel(x0 - x, y0 + y); OLED_DrawPixel(x0 - x, y0 - y); OLED_DrawPixel(x0 + y, y0 + x); OLED_DrawPixel(x0 + y, y0 - x); OLED_DrawPixel(x0 - y, y0 + x); OLED_DrawPixel(x0 - y, y0 - x); if (d < 0) { d += 4 * x + 6; } else { d += 4 * (x - y) + 10; y--; } x++; } } void OLED_DrawImage(uint8_t *image) { uint8_t i, j; OLED_CS_L(); for (i = 0; i < 8; i++) { OLED_DC_L(); SPI1_Send(0xb0 + i); SPI1_Send(0x00); SPI1_Send(0x10); OLED_DC_H(); for (j = 0; j < 128; j++) { SPI1_Send(image[i * 128 + j]); } } OLED_CS_H(); } int main(void) { SPI_Configuration(); OLED_Init(); OLED_Display_On(); OLED_Clear(); uint8_t image[1024] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; OLED_DrawImage(image); while (1) { } } void SPI1_Send(uint8_t data) { while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); SPI_I2S_SendData(SPI1, data); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); SPI_I2S_ReceiveData(SPI1); } void delay_ms(uint16_t ms) { uint32_t i; for (i = 0; i < ms * 8000; i++); } ``` 需要注意的是,这只是一个简单的例子,实际开发需要根据具体的需求编写更加复杂的程序。同时,需要根据具体的OLED屏幕和STM32微控制器型号,选择相应的驱动程序和通信协议。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值