Arduino Nano 驱动OLED测试入门(一)(软IIC以及硬IIC链接SSD1306屏)

需要的准备工作

硬件

面包板一个, Arduino Nano板, SSD1306的OLED屏(本文采用I2C接口)一个,线材若干

在这里插入图片描述

库文件

u8glib ,U8g2 两个库,如果没有的话可以在CSDN里下载。

连接(硬IIC接口)
ArduinoOLED
3V3VCC
GNDGND
A5SCL
A4SDA
连接(软IIC接口)

如果I2C需要共用的情况下,U8g2的库可以支持用普通的IO做I2C接口驱动OLED,本文中使用D10做SCL接口,D11做SDA接口。

ArduinoOLED
3V3VCC
GNDGND
D10(可更改)SCL
D11(可更改)SDA
Hello word 测试
基于u8glib(硬IIC接口)

为了提高可阅读性,无关紧要的内容已删除。

  1. 添加库文件,打开arduino程序,执行下图1、2、3,找到下载的库文件。
    在这里插入图片描述
  2. 执行Demo程序
    打开Arduino程序,执行1、2、3、4
    在这里插入图片描述
    如果想继续测试显示结果,可复制下列代码,显示三行。
#include "U8glib.h"
/*I2C协议*/
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); 

void setup()
{
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) 
    u8g.setColorIndex(255);     // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3);         // max intensity
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);         // pixel on

  // u8g.setFont(u8g_font_unifont);
  Serial.begin(9600);

  u8g.setFont(u8g_font_osb18);   /*设置字体大小*/
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();
}

void loop()
{
  u8g.firstPage();  
  do {
    u8g.drawStr(0,0,"hello world!");
    u8g.drawStr(0,20,"ha ha!");
    u8g.drawStr(20,40,"well done!");  /*设置起始位置*/
  } while( u8g.nextPage() );
  delay(500);
}
基于U8g2(软IIC接口)

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

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 10, /* data=*/ 11, /* reset=*/ U8X8_PIN_NONE);   // 设置D10做SCL,D11做SDA,可以改为其它接口
//U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 16, /* data=*/ 17);   // 如果要用硬IIC接口,可以用这句


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

void loop(void) {
  u8g2.clearBuffer();					// clear the internal memory
  u8g2.setFont(u8g2_font_ncenB14_tr);	// 这里可以修改字体大小
  u8g2.drawStr(0,10,"Hello World!");	// write something to the internal memory
  u8g2.sendBuffer();					// transfer internal memory to the display
  delay(1000);  
}

需要了解更多请查阅相关手册。

  • 11
    点赞
  • 91
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Learning C for Arduino by Syed Omar Faruk Towaha English | 24 Mar. 2017 | ASIN: B01N7F91HN | 378 Pages | AZW3 | 19.45 MB Key Features Get hands-on experience with the Ardruino board and learn to control it with your programming skills Learn the essential concepts of C such as variables, data structures, functions, loops, and pointers Work with electronic devices such as LEDs, switches, and motors and connect them to Arduino using C Book Description This book will start with the fundamentals of C programming and programming topics, such data types, functions, decision making, program loops, pointers, and structures, with the help of an Arduino board. Then you will get acquainted with Arduino interactions with sensors, LEDs, and autonomous systems and setting up the Arduino environment. Moving on you will also learn how to work on the digital and analog I/O, establish serial communications with autonomous systems, and integrate with electronic devices. By the end of the book, you will be able to make basic projects such as LED cube and smart weather system that leverages C. What you will learn Play with mathematical operations using C Use logical operations and loops to play with LEDs and the Arduino board Create custom functions using C and connect an SD card to the Arduino Use Object-oriented Programming to connect a GSM module to the Arduino board Play with an LCD board and Servo using standard Arduino libraries Build projects using Arduino such as a LED cube, a smart weather system, and home security Identify and fix common errors on an Arduino board About the Author Syed Omar Faruk Towaha has degrees in physics and computer engineering. He is a technologist, tech speaker, and physics enthusiast from Shahjalal University of Science and Technology (SUST), Bangladesh. He has passion for programming, tech writing, and physics experiments. His recent books include Easy Circuits for Kids, Fundamentals of Ruby, How You Should Design Algorithms, and JavaScript Projects for Kids. He is an Oracle-certified professional developer who is currently involved with a number of projects that serve both physics and computer architecture. He is currently working as the CTO of an IT company. Table of Contents Getting Started Our First Program! Exploring C with Arduino IDE Blinking with Operations and Loops Functions and Files with Arduino Arduino and C++ Using Pointers and Structure Working with Arduino Libraries Lets Build Something Awesome Few Error Handlings

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值