【BW21-CBV-Kit 开发套件测评】OLED 显示
本文介绍了安信可 BW21-CBV-Kit 开发套件实现 OLED 屏幕显示的相关测试。
IIC 设备扫描
这里介绍了 IIC 设备扫描的代码和实现流程。
硬件连接
OLED_SCL -> 12
OLED_SDA -> 13
示意图

代码
打开例程 Examples - AmebaWire - I2CScanner
// --------------------------------------
// i2c_scanner
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
// ---------------------------------------
/*
Example guide:
https://www.amebaiot.com/en/amebapro2-arduino-i2c-scan/
*/
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
while (!Serial)
; // wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
byte x = 0;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 0; address < 127; address++) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
Wire.write(x);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
} else {
Serial.println("done\n");
}
delay(5000); // wait 2 seconds for next scan
}
按照示意图连接硬件,编译并上传固件,复位后运行程序;
打开串口监视器,每秒打印一次 IIC 设备扫描结果。
效果
串口输出连接的 IIC 设备地址

SSD1306显示
在前面关于 IIC 设备扫描的基础上,结合例程 Examples - AmebaWire - OLED_SSD1306
,通过 IIC 通信协议驱动基于 SSD1306 驱动器的 OLED 设备,实现图片和文字显示。
代码
参考例程 Examples - AmebaWire - OLED_SSD1306
,截取图片动态散落的部分。
#include <Wire.h>
#include <Adafruit_OLED_libraries/Adafruit_GFX.h>
#include <Adafruit_OLED_libraries/Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT_FLAKES 16
#define LOGO_WIDTH_FLAKES 16
static const unsigned char PROGMEM logo_bmp_flake[] =
{0b00000000, 0b11000000,
0b00000001, 0b11000000,
0b00000001, 0b11000000,
0b00000011, 0b11100000,
0b11110011, 0b11100000,
0b11111110, 0b11111000,
0b01111110, 0b11111111,
0b00110011, 0b10011111,
0b00011111, 0b11111100,
0b00001101, 0b01110000,
0b00011011, 0b10100000,
0b00111111, 0b11100000,
0b00111111, 0b11110000,
0b01111100, 0b11110000,
0b01110000, 0b01110000,
0b00000000, 0b00110000};
void setup()
{
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
delay(500);
testanimate(logo_bmp_flake, LOGO_WIDTH_FLAKES, LOGO_HEIGHT_FLAKES); // Animate bitmaps
}
void loop()
{
}
#define XPOS 0 // Indexes into the 'icons' array in function below
#define YPOS 1
#define DELTAY 2
void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h)
{
int8_t f, icons[NUMFLAKES][3];
// Initialize 'snowflake' positions
for (f = 0; f < NUMFLAKES; f++) {
icons[f][XPOS] = random(1 - LOGO_WIDTH_FLAKES, display.width());
icons[f][YPOS] = -LOGO_HEIGHT_FLAKES;
icons[f][DELTAY] = random(1, 6);
Serial.print(F("x: "));
Serial.print(icons[f][XPOS], DEC);
Serial.print(F(" y: "));
Serial.print(icons[f][YPOS], DEC);
Serial.print(F(" dy: "));
Serial.println(icons[f][DELTAY], DEC);
}
for (;;) { // Loop forever...
display.clearDisplay(); // Clear the display buffer
// Draw each snowflake:
for (f = 0; f < NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
}
display.display(); // Show the display buffer on the screen
delay(200); // Pause for 1/10 second
// Then update coordinates of each flake...
for (f = 0; f < NUMFLAKES; f++) {
icons[f][YPOS] += icons[f][DELTAY];
// If snowflake is off the bottom of the screen...
if (icons[f][YPOS] >= display.height()) {
// Reinitialize to a random position, just off the top
icons[f][XPOS] = random(1 - LOGO_WIDTH_FLAKES, display.width());
icons[f][YPOS] = -LOGO_HEIGHT_FLAKES;
icons[f][DELTAY] = random(1, 6);
}
}
}
}
编译并上传固件,实现花朵随机下落的效果。
OLED 取模网站:image2cpp .
效果

散落的星星

总结
本文介绍了安信可 BW21-CBV-Kit 开发套件实现 OLED 屏幕显示的相关测试,包括 IIC 设备扫描获取地址,SSD1306 驱动 OLED 设备的连接、工程测试(文字、图片、动画等),为小安派系列产品的数据显示和智能交互相关实验提供了参考。