zoukankan      html  css  js  c++  java
  • arduino驱动oled

    OLED一款小巧的显示屏,感觉可以做出很可爱的东西。

    这次实验的这款是128X64的OLED屏幕 ,

    芯片是SSD1306,请确认自家模块芯片型号,不然对不上号啊

    使用IIC的方法,简单实验显示示例程序。

    (请确认你手头上的模块可以IIC连接,若干不支持那只能SPI方式接线)

    先实现连接与显示,之后再进行更深入的应用。

    任意门:

    Arduino Uno 驱动OLED进阶 显示中英文字

    Arduino Uno 驱动OLED进阶 显示图片

    Arduino Uno 驱动OLED进阶 显示几何动画

    编译的过程,可能会遇到以下问题:

    ①提示错误

    #error("Height incorrect, please fix Adafruit_SSD1306.h!");  

    错误信息意思是指:

    高度不正确,请修正Adafruit_SSD1306.h!

    进入Arduino安装文件夹的libraries文件夹的Adfruit_SSD1306-master 找到Adafruit_SSD1306.h

    打开此文件,找到第70行左右

    默认是定义 SSD1306_128_32 ,由于我们使用的是128*64的OLED,所以,把原来的#define SSD1306_128_32,前面加上//

    把#define SSD_128_64 前面的//去掉  

    最后就如上面图例一样

    ②模块的IIC 地址问题

    模块的地址修改在这个位置,示例程序的61行

    这个模块地址我用的是这个,但每个模块可能不一样,具体请咨询购买的商家,又或者可以参考下面链接的,IIC搜索地址程序。

    任意门:Arduino 和LCD1602液晶屏 I2C接口实验

    实验效果

    BOM表

    Arduino Uno             *1

    OLED 128*64           *1

    跳线若干


    针脚说明

    VCC   接3.3v电源

    GND  接地(GND)

    SCL   时钟线

    SDA   数据线

    接线图

    程序开源代码

    在上代码之前,先下载两个库分别是

    Adafruit SSD1306 Library:

    https://github.com/adafruit/Adafruit_SSD1306

    or

    http://download.csdn.net/detail/ling3ye/9729179

    Adafruit GFX Library:

    https://github.com/adafruit/Adafruit-GFX-Library

    or

    http://download.csdn.net/detail/ling3ye/9729180

    下载后把解压的文件放在 Arduino 安装目录里的 "libraries"

    例如:C:Program Files (x86)Arduinolibraries

    调出示例程序

    或者复制以下代码,都是一样的:

    [objc] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /********************************************************************* 
    2. This is an example for our Monochrome OLEDs based on SSD1306 drivers 
    3.  
    4.   Pick one up today in the adafruit shop! 
    5.   ------> http://www.adafruit.com/category/63_98 
    6.  
    7. This example is for a 128x64 size display using I2C to communicate 
    8. 3 pins are required to interface (2 I2C and one reset) 
    9.  
    10. Adafruit invests time and resources providing this open source code,  
    11. please support Adafruit and open-source hardware by purchasing  
    12. products from Adafruit! 
    13.  
    14. Written by Limor Fried/Ladyada  for Adafruit Industries.   
    15. BSD license, check license.txt for more information 
    16. All text above, and the splash screen must be included in any redistribution 
    17. *********************************************************************/  
    18.   
    19. #include <SPI.h>  
    20. #include <Wire.h>  
    21. #include <Adafruit_GFX.h>  
    22. #include <Adafruit_SSD1306.h>  
    23.   
    24. #define OLED_RESET 4  
    25. Adafruit_SSD1306 display(OLED_RESET);  
    26.   
    27. #define NUMFLAKES 10  
    28. #define XPOS 0  
    29. #define YPOS 1  
    30. #define DELTAY 2  
    31.   
    32.   
    33. #define LOGO16_GLCD_HEIGHT 16   
    34. #define LOGO16_GLCD_WIDTH  16   
    35. static const unsigned char PROGMEM logo16_glcd_bmp[] =  
    36. { B00000000, B11000000,  
    37.   B00000001, B11000000,  
    38.   B00000001, B11000000,  
    39.   B00000011, B11100000,  
    40.   B11110011, B11100000,  
    41.   B11111110, B11111000,  
    42.   B01111110, B11111111,  
    43.   B00110011, B10011111,  
    44.   B00011111, B11111100,  
    45.   B00001101, B01110000,  
    46.   B00011011, B10100000,  
    47.   B00111111, B11100000,  
    48.   B00111111, B11110000,  
    49.   B01111100, B11110000,  
    50.   B01110000, B01110000,  
    51.   B00000000, B00110000 };  
    52.   
    53. #if (SSD1306_LCDHEIGHT != 64)  
    54. #error("Height incorrect, please fix Adafruit_SSD1306.h!");  
    55. #endif  
    56.   
    57. void setup()   {                  
    58.   Serial.begin(9600);  
    59.   
    60.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)  
    61.   display.begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D (for the 128x64)  
    62.   // init done  
    63.     
    64.   // Show image buffer on the display hardware.  
    65.   // Since the buffer is intialized with an Adafruit splashscreen  
    66.   // internally, this will display the splashscreen.  
    67.   display.display();  
    68.   delay(2000);  
    69.   
    70.   // Clear the buffer.  
    71.   display.clearDisplay();  
    72.   
    73.   // draw a single pixel  
    74.   display.drawPixel(10, 10, WHITE);  
    75.   // Show the display buffer on the hardware.  
    76.   // NOTE: You _must_ call display after making any drawing commands  
    77.   // to make them visible on the display hardware!  
    78.   display.display();  
    79.   delay(2000);  
    80.   display.clearDisplay();  
    81.   
    82.   // draw many lines  
    83.   testdrawline();  
    84.   display.display();  
    85.   delay(2000);  
    86.   display.clearDisplay();  
    87.   
    88.   // draw rectangles  
    89.   testdrawrect();  
    90.   display.display();  
    91.   delay(2000);  
    92.   display.clearDisplay();  
    93.   
    94.   // draw multiple rectangles  
    95.   testfillrect();  
    96.   display.display();  
    97.   delay(2000);  
    98.   display.clearDisplay();  
    99.   
    100.   // draw mulitple circles  
    101.   testdrawcircle();  
    102.   display.display();  
    103.   delay(2000);  
    104.   display.clearDisplay();  
    105.   
    106.   // draw a white circle, 10 pixel radius  
    107.   display.fillCircle(display.width()/2, display.height()/2, 10, WHITE);  
    108.   display.display();  
    109.   delay(2000);  
    110.   display.clearDisplay();  
    111.   
    112.   testdrawroundrect();  
    113.   delay(2000);  
    114.   display.clearDisplay();  
    115.   
    116.   testfillroundrect();  
    117.   delay(2000);  
    118.   display.clearDisplay();  
    119.   
    120.   testdrawtriangle();  
    121.   delay(2000);  
    122.   display.clearDisplay();  
    123.      
    124.   testfilltriangle();  
    125.   delay(2000);  
    126.   display.clearDisplay();  
    127.   
    128.   // draw the first ~12 characters in the font  
    129.   testdrawchar();  
    130.   display.display();  
    131.   delay(2000);  
    132.   display.clearDisplay();  
    133.   
    134.   // draw scrolling text  
    135.   testscrolltext();  
    136.   delay(2000);  
    137.   display.clearDisplay();  
    138.   
    139.   // text display tests  
    140.   display.setTextSize(1);  
    141.   display.setTextColor(WHITE);  
    142.   display.setCursor(0,0);  
    143.   display.println("Hello, world!");  
    144.   display.setTextColor(BLACK, WHITE); // 'inverted' text  
    145.   display.println(3.141592);  
    146.   display.setTextSize(2);  
    147.   display.setTextColor(WHITE);  
    148.   display.print("0x"); display.println(0xDEADBEEF, HEX);  
    149.   display.display();  
    150.   delay(2000);  
    151.   display.clearDisplay();  
    152.   
    153.   // miniature bitmap display  
    154.   display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);  
    155.   display.display();  
    156.   delay(1);  
    157.   
    158.   // invert the display  
    159.   display.invertDisplay(true);  
    160.   delay(1000);   
    161.   display.invertDisplay(false);  
    162.   delay(1000);   
    163.   display.clearDisplay();  
    164.   
    165.   // draw a bitmap icon and 'animate' movement  
    166.   testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);  
    167. }  
    168.   
    169.   
    170. void loop() {  
    171.     
    172. }  
    173.   
    174.   
    175. void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {  
    176.   uint8_t icons[NUMFLAKES][3];  
    177.    
    178.   // initialize  
    179.   for (uint8_t f=0; f< NUMFLAKES; f++) {  
    180.     icons[f][XPOS] = random(display.width());  
    181.     icons[f][YPOS] = 0;  
    182.     icons[f][DELTAY] = random(5) + 1;  
    183.       
    184.     Serial.print("x: ");  
    185.     Serial.print(icons[f][XPOS], DEC);  
    186.     Serial.print(" y: ");  
    187.     Serial.print(icons[f][YPOS], DEC);  
    188.     Serial.print(" dy: ");  
    189.     Serial.println(icons[f][DELTAY], DEC);  
    190.   }  
    191.   
    192.   while (1) {  
    193.     // draw each icon  
    194.     for (uint8_t f=0; f< NUMFLAKES; f++) {  
    195.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);  
    196.     }  
    197.     display.display();  
    198.     delay(200);  
    199.       
    200.     // then erase it + move it  
    201.     for (uint8_t f=0; f< NUMFLAKES; f++) {  
    202.       display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);  
    203.       // move it  
    204.       icons[f][YPOS] += icons[f][DELTAY];  
    205.       // if its gone, reinit  
    206.       if (icons[f][YPOS] > display.height()) {  
    207.         icons[f][XPOS] = random(display.width());  
    208.         icons[f][YPOS] = 0;  
    209.         icons[f][DELTAY] = random(5) + 1;  
    210.       }  
    211.     }  
    212.    }  
    213. }  
    214.   
    215.   
    216. void testdrawchar(void) {  
    217.   display.setTextSize(1);  
    218.   display.setTextColor(WHITE);  
    219.   display.setCursor(0,0);  
    220.   
    221.   for (uint8_t i=0; i < 168; i++) {  
    222.     if (i == ' ') continue;  
    223.     display.write(i);  
    224.     if ((i > 0) && (i % 21 == 0))  
    225.       display.println();  
    226.   }      
    227.   display.display();  
    228.   delay(1);  
    229. }  
    230.   
    231. void testdrawcircle(void) {  
    232.   for (int16_t i=0; i<display.height(); i+=2) {  
    233.     display.drawCircle(display.width()/2, display.height()/2, i, WHITE);  
    234.     display.display();  
    235.     delay(1);  
    236.   }  
    237. }  
    238.   
    239. void testfillrect(void) {  
    240.   uint8_t color = 1;  
    241.   for (int16_t i=0; i<display.height()/2; i+=3) {  
    242.     // alternate colors  
    243.     display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);  
    244.     display.display();  
    245.     delay(1);  
    246.     color++;  
    247.   }  
    248. }  
    249.   
    250. void testdrawtriangle(void) {  
    251.   for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5) {  
    252.     display.drawTriangle(display.width()/2, display.height()/2-i,  
    253.                      display.width()/2-i, display.height()/2+i,  
    254.                      display.width()/2+i, display.height()/2+i, WHITE);  
    255.     display.display();  
    256.     delay(1);  
    257.   }  
    258. }  
    259.   
    260. void testfilltriangle(void) {  
    261.   uint8_t color = WHITE;  
    262.   for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5) {  
    263.     display.fillTriangle(display.width()/2, display.height()/2-i,  
    264.                      display.width()/2-i, display.height()/2+i,  
    265.                      display.width()/2+i, display.height()/2+i, WHITE);  
    266.     if (color == WHITE) color = BLACK;  
    267.     else color = WHITE;  
    268.     display.display();  
    269.     delay(1);  
    270.   }  
    271. }  
    272.   
    273. void testdrawroundrect(void) {  
    274.   for (int16_t i=0; i<display.height()/2-2; i+=2) {  
    275.     display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, WHITE);  
    276.     display.display();  
    277.     delay(1);  
    278.   }  
    279. }  
    280.   
    281. void testfillroundrect(void) {  
    282.   uint8_t color = WHITE;  
    283.   for (int16_t i=0; i<display.height()/2-2; i+=2) {  
    284.     display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);  
    285.     if (color == WHITE) color = BLACK;  
    286.     else color = WHITE;  
    287.     display.display();  
    288.     delay(1);  
    289.   }  
    290. }  
    291.      
    292. void testdrawrect(void) {  
    293.   for (int16_t i=0; i<display.height()/2; i+=2) {  
    294.     display.drawRect(i, i, display.width()-2*i, display.height()-2*i, WHITE);  
    295.     display.display();  
    296.     delay(1);  
    297.   }  
    298. }  
    299.   
    300. void testdrawline() {    
    301.   for (int16_t i=0; i<display.width(); i+=4) {  
    302.     display.drawLine(0, 0, i, display.height()-1, WHITE);  
    303.     display.display();  
    304.     delay(1);  
    305.   }  
    306.   for (int16_t i=0; i<display.height(); i+=4) {  
    307.     display.drawLine(0, 0, display.width()-1, i, WHITE);  
    308.     display.display();  
    309.     delay(1);  
    310.   }  
    311.   delay(250);  
    312.     
    313.   display.clearDisplay();  
    314.   for (int16_t i=0; i<display.width(); i+=4) {  
    315.     display.drawLine(0, display.height()-1, i, 0, WHITE);  
    316.     display.display();  
    317.     delay(1);  
    318.   }  
    319.   for (int16_t i=display.height()-1; i>=0; i-=4) {  
    320.     display.drawLine(0, display.height()-1, display.width()-1, i, WHITE);  
    321.     display.display();  
    322.     delay(1);  
    323.   }  
    324.   delay(250);  
    325.     
    326.   display.clearDisplay();  
    327.   for (int16_t i=display.width()-1; i>=0; i-=4) {  
    328.     display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE);  
    329.     display.display();  
    330.     delay(1);  
    331.   }  
    332.   for (int16_t i=display.height()-1; i>=0; i-=4) {  
    333.     display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE);  
    334.     display.display();  
    335.     delay(1);  
    336.   }  
    337.   delay(250);  
    338.   
    339.   display.clearDisplay();  
    340.   for (int16_t i=0; i<display.height(); i+=4) {  
    341.     display.drawLine(display.width()-1, 0, 0, i, WHITE);  
    342.     display.display();  
    343.     delay(1);  
    344.   }  
    345.   for (int16_t i=0; i<display.width(); i+=4) {  
    346.     display.drawLine(display.width()-1, 0, i, display.height()-1, WHITE);   
    347.     display.display();  
    348.     delay(1);  
    349.   }  
    350.   delay(250);  
    351. }  
    352.   
    353. void testscrolltext(void) {  
    354.   display.setTextSize(2);  
    355.   display.setTextColor(WHITE);  
    356.   display.setCursor(10,0);  
    357.   display.clearDisplay();  
    358.   display.println("scroll");  
    359.   display.display();  
    360.   delay(1);  
    361.    
    362.   display.startscrollright(0x00, 0x0F);  
    363.   delay(2000);  
    364.   display.stopscroll();  
    365.   delay(1000);  
    366.   display.startscrollleft(0x00, 0x0F);  
    367.   delay(2000);  
    368.   display.stopscroll();  
    369.   delay(1000);      
    370.   display.startscrolldiagright(0x00, 0x07);  
    371.   delay(2000);  
    372.   display.startscrolldiagleft(0x00, 0x07);  
    373.   delay(2000);  
    374.   display.stopscroll();  
    375. }  


     
  • 相关阅读:
    Mycat适合场景及不适合场景
    solr与Elasticsearch对比
    分布式搜索之搭建Solrcloud(Solr集群)
    Mysql索引最左匹配原则
    CAS实现单点登录SSO执行原理及部署
    Spring Cloud,Dubbo及HSF对比
    Dubbo支持的协议的详解
    Dubbo架构设计详解
    几种分布式锁的实现方式
    深入分析volatile的实现原理
  • 原文地址:https://www.cnblogs.com/edan/p/6817157.html
Copyright © 2011-2022 走看看