查看: 4925|回复: 2

[经验] [Arduino STM32] 03:OLED 驱动

[复制链接]
  • TA的每日心情
    开心
    2021-12-10 15:56
  • 签到天数: 2675 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2016-11-20 16:25:36 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 slotg 于 2016-11-20 16:27 编辑

    并不是每一个 Arduino AVR 的程序都可以直接使用在 Arduino STM32 板上的,点完了 LCD 之后接下来也要试一下 OLED 的驱动,找了一个使用 Adafruit 库在 Arduino AVR 板上可以运行的例程直接在 STM32 下编译,结果却产生了错误!这貌似是库的支持不兼容,问题目前还在确认中,不过在 Arduino 官网论坛看到了这一篇帖子:

    http://forum.arduino.cc/index.php?topic=265904.2115

    有人改写了 Adafruit_SSD1306 库,让这个库可以在 Arduino STM32 底下运行,程序中对于 SPI 接口方式的 OLED 管脚是这么声明的:

    #define OLED_DC   22   //   D/C   6
    #define OLED_RST  21   //   RST   5
    #define OLED_MOSI 20   //   SDA   4
    #define OLED_CLK  19   //   SCL   3
    #define OLED_CS   14   //   ---   x Not Connected

    对应于 Arduino STM32 板的管脚为:

    22  ->  B6
    21  ->  B5
    20  ->  B4
    19  ->  B3
    14  ->  A14

    主程序:
    1. /*
    2.    Using Display: http://www.ebay.com/itm/301504841805
    3.    Compiled Arduino GUI 1.6.1 on 2015-04-01 by M. Ray Burnette
    4.      Sketch uses 27,912 bytes (25%) of program storage space. Maximum is 108,000 bytes.
    5.      Global variables use 5,616 bytes of dynamic memory.
    6.    THIS VERSION OF SSD1306 IS SOFTWARE SPI ONLY!!!
    7.    https://github.com/hwiguna/g33k/blob/master/ArduinoProjects/128x64_OLED/HariChord/HariChord.ino
    8. */

    9.   #include ".\Adafruit_GFX.h"
    10.   #include ".\Adafruit_SSD1306.h"

    11. // software SPI works on Maple Mini ARM 32-bit
    12.   // use these settings for the OLED display...
    13.   // These pin #'s are for Maple Mini/UNO/Nano/Mini328
    14.   //     __Signal__Maple_//__OLED 128x64___
    15.   #define OLED_DC   22   //   D/C   6
    16.   #define OLED_RST  21   //   RST   5
    17.   #define OLED_MOSI 20   //   SDA   4
    18.   #define OLED_CLK  19   //   SCL   3
    19.   #define OLED_CS   14   //   ---   x Not Connected
    20.   Adafruit_SSD1306 OLED(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);

    21. int nFrames = 36;

    22. void setup()   {
    23.   pinMode(17, OUTPUT);
    24.   pinMode(18, OUTPUT);
    25.   digitalWrite(17, LOW);
    26.   digitalWrite(18, HIGH);
    27.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    28.   OLED.begin(SSD1306_SWITCHCAPVCC);
    29.   // init done
    30.   
    31.   // Show image buffer on the display hardware.
    32.   // Since the buffer is intialized with an Adafruit splashscreen
    33.   // internally, this will display the splashscreen.
    34.   // NOTE: You _must_ call display after making any drawing commands
    35.   // to make them visible on the display hardware!
    36.   OLED.display();                // Adafruit Splash
    37.   OLED.clearDisplay();           // Clear the buffer.
    38.   // Y = 15 is the Blank Line between yellow / blue
    39. }

    40. void loop() {
    41.   for (int frame=0; frame < nFrames; frame++)
    42.   {
    43.     HariChord(frame);
    44.   }

    45.   for (int frame=(nFrames-1); frame >= 0; frame--)
    46.   {
    47.     HariChord(frame);
    48.   }
    49. }

    50. void HariChord(int frame)
    51. {
    52.   OLED.clearDisplay();
    53.   int n = 7;
    54.   int r = frame * 64 / nFrames;
    55.   float rot = frame * 2*PI / nFrames;
    56.   for (int i=0; i<(n-1); i++)
    57.   {
    58.     float a = rot + i * 2*PI / n;
    59.     int x1 = 64 + cos(a) * r;
    60.     int y1 = 32 + sin(a) * r;
    61.     for (int j=i+1; j<n; j++)
    62.     {
    63.       a = rot + j * 2*PI / n;
    64.       int x2 = 64 + cos(a) * r;
    65.       int y2 = 32 + sin(a) * r;
    66.       OLED.drawLine(x1,y1, x2,y2, WHITE);
    67.     }
    68.   }
    69.   OLED.display();
    70. }
    复制代码
    运行结果:
    20161117223121.gif

    包括库与例程的压缩文件:
    SSD1306-HariChord_swSPI.zip (15.93 KB, 下载次数: 28)
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2021-3-13 10:11
  • 签到天数: 1088 天

    连续签到: 2 天

    [LV.10]以坛为家III

    发表于 2016-11-21 09:27:09 | 显示全部楼层
    这个图像变化搞得很溜啊。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2021-12-10 15:56
  • 签到天数: 2675 天

    连续签到: 1 天

    [LV.Master]伴坛终老

     楼主| 发表于 2016-11-21 09:48:34 | 显示全部楼层
    yanhaijian 发表于 2016-11-21 09:27
    这个图像变化搞得很溜啊。

    32位的M3跑起来当然是比8位的AVR来的快。
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-19 06:52 , Processed in 0.124841 second(s), 18 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.