【雕爷学编程】Arduino动手做(187)---1.3寸OLED液晶屏模块4

37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试多做实验,不管成功与否,都会记录下来——小小的进步或是搞不掂的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验一百八十七:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 128*64像素

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百八十一:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 132*64像素
项目之五:I2C 检测器 - 使用 BitBang_I2C 库扫描和识别 I2C 总线上的设备

实验开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验一百八十一:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 132*64像素
  项目之五:I2C 检测器 - 使用 BitBang_I2C 库扫描和识别 I2C 总线上的设备
  实验接线:
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------D6
  SCL ------- D4
*/

// I2C Detector - scan and identify devices on an I2C bus using the BitBang_I2C library
// 
// The purpose of this code is to provide a sample sketch which can serve
// to detect not only the addresses of I2C devices, but what type of device each one is.
// So far, I've added the 25 devices I've personally used or found to be reliably detected
// based on their register contents. I encourage people to do pull requests to add support
// for more devices to make this code have wider appeal.

// There are plenty of I2C devices which appear at fixed addresses, yet don't have unique
// "Who_Am_I" registers or other data to reliably identify them. It's certainly possible to
// write code which initializes these devices and tries to verify their identity. This can
// potentially damage them and would necessarily increase the code size. I would like to keep
// the size of this code small enough so that it can be included in many microcontroller 
// projects where code space is scarce.

// Copyright (c) 2019 BitBank Software, Inc.
// Written by Larry Bank
// email: [email protected]
// Project started 25/02/2019
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Uses my Bit Bang I2C library. You can find it here:
// https://github.com/bitbank2/BitBang_I2C
#include <BitBang_I2C.h>

// Arbitrary pins I used for testing with an ATmega328p
// Define as -1, -1 to use the Wire library over the default I2C interface
#define SDA_PIN A4
#define SCL_PIN A5
// M5Stack Atom Grove connector pin assignments
//#define SDA_PIN 32 
//#define SCL_PIN 26
// M5Stack Atom internal I2C connected to the IMU
//#define SDA_PIN 25
//#define SCL_PIN 21
// M5Stack Core2 internal I2C
#define SDA_PIN 8
#define SCL_PIN 9
//
// If you don't need the explicit device names displayed, disable this code by
// commenting out the next line
//
#define SHOW_NAME
#ifdef SHOW_NAME
const char *szNames[]  = {
    
    "Unknown","SSD1306","SH1106","VL53L0X","BMP180", "BMP280","BME280",
                "MPU-60x0", "MPU-9250", "MCP9808","LSM6DS3", "ADXL345", "ADS1115","MAX44009",
                "MAG3110", "CCS811", "HTS221", "LPS25H", "LSM9DS1","LM8330", "DS3231", "LIS3DH",
                "LIS3DSH","INA219","SHT3X","HDC1080","MPU6886","BME680", "AXP202", "AXP192", "24AA02XEXX", 
                "DS1307", "MPU688X", "FT6236G", "FT6336G", "FT6336U", "FT6436", "BM8563","BNO055"};
#endif

BBI2C bbi2c;

void setup() {
    
    
  Serial.begin(115200);
  memset(&bbi2c, 0, sizeof(bbi2c));
  bbi2c.bWire = 0; // use bit bang, not wire library
  bbi2c.iSDA = SDA_PIN;
  bbi2c.iSCL = SCL_PIN;
  I2CInit(&bbi2c, 100000L);
  delay(100); // allow devices to power up
}

void loop() {
    
    
uint8_t map[16];
uint8_t i;
int iDevice, iCount;

  Serial.println("Starting I2C Scan");
  I2CScan(&bbi2c, map); // get bitmap of connected I2C devices
  if (map[0] == 0xfe) // something is wrong with the I2C bus
  {
    
    
    Serial.println("I2C pins are not correct or the bus is being pulled low by a bad device; unable to run scan");
  }
  else
  {
    
    
    iCount = 0;
    for (i=1; i<128; i++) // skip address 0 (general call address) since more than 1 device can respond
    {
    
    
      if (map[i>>3] & (1 << (i & 7))) // device found
      {
    
    
        iCount++;
        Serial.print("Device found at 0x");
        Serial.print(i, HEX);
        iDevice = I2CDiscoverDevice(&bbi2c, i);
        Serial.print(", type = ");
  #ifdef SHOW_NAME
        Serial.println(szNames[iDevice]); // show the device name as a string
  #else
        Serial.println(iDevice); // show the device name as the enum index
  #endif
      }
    } // for i
    Serial.print(iCount, DEC);
    Serial.println(" device(s) found");
  }
  delay(5000);
}

实验串口返回情况

在这里插入图片描述

【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百八十一:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 132*64像素
项目之六:动画:一匹走路的狼

Arduino实验场景图

在这里插入图片描述

实验场景图 动态图

在这里插入图片描述
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百八十一:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 132*64像素
项目之七:显示文字与图形
实验开源代码

/*
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验一百八十一:1.3寸OLED液晶屏 I2C IIC通信 4针模块 1106/1306驱动 132*64像素
  项目之七:显示文字与图形
  实验接线:
  oled模块    Ardunio Uno
  GND---------GND接地线
  VCC---------5V 接电源
  SDA---------A4
  SCL ------- A5
*/

#include <ss_oled.h>

// Use -1 for the Wire library default pins
// or specify the pin numbers to use with the Wire library or bit banging on any GPIO pins
// These are reversed because I did straight-through wiring for my SSD1306
// and it has the 4-pin header as GND,VCC,SCL,SDA, but the GROVE connector is
// GND,VCC,SDA,SCL
#define GROVE_SDA_PIN A4
#define GROVE_SCL_PIN A5
// Set this to -1 to disable or the GPIO pin number connected to the reset
// line of your display if it requires an external reset
#define RESET_PIN -1
// let ss_oled figure out the display address
#define OLED_ADDR -1
// don't rotate the display
#define FLIP180 0
// don't invert the display
#define INVERT 0
// Bit-Bang the I2C bus
#define USE_HW_I2C 1

// Change this if you're using different OLED displays
#define MY_OLED OLED_128x64
uint8_t ucBackBuffer[1024];

// The SSOLED structure. Each structure is about 56 bytes
// There is no limit to the number of simultaneous displays which can be controlled by ss_oled 
SSOLED ssoled;

void setup() {
    
    
char *msgs[] = {
    
    (char *)"SSD1306 @ 0x3C", (char *)"SSD1306 @ 0x3D",(char *)"SH1106 @ 0x3C",(char *)"SH1106 @ 0x3D"};
int rc;
// The I2C SDA/SCL pins set to -1 means to use the default Wire library
// If pins were specified, they would be bit-banged in software
// This isn't inferior to hw I2C and in fact allows you to go faster on certain CPUs
// The reset pin is optional and I've only seen it needed on larger OLEDs (2.4")
//    that can be configured as either SPI or I2C
//
// oledInit(SSOLED *, type, oled_addr, rotate180, invert, bWire, SDA_PIN, SCL_PIN, RESET_PIN, speed)

rc = oledInit(&ssoled, MY_OLED, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, GROVE_SDA_PIN, GROVE_SCL_PIN, RESET_PIN, 800000L); // use standard I2C bus at 400Khz
  if (rc != OLED_NOT_FOUND)
  {
    
    
    oledFill(&ssoled, 0, 1);
    oledWriteString(&ssoled, 0,0,0,msgs[rc], FONT_NORMAL, 0, 1);
    delay(2000);
  }
  else
  {
    
    
    while (1) {
    
    };
  }
  oledSetBackBuffer(&ssoled, ucBackBuffer);
} /* setup() */

#define DRAW_ELLIPSES
#define DRAW_RECTS

void loop() {
    
    
  int i, x, y, x2, y2, r1, r2;
  uint8_t ucColor;

#ifdef DRAW_ELLIPSES
  oledFill(&ssoled, 0, 1);
  oledWriteString(&ssoled, 0, 0, 0, (char *)"Ellipses", FONT_NORMAL, 0, 1);
  delay(2000);
  oledFill(&ssoled, 0, 1);
  for (i=0; i<100; i++)
  {
    
    
    x = random(128);
    y = random(64);
    r1 = random(64);
    r2 = random(32);
    oledEllipse(&ssoled, x, y, r1, r2, 1, 0);
    oledDumpBuffer(&ssoled, NULL);    
  }
  oledFill(&ssoled, 0, 1);
  oledWriteString(&ssoled, 0, 0, 0, (char *)"Filled Ellipses", FONT_NORMAL, 0, 1);
  delay(2000);
  oledFill(&ssoled, 0, 1);
  for (i=0; i<100; i++)
  {
    
    
    x = random(128);
    y = random(64);
    r1 = random(64);
    r2 = random(32);
    ucColor = random(2);
    oledEllipse(&ssoled, x, y, r1, r2, ucColor, 1);
    oledDumpBuffer(&ssoled, NULL);
  }
#endif // DRAW_ELLIPSES

#ifdef DRAW_RECTS
  oledFill(&ssoled, 0, 1);
  oledWriteString(&ssoled, 0, 0, 0, (char *)"Rectangles", FONT_NORMAL, 0, 1);
  delay(2000);
  oledFill(&ssoled, 0, 1);
  for (i=0; i<100; i++)
  {
    
    
    x = random(128);
    y = random(64);
    x2 = random(128);
    y2 = random(64);
    oledRectangle(&ssoled, x, y, x2, y2, 1, 0);
    oledDumpBuffer(&ssoled, NULL);
  }
  oledFill(&ssoled, 0, 1);
  oledWriteString(&ssoled, 0, 0, 0, (char *)"Filled Rects", FONT_NORMAL, 0, 1);
  delay(2000);
  oledFill(&ssoled, 0, 1);
  for (i=0; i<100; i++)
  {
    
    
    x = random(128);
    y = random(64);
    x2 = random(128);
    y2 = random(64);
    ucColor = random(2);
    oledRectangle(&ssoled, x, y, x2, y2, ucColor, 1);
    oledDumpBuffer(&ssoled, NULL);
  }
#endif // DRAW_RECTS

  delay(4000);
} /* loop() */

猜你喜欢

转载自blog.csdn.net/weixin_41659040/article/details/132129459