回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【雕爷学编程】Arduino动手做(93)--- 0.96寸OLED液晶屏模块15 中等

头像 驴友花雕 2023.07.27 19 1

37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞不掂的问题,希望能够抛砖引玉。

 

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验九十三:0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块

 

131.jpg

  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
 实验九十三: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
 项目四十七:简单的文本反转

 

 实验场景图
 

 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验九十七: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
  项目四十七:简单的文本反转
  实验接线: 
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
*/

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

SSD1306AsciiWire oled;
//------------------------------------------------------------------------------
void setup() {
  Wire.begin();
  Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0

  oled.setFont(System5x7);
  oled.clear();
  oled.println("normal");
  oled.println();
}
//------------------------------------------------------------------------------
void loop() {
  for (int i = 0; i < 4; i++) {
    // Toggle invert mode for next line of text.
    oled.setInvertMode(i%2);
    oled.print("\rinvert");
    delay(500);
  }
  for (int i = 0; i < 4; i++) {
    // Invert all text on screen.
    oled.invertDisplay(!(i%2));
    delay(1000);
  }
}

Arduino实验场景图

 

170.jpg
171.jpg

 【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
 实验九十三: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
 项目四十八:64 像素高显示的示例滚动显示

 实验开源代码

 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验九十七: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
  项目四十八:64 像素高显示的示例滚动显示
  实验接线: 
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
*/

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

SSD1306AsciiWire oled;
//------------------------------------------------------------------------------
void setup() {
  Wire.begin();
  Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0

  oled.setFont(System5x7);
  
  #if INCLUDE_SCROLLING == 0
  #error INCLUDE_SCROLLING must be non-zero.  Edit SSD1306Ascii.h
  #endif //  INCLUDE_SCROLLING
  // Set auto scrolling at end of window.
  oled.setScrollMode(SCROLL_MODE_AUTO);
  
  for (int i = 0; i <= 20; i++) {
    if (i == 10) {
      oled.clear();
    }
    oled.print("Line ");
    oled.println(i);
    delay(500);
  }
  // don't scroll last line.
  oled.print("Done");
}
//------------------------------------------------------------------------------
void loop() {}

Arduino实验场景图

 

172.jpg

  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
 实验九十三: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
 项目四十九:动态读取、显示六个 ADC 的值

 实验开源代码
 

 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验九十七: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
  项目四十九:动态读取、显示六个 ADC 的值
  实验接线: 
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
*/

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

SSD1306AsciiWire oled;

uint8_t col[2]; // Columns for ADC values.
uint8_t rows;   // Rows per line.
//------------------------------------------------------------------------------
void setup() {
  Wire.begin();
  Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0

  oled.setFont(System5x7);
  oled.clear();

  // Setup form.  Could use F() macro to save RAM on AVR.
  oled.println("ADC0: 9999 ADC1: 9999");
  oled.println("ADC2: 9999 ADC3: 9999");
  oled.println("ADC4: 9999 ADC5: 9999");
  
  // Calculate columns for ADC values.  No RAM is used by strings.
  // Compiler replaces strlen() calc with 6 and 17.  
  col[0] = oled.fieldWidth(strlen("ADC0: "));
  col[1] = oled.fieldWidth(strlen("ADC0: 9999 ADC1: "));
  rows = oled.fontRows();
  delay(3000);  
}
//------------------------------------------------------------------------------
void loop() {
  for (uint8_t i = 0; i < 6; i++) {  
    oled.clearField(col[i%2], rows*(i/2), 4);    
    oled.print(analogRead(i));
  }
  delay(1000);
}

Arduino实验场景图

 

173.jpg

  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验九十三: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
  项目五十:上下高级滚动功能显示文本

 实验开源代码
 

代码
/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验九十七: 0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
  项目五十:上下高级滚动功能显示文本
  实验接线: 
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
*/

#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN 8

SSD1306AsciiWire oled;

int blank = 0; // Count of blank lines.
int count = 0; // Count of displayed lines.
int dir = 1;   // Scroll direction.
uint32_t scrollTime = 0;
//------------------------------------------------------------------------------
void setup () {
  Wire.begin();
  Wire.setClock(400000L);

  // MicroOLED64x48 or Adafruit128x32 work well.
#if RST_PIN >= 0
  oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif // RST_PIN >= 0
  oled.setFont(System5x7);
  oled.clear();

  // Not really needed since newline will not scroll the display in this example.
  oled.setScrollMode(SCROLL_MODE_APP);

  oled.print("Smooth\nScrolling\ndemo");
  delay(3000);

  // Set cursor to last row of window.
  oled.setRow(oled.displayRows() - oled.fontRows());
}
//------------------------------------------------------------------------------
void loop () {
  if (!oled.scrollIsSynced()) {
    uint32_t now = millis();
    if ((now - scrollTime) >= 15) {
      // Scroll display window.
      oled.scrollDisplay(dir);
      scrollTime = now;
    }
    // Reduce flicker by allowing display to scroll before writing.
  } else if ((millis() - scrollTime) > 15) {
    // Done if screen is blank.
    if (blank*oled.fontRows() > oled.displayRows()) {
      // Set new direction and magnification.
      blank = 0;
      count = 0;
      // Reverse scroll direction.
      dir = -dir;
      // Set font magnification.
      if (dir > 0) {
        if (oled.magFactor() == 1) {
          oled.set2X();
        } else {
          oled.set1X();
        }
      }
      // Set cursor to first or last line of memory window.
      oled.setCursor(0, dir < 0 ? 0 : oled.displayRows() - oled.fontRows());
    }
    // Scroll memory window.
    oled.scrollMemory(dir*oled.fontRows());
    oled.setCol(0);
    if (count*oled.fontRows() <= oled.displayRows()) {
      oled.print(dir < 0 ? "DN " : "UP ");
      oled.print(++count);
    } else {
      blank++;
    }
    oled.clearToEOL();
  }
}

实验场景图(字体移动速度有一点快,拍虚了)

 

174.jpg

评论

user-avatar
  • hacker_

    hacker_2023.07.27

    666

    0
    icon 他的勋章
      展开更多