eagler8 发表于 2020-2-20 14:56:21

eagler8 发表于 2020-2-20 15:27:07

/*
【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
实验接线方法
oled模块    Ardunio Uno
GND---------GND接地线
VCC---------5V 接电源
SDA---------A4
SCL ------- A5
实验之二:自动计数器
*/

#include <Arduino.h>
#include <U8x8lib.h>

U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(U8X8_PIN_NONE);


int i = 0;
void setup(void)
{
u8x8.begin();
u8x8.setPowerSave(0);
}

void loop(void)
{

//u8x8.setFont(u8x8_font_chroma48medium8_r); //小字体,细
//u8x8.setFont(u8x8_font_pxplustandynewtv_r); //小字体,粗
//u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_r_2x2_r); //两行字体,细
u8x8.setFont(u8x8_font_px437wyse700a_2x2_r); //两行字体,粗


String s = "PA:" ;
String s2;
s2 = s + i;
u8x8.drawString(0,0,s2.c_str());

s = "TL:";
s2 = s + (i * 2);

u8x8.drawString(0,2,s2.c_str());

delay(50);
i ++;
}

eagler8 发表于 2020-2-20 15:27:27

eagler8 发表于 2020-2-20 16:11:01

/*
  【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  实验接线方法
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
  实验之三:显示英文 Hello, world!
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()   {
Serial.begin(9600);

// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3D (for the 128x64)
// init done

display.clearDisplay();

//英文字符显示
display.setTextSize(1);             //设置字体大小
display.setTextColor(WHITE);      //设置字体颜色白色
display.setCursor(0, 0);            //设置字体的起始位置
display.println("Hello, world!");   //输出字符并换行

display.setTextColor(BLACK, WHITE); //设置字体黑色,字体背景白色
display.println(3.141592);          //输出数字并换行

display.setTextSize(2);             //设置字体大小
display.setTextColor(WHITE);      //设置字体白色
display.print("0x");                //输出字符
display.println(0xDEADBEEF, HEX);   //输出为ASCII编码的十六进制
//display.display();                  //显示以上
}

void loop() {

}

eagler8 发表于 2020-2-20 16:13:50

eagler8 发表于 2020-2-21 09:00:32

I2C驱动的128x32 OLED
I2C (Inter-Integrated Circuit) 集成电路总线是I2CBus的简称,是一种串行通信总线,使用多主从架构。飞利浦公司在1980年代为了让主板,嵌入式系统或手机用以连接低速周边设备而发展。 I2C的正确读法为"I-squared-C"。I2C只使用两条双向漏极开路(Open Drain): 串行数据SDA及串行时钟频率SCL总线,且利用上拉电阻将两条总线的电位上拉。I2C允许相当大的工作电压范围,但典型的电压准位为+3.3V或+5V. I2C的参考设计使用一个7bit长度的地址空间但保留了16个地址,所以在一组总线最多可和112个节点通信。

常见的I2C总线依传输速率的不同而有不同的模式: 标准模式100 Kbit/s,低速模式10 Kbit/s,但时钟频率可被允许下降至零,这代表可以暂停通信。而新一代的I2C总线可以和更多的节点(支持10比特长度的地址空间)以更快的速率通信: 快速模式400 Kbit/s,高速模式3.4 Mbit/s。在单片机中使用I2C通信协议的时候,需要编写程序去模拟I2C总线的通信,对于I2C通信协议需要补充的一点是: 在实际通信传输数据时,SCL总线拉高的时间只要大于1.5μs都能够正常传输数据。

这块128x32 OLED的裸屏是由SSD1306驱动的,该芯片专为共阴极 OLED 面板设计,SSD1306 中嵌入了对比度控制器,显示 RAM 和晶振,并因此减少了外部器件和功耗,有 256级亮度控制,数据/命令的发送有三种接口可选择: 6800/8000串口、I2C接口或 SPI 接口。适用于多数简单的应用,移动电话的屏显, MP3播放器和计算器等。

SSD1306本身支持多种总线驱动方式包括SPI以及并口等,通过芯片的相应IO口拉低拉高来选择哪一种接口。模块通过电阻将相应IO口配置固化使用了I2C接口方式,但可能你买到的同样的驱动芯片的模块会采用其他接口。使用I2C接口时, SSD1306允许有最多两个7位的I2C地址,同样通过相应的IO口拉低拉高来切换, 一般默认是0x3C。在有些模块(不是所有,有些PCB没有预留)的背面,可以看到I2C地址选项提示,需要改变模块I2C地址时只需要把提示位置的电阻取下焊接到另外一端即可。要注意的是版上的I2C地址是加上了第零位读写位后的数值。

eagler8 发表于 2020-2-21 09:25:13

/*
【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
实验接线方法
oled模块    Ardunio Uno
GND---------GND接地线
VCC---------5V 接电源
SDA---------A4
SCL ------- A5
实验之四:输出直线和文字以及文字滚动
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)

#endif

void setup() {
Serial.begin(115200);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32)
display.display();
delay(2000);
}

void loop() {
display.clearDisplay();
testdrawline();
delay(1000);

display.clearDisplay();
testdrawchar();
delay(1000);

display.clearDisplay();
testdrawchar2();
delay(2000);

display.clearDisplay();
testscrolltext();
delay(2000);
}

void testdrawline() {
for (int16_t i = 0; i < display.width(); i += 2) {
    // x1 y1 x2 y2
    display.drawLine(i, 0, i, display.height(), WHITE);
    display.display();
    delay(5);
}
delay(250);
display.clearDisplay();
for (int16_t i = 0; i < display.height(); i += 2) {
    display.drawLine(0, i, display.width(), i, WHITE);
    display.display();
    delay(5);
}
}

void testdrawchar(void) {
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);

for (uint8_t i = 0; i < 168; i++) {
    if (i == '\n') continue;
    display.write(i);
    if ((i > 0) && (i % 21 == 0))
      display.println();
}
display.display();
delay(1);
}

void testdrawchar2(void) {
display.setTextSize(2);
display.setTextColor(WHITE);

uint8_t j = 47;
for (uint8_t l = 0; l < 4; l++) {
    display.setCursor(0, l * 16);
    for (uint8_t i = 0; i < 10; i++) {
      j++;
      if (j == '\n') continue;
      display.write(j);
      display.display();
      delay(5);
    }
}
}

void testscrolltext(void) {
testdrawchar();
delay(5);
// startscrollright(uint8_t start, uint8_t stop)
// Activate a scroll to the right for rows start through stop The display is 16 rows tall. To scroll the whole display, run: display.scrollright(0x00, 0x0F)
// Parameters: start First row to scroll, stopLast row to scroll
display.startscrollright(0x00, 0x02);
delay(1000);
display.startscrollright(0x03, 0x05);
delay(1000);
display.stopscroll();
delay(1000);
// Activate a scroll to the left for rows start through stop The display is 16 rows tall. To scroll the whole display, run: display.startscrollright(0x00, 0x0F)
display.startscrollleft(0x00, 0x05);
delay(2000);
display.startscrollleft(0x00, 0x0F);
delay(2000);
display.stopscroll();
delay(1000);
// Activate a scroll to the upper right for rows start through stop The display is 16 rows tall.
display.startscrolldiagright(0x00, 0x03);
delay(2000);
display.startscrolldiagright(0x00, 0x07);
delay(2000);
// Activate a scroll to the upper left for rows start through stop The display is 16 rows tall.
display.startscrolldiagleft(0x00, 0x03);
delay(2000);
display.startscrolldiagleft(0x00, 0x07);
delay(2000);
display.stopscroll();
}

eagler8 发表于 2020-2-21 09:42:47

eagler8 发表于 2020-2-21 10:00:07

/*
【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
实验接线方法
oled模块    Ardunio Uno
GND---------GND接地线
VCC---------5V 接电源
SDA---------A4
SCL ------- A5
实验之五:输出输出位图(及汉字位图)——实验室
*/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#if (SSD1306_LCDHEIGHT != 32)
#endif

#define NUMFLAKES 5
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };

//中文:实
static const unsigned char PROGMEM str_1[] = {   
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x88,0x84,0x04,0x80,0x04,0x80,0x10,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x01,0x40,0x02,0x20,0x04,0x10,0x18,0x08,0x60,0x04
};

//中文:验
static const unsigned char PROGMEM str_2[] = {   
0x00,0x20,0xF8,0x20,0x08,0x50,0x48,0x50,0x48,0x88,0x49,0x04,0x4A,0xFA,0x7C,0x00,
0x04,0x44,0x04,0x24,0x1D,0x24,0xE4,0xA8,0x44,0x88,0x04,0x10,0x2B,0xFE,0x10,0x00
};

//中文:室
static const unsigned char PROGMEM str_3[] = {   
0x02,0x00,0x01,0x00,0x7F,0xFE,0x40,0x02,0x80,0x04,0x3F,0xF8,0x04,0x00,0x08,0x20,
0x1F,0xF0,0x01,0x10,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00
};

void setup() {
Serial.begin(115200);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x64)
display.display();
delay(2000);
}

void loop() {
display.clearDisplay();
display.drawBitmap(30, 16,logo16_glcd_bmp, 16, 16, WHITE);
display.display();
delay(2000);
display.drawBitmap(30, 16,logo16_glcd_bmp, 16, 16, BLACK);
display.display();
delay(500);
display.drawBitmap(30, 16,logo16_glcd_bmp, 16, 16, WHITE);
display.display();
delay(500);
display.clearDisplay();
display.drawBitmap(0, 0, str_1, 16, 16, WHITE);
display.drawBitmap(16, 0, str_2, 16, 16, WHITE);
display.drawBitmap(32, 0, str_3, 16, 16, WHITE);
display.display();
delay(2000);
display.clearDisplay();
testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
}

void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons;

// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
    icons = random(display.width());
    icons = 0;
    icons = random(8) + 2;
   
    Serial.print("x: ");
    Serial.print(icons, DEC);
    Serial.print(" y: ");
    Serial.print(icons, DEC);
    Serial.print(" dy: ");
    Serial.println(icons, DEC);
}

while (1) {
    // draw each icon
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons, icons, bitmap, w, h, WHITE);
    }
    display.display();
    delay(200);
   
    // then erase it + move it
    for (uint8_t f=0; f< NUMFLAKES; f++) {
      display.drawBitmap(icons, icons, bitmap, w, h, BLACK);
      // move it
      icons += icons;
      // if its gone, reinit
      if (icons > display.height()) {
      icons = random(display.width());
      icons = 0;
      icons = random(8) + 2;
      }
    }
   }
}

eagler8 发表于 2020-2-21 10:03:49

eagler8 发表于 2020-2-21 11:52:31

/*
【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
实验接线方法
oled模块    Ardunio Uno
GND---------GND接地线
VCC---------5V 接电源
SDA---------A4
SCL ------- A5
实验之八:查询I2C地址(这里为0x3c)
*/

#include <Wire.h>
   
void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ ){
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0){
      Serial.print("I2C device found at address 0x");
      if (address < 16)
      Serial.print("0");
      Serial.print(address, HEX);
      Serial.println(" !");
      nDevices++;
    }else if (error == 4){
      Serial.print("Unknow error at address 0x");
      if (address < 16)
      Serial.print("0");
      Serial.println(address, HEX);
    }
}
if (nDevices == 0)
    Serial.println("No I2C devices found\n");
else
    Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}

eagler8 发表于 2020-2-21 11:53:50

上传到arduino板上,打开串口可查看地址

eagler8 发表于 2020-2-21 12:46:27

/*
  【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  实验接线方法
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
  实验之九:综合测试,显示线段、图形与英文字母数字
*/

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH16

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()      //初始化
{
Serial.begin(9600);
delay(500);

// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);// initialize with the I2C addr 0x3C (for the 128x32) ,0x3C为I2C协议通讯地址,需根据实际情况更改
}
void loop()
{
test_SSD1306();   //调用测试函数
}

void test_SSD1306(void)   //测试函数
{
/*-----------------点亮全屏检测屏幕是否有不正常点亮现象-----------------------------*/

display.fillScreen(WHITE);
display.display();
delay(2000);

/*------------------------------画点 点坐标(64,16)-------------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawPixel(64, 16, WHITE);
display.display();
delay(2000);

/*-------------------------- 画线 从(0,0)到(128,32)----------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawLine(0, 0, 128, 32, WHITE);
display.display();
delay(2000);
display.clearDisplay();   // clears the screen and buffer
display.drawLine(0, 32, 128, 0, WHITE);
display.display();
delay(2000);

/*--------------.画空心矩形左上角坐标(x0,y0)右下角坐标(x1,y1)------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawRect(0, 0, 128, 32, WHITE);
display.display();
delay(2000);

/*-----------------------实心矩形---------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.fillRect(0, 0, 128, 32, WHITE);
display.display();
delay(2000);

/*------------------------画空心圆-------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawCircle(64, 16, 13, WHITE);
display.display();
delay(2000);

/*----------------------画实心圆---------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.fillCircle(64, 16, 13, WHITE);
display.display();
delay(2000);

/*---------------------画空心三角形-------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawTriangle(32, 0, 0, 30, 128, 30, WHITE);
display.display();
delay(2000);

/*------------------------画实心三角形-----------------------*/
display.clearDisplay();   // clears the screen and buffer
display.fillTriangle(32, 0, 0, 30, 128, 30, WHITE);
display.display();
delay(2000);

/*-----------------------空心圆角矩形------------------------*/
display.clearDisplay();   // clears the screen and buffer
display.drawRoundRect(0, 0, 128, 32, 5, WHITE);
display.display();
delay(2000);

/*----------------------画实心圆角矩形-----------------------*/
display.clearDisplay();   // clears the screen and buffer
display.fillRoundRect(0, 0, 128, 32, 5, WHITE);
display.display();
delay(2000);

/*------------------------显示英文 数字---------------------*/
display.clearDisplay();   // clears the screen and buffer
display.setTextSize(1); //选择字号
display.setTextColor(WHITE);//字体颜色
display.setCursor(0, 0);//起点坐标
display.println("Hello, Arduino!");
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.println(3.141592);
display.setTextSize(2);
display.setTextColor(WHITE);
display.print("0x"); display.println(0xDEADBEEF, HEX);
display.display();
delay(2000);
}

eagler8 发表于 2020-2-21 13:25:05

/*
  【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  安装库:IDE—工具—管理库—搜索U8g2lib—安装
 
  实验接线方法
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
  实验之十:综合测试,显示电子钟
*/

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>

//iic驱动方式
U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);

void setup(void) {
u8g2.begin();
}

uint8_t m = 24;

void loop(void) {
char m_str;
strcpy(m_str, u8x8_u8toa(m, 2));    /* convert m to a string with two digits */
u8g2.firstPage();
do {
    u8g2.setFont(u8g2_font_logisoso62_tn);
    u8g2.drawStr(0, 63, "6");
    u8g2.drawStr(33, 63, ":");
    u8g2.drawStr(50, 63, m_str);
} while ( u8g2.nextPage() );
delay(1000);
m++;
if ( m == 60 )
    m = 0;
}

eagler8 发表于 2020-2-21 13:32:16

相关函数
u8g2.begin():U8g2构造函数。
u8g2.clean():清除屏幕显示,清除缓冲区,光标回到原点位置。
u8g2.setFont():设置字体。
u8g2.drawStr():绘制字符串。
u8g2.firstPage()/nextPage():循环刷新显示。

U8g2库提供的API函数有很多,其他的介绍可以参考官方手册。
(https://github.com/olikraus/u8g2/wiki/u8g2reference)
页: 1 [2] 3 4
查看完整版本: 【Arduino】168种传感器模块系列实验(145)---0.91寸OLED液晶屏