使用安信可小安派BW21-CBV-Kit开发板进行I2C设备扫描的,驱动OLED显示。
开发平台:Arduino IDE
开发板:小安派BW21-CBV-Kit
模块:OLED
功能:使用开发板实现OLED显示.
1、硬件连接确认
确保开发板与OLED模块的连接正确:
OLED_SCL → GPIO13 (I2C时钟线)
OLED_SDA → GPIO12 (I2C数据线)
共地连接→GND
电源→5V。
2、代码
#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屏幕初始化
使用IIC通信,地址为0x3C。SSD1306_SWITCHCAPVCC表示内部升压生成驱动电压。
其中16x16像素的二进制位图,每行2字节,MSB优先。
clearDisplay() → 绘制 → display() 避免闪烁,drawBitmap直接操作显存,比逐像素绘制高效,random()函数生成动态效果。
3.软件界面代码操作

注意:
当开发板连接电脑后,刚开始找不到端口,需要进行如下操作,按住左边boot按键不松手,再点击一下右边reset按键,1-2秒内可以观察到蓝灯逐渐亮起来,蓝灯亮起来红,此时松开左边boot按键,开发板进入烧录模式,不过有时按下BOOT后,再按RESET就可以了。

在这里选择开发板型号就可以进行上传了。

上传完成什么会出现End Upload Flash,按一下开发板的RESET就可以看到实际的现象了。
4、连接图

5、实现视频