极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: eagler8

【Arduino】168种传感器模块系列实验(145)---0.91寸OLED液晶屏

[复制链接]
 楼主| 发表于 2020-2-20 14:56:21 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 15:27:07 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.   实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.   实验接线方法
  5.   oled模块    Ardunio Uno
  6.   GND---------GND接地线
  7.   VCC---------5V 接电源
  8.   SDA---------A4
  9.   SCL ------- A5
  10.   实验之二:自动计数器
  11. */

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

  14. U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(U8X8_PIN_NONE);


  15. int i = 0;
  16. void setup(void)
  17. {
  18.   u8x8.begin();
  19.   u8x8.setPowerSave(0);
  20. }

  21. void loop(void)
  22. {
  23.   
  24.   //u8x8.setFont(u8x8_font_chroma48medium8_r); //小字体,细
  25.   //u8x8.setFont(u8x8_font_pxplustandynewtv_r); //小字体,粗
  26.   //u8x8.setFont(u8x8_font_lucasarts_scumm_subtitle_r_2x2_r); //两行字体,细
  27.   u8x8.setFont(u8x8_font_px437wyse700a_2x2_r); //两行字体,粗

  28.   
  29.   String s = "PA:" ;
  30.   String s2;
  31.   s2 = s + i;
  32.   u8x8.drawString(0,0,s2.c_str());

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

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

  36.   delay(50);
  37.   i ++;
  38. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 15:27:27 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 16:11:01 | 显示全部楼层
  1. /*
  2.     【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.     实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.     安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  5.     安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  6.     实验接线方法
  7.     oled模块    Ardunio Uno
  8.     GND---------GND接地线
  9.     VCC---------5V 接电源
  10.     SDA---------A4
  11.     SCL ------- A5
  12.     实验之三:显示英文 Hello, world!
  13. */

  14. #include <SPI.h>
  15. #include <Wire.h>
  16. #include <Adafruit_GFX.h>
  17. #include <Adafruit_SSD1306.h>

  18. #define OLED_RESET 4
  19. Adafruit_SSD1306 display(OLED_RESET);

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

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

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

  28.   display.clearDisplay();

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

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

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

  42. void loop() {

  43. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 16:13:50 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 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地址是加上了第零位读写位后的数值。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 09:25:13 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.   实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.   安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  5.   安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  6.   实验接线方法
  7.   oled模块    Ardunio Uno
  8.   GND---------GND接地线
  9.   VCC---------5V 接电源
  10.   SDA---------A4
  11.   SCL ------- A5
  12.   实验之四:输出直线和文字以及文字滚动
  13. */

  14. #include <SPI.h>
  15. #include <Wire.h>
  16. #include <Adafruit_GFX.h>
  17. #include <Adafruit_SSD1306.h>

  18. #define OLED_RESET 4
  19. Adafruit_SSD1306 display(OLED_RESET);

  20. #if (SSD1306_LCDHEIGHT != 32)

  21. #endif

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

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

  28. void loop() {
  29.   display.clearDisplay();
  30.   testdrawline();
  31.   delay(1000);

  32.   display.clearDisplay();
  33.   testdrawchar();
  34.   delay(1000);

  35.   display.clearDisplay();
  36.   testdrawchar2();
  37.   delay(2000);

  38.   display.clearDisplay();
  39.   testscrolltext();
  40.   delay(2000);
  41. }

  42. void testdrawline() {
  43.   for (int16_t i = 0; i < display.width(); i += 2) {
  44.     // x1 y1 x2 y2
  45.     display.drawLine(i, 0, i, display.height(), WHITE);
  46.     display.display();
  47.     delay(5);
  48.   }
  49.   delay(250);
  50.   display.clearDisplay();
  51.   for (int16_t i = 0; i < display.height(); i += 2) {
  52.     display.drawLine(0, i, display.width(), i, WHITE);
  53.     display.display();
  54.     delay(5);
  55.   }
  56. }

  57. void testdrawchar(void) {
  58.   display.setTextSize(1);
  59.   display.setTextColor(WHITE);
  60.   display.setCursor(0, 0);

  61.   for (uint8_t i = 0; i < 168; i++) {
  62.     if (i == '\n') continue;
  63.     display.write(i);
  64.     if ((i > 0) && (i % 21 == 0))
  65.       display.println();
  66.   }
  67.   display.display();
  68.   delay(1);
  69. }

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

  73.   uint8_t j = 47;
  74.   for (uint8_t l = 0; l < 4; l++) {
  75.     display.setCursor(0, l * 16);
  76.     for (uint8_t i = 0; i < 10; i++) {
  77.       j++;
  78.       if (j == '\n') continue;
  79.       display.write(j);
  80.       display.display();
  81.       delay(5);
  82.     }
  83.   }
  84. }

  85. void testscrolltext(void) {
  86.   testdrawchar();
  87.   delay(5);
  88.   // startscrollright(uint8_t start, uint8_t stop)
  89.   // 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)
  90.   // Parameters: start First row to scroll, stop  Last row to scroll
  91.   display.startscrollright(0x00, 0x02);
  92.   delay(1000);
  93.   display.startscrollright(0x03, 0x05);
  94.   delay(1000);
  95.   display.stopscroll();
  96.   delay(1000);
  97.   // 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)
  98.   display.startscrollleft(0x00, 0x05);
  99.   delay(2000);
  100.   display.startscrollleft(0x00, 0x0F);
  101.   delay(2000);
  102.   display.stopscroll();
  103.   delay(1000);
  104.   // Activate a scroll to the upper right for rows start through stop The display is 16 rows tall.
  105.   display.startscrolldiagright(0x00, 0x03);
  106.   delay(2000);
  107.   display.startscrolldiagright(0x00, 0x07);
  108.   delay(2000);
  109.   // Activate a scroll to the upper left for rows start through stop The display is 16 rows tall.
  110.   display.startscrolldiagleft(0x00, 0x03);
  111.   delay(2000);
  112.   display.startscrolldiagleft(0x00, 0x07);
  113.   delay(2000);
  114.   display.stopscroll();
  115. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 09:42:47 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 10:00:07 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.   实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.   安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  5.   安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  6.   实验接线方法
  7.   oled模块    Ardunio Uno
  8.   GND---------GND接地线
  9.   VCC---------5V 接电源
  10.   SDA---------A4
  11.   SCL ------- A5
  12.   实验之五:输出输出位图(及汉字位图)——实验室
  13. */

  14. #include <SPI.h>
  15. #include <Wire.h>
  16. #include <Adafruit_GFX.h>
  17. #include <Adafruit_SSD1306.h>

  18. #define OLED_RESET 4
  19. Adafruit_SSD1306 display(OLED_RESET);

  20. #if (SSD1306_LCDHEIGHT != 32)
  21. #endif

  22. #define NUMFLAKES 5
  23. #define XPOS 0
  24. #define YPOS 1
  25. #define DELTAY 2
  26. #define LOGO16_GLCD_HEIGHT 16
  27. #define LOGO16_GLCD_WIDTH  16
  28. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  29. { B00000000, B11000000,
  30.   B00000001, B11000000,
  31.   B00000001, B11000000,
  32.   B00000011, B11100000,
  33.   B11110011, B11100000,
  34.   B11111110, B11111000,
  35.   B01111110, B11111111,
  36.   B00110011, B10011111,
  37.   B00011111, B11111100,
  38.   B00001101, B01110000,
  39.   B00011011, B10100000,
  40.   B00111111, B11100000,
  41.   B00111111, B11110000,
  42.   B01111100, B11110000,
  43.   B01110000, B01110000,
  44.   B00000000, B00110000 };

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

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

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

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

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

  66. void loop() {
  67.   display.clearDisplay();
  68.   display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, WHITE);
  69.   display.display();
  70.   delay(2000);
  71.   display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, BLACK);
  72.   display.display();
  73.   delay(500);
  74.   display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, WHITE);
  75.   display.display();
  76.   delay(500);
  77.   display.clearDisplay();
  78.   display.drawBitmap(0, 0, str_1, 16, 16, WHITE);
  79.   display.drawBitmap(16, 0, str_2, 16, 16, WHITE);
  80.   display.drawBitmap(32, 0, str_3, 16, 16, WHITE);
  81.   display.display();
  82.   delay(2000);
  83.   display.clearDisplay();
  84.   testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
  85. }

  86. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
  87.   uint8_t icons[NUMFLAKES][3];

  88.   // initialize
  89.   for (uint8_t f=0; f< NUMFLAKES; f++) {
  90.     icons[f][XPOS] = random(display.width());
  91.     icons[f][YPOS] = 0;
  92.     icons[f][DELTAY] = random(8) + 2;
  93.    
  94.     Serial.print("x: ");
  95.     Serial.print(icons[f][XPOS], DEC);
  96.     Serial.print(" y: ");
  97.     Serial.print(icons[f][YPOS], DEC);
  98.     Serial.print(" dy: ");
  99.     Serial.println(icons[f][DELTAY], DEC);
  100.   }

  101.   while (1) {
  102.     // draw each icon
  103.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  104.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
  105.     }
  106.     display.display();
  107.     delay(200);
  108.    
  109.     // then erase it + move it
  110.     for (uint8_t f=0; f< NUMFLAKES; f++) {
  111.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
  112.       // move it
  113.       icons[f][YPOS] += icons[f][DELTAY];
  114.       // if its gone, reinit
  115.       if (icons[f][YPOS] > display.height()) {
  116.         icons[f][XPOS] = random(display.width());
  117.         icons[f][YPOS] = 0;
  118.         icons[f][DELTAY] = random(8) + 2;
  119.       }
  120.     }
  121.    }
  122. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 10:03:49 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 11:52:31 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.   实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.   实验接线方法
  5.   oled模块    Ardunio Uno
  6.   GND---------GND接地线
  7.   VCC---------5V 接电源
  8.   SDA---------A4
  9.   SCL ------- A5
  10.   实验之八:查询I2C地址(这里为0x3c)
  11. */

  12. #include <Wire.h>  
  13.    
  14. void setup(){  
  15.   Wire.begin();  
  16.   Serial.begin(9600);  
  17.   Serial.println("\nI2C Scanner");  
  18. }  
  19. void loop(){  
  20.   byte error, address;  
  21.   int nDevices;  
  22.   Serial.println("Scanning...");  
  23.   nDevices = 0;  
  24.   for (address = 1; address < 127; address++ ){  
  25.     // The i2c_scanner uses the return value of  
  26.     // the Write.endTransmisstion to see if  
  27.     // a device did acknowledge to the address.  
  28.     Wire.beginTransmission(address);  
  29.     error = Wire.endTransmission();  
  30.     if (error == 0){  
  31.       Serial.print("I2C device found at address 0x");  
  32.       if (address < 16)  
  33.         Serial.print("0");  
  34.       Serial.print(address, HEX);  
  35.       Serial.println(" !");  
  36.       nDevices++;  
  37.     }else if (error == 4){  
  38.       Serial.print("Unknow error at address 0x");  
  39.       if (address < 16)  
  40.         Serial.print("0");  
  41.       Serial.println(address, HEX);  
  42.     }  
  43.   }  
  44.   if (nDevices == 0)  
  45.     Serial.println("No I2C devices found\n");  
  46.   else  
  47.     Serial.println("done\n");  
  48.   delay(5000); // wait 5 seconds for next scan  
  49. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 11:53:50 | 显示全部楼层
上传到arduino板上,打开串口可查看地址

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 12:46:27 | 显示全部楼层
  1. /*
  2.     【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.     实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.     安装库:IDE—工具—管理库—搜索Adafruit_SSD1306—安装
  5.     安装库:IDE—工具—管理库—搜索Adafruit_GFX—安装
  6.     实验接线方法
  7.     oled模块    Ardunio Uno
  8.     GND---------GND接地线
  9.     VCC---------5V 接电源
  10.     SDA---------A4
  11.     SCL ------- A5
  12.     实验之九:综合测试,显示线段、图形与英文字母数字
  13. */

  14. #include <Wire.h>
  15. #include <Adafruit_GFX.h>
  16. #include <Adafruit_SSD1306.h>

  17. #define OLED_RESET 4
  18. Adafruit_SSD1306 display(OLED_RESET);

  19. #define NUMFLAKES 10
  20. #define XPOS 0
  21. #define YPOS 1
  22. #define DELTAY 2

  23. #define LOGO16_GLCD_HEIGHT 16
  24. #define LOGO16_GLCD_WIDTH  16

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

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

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

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

  42.   display.fillScreen(WHITE);
  43.   display.display();
  44.   delay(2000);

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

  50.   /*-------------------------- 画线 从(0,0)到(128,32)----------------------------*/
  51.   display.clearDisplay();   // clears the screen and buffer
  52.   display.drawLine(0, 0, 128, 32, WHITE);
  53.   display.display();
  54.   delay(2000);
  55.   display.clearDisplay();   // clears the screen and buffer
  56.   display.drawLine(0, 32, 128, 0, WHITE);
  57.   display.display();
  58.   delay(2000);

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

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

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

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

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

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

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

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

  99.   /*------------------------显示英文 数字---------------------*/
  100.   display.clearDisplay();   // clears the screen and buffer
  101.   display.setTextSize(1); //选择字号
  102.   display.setTextColor(WHITE);  //字体颜色
  103.   display.setCursor(0, 0);  //起点坐标
  104.   display.println("Hello, Arduino!");
  105.   display.setTextColor(BLACK, WHITE); // 'inverted' text
  106.   display.println(3.141592);
  107.   display.setTextSize(2);
  108.   display.setTextColor(WHITE);
  109.   display.print("0x"); display.println(0xDEADBEEF, HEX);
  110.   display.display();
  111.   delay(2000);
  112. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-21 13:25:05 | 显示全部楼层
  1. /*
  2.     【Arduino】168种传感器模块系列实验(资料 +代码 +图形 +仿真)
  3.     实验一百四十四:0.91寸OLED液晶屏显示模块 IIC
  4.     安装库:IDE—工具—管理库—搜索U8g2lib—安装
  5.    
  6.     实验接线方法
  7.     oled模块    Ardunio Uno
  8.     GND---------GND接地线
  9.     VCC---------5V 接电源
  10.     SDA---------A4
  11.     SCL ------- A5
  12.     实验之十:综合测试,显示电子钟
  13. */

  14. #include <Arduino.h>
  15. #include <U8g2lib.h>
  16. #include <Wire.h>

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

  19. void setup(void) {
  20.   u8g2.begin();
  21. }

  22. uint8_t m = 24;

  23. void loop(void) {
  24.   char m_str[3];
  25.   strcpy(m_str, u8x8_u8toa(m, 2));    /* convert m to a string with two digits */
  26.   u8g2.firstPage();
  27.   do {
  28.     u8g2.setFont(u8g2_font_logisoso62_tn);
  29.     u8g2.drawStr(0, 63, "6");
  30.     u8g2.drawStr(33, 63, ":");
  31.     u8g2.drawStr(50, 63, m_str);
  32.   } while ( u8g2.nextPage() );
  33.   delay(1000);
  34.   m++;
  35.   if ( m == 60 )
  36.     m = 0;
  37. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 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
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 21:31 , Processed in 0.041948 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表