6.I2C 驱动OLED屏幕测试
该教程演示如何通过开发板的I2C 驱动OLED屏幕
一、准备工作
- 准备1块BearPi-Pico H3863开发板
- 准备1块 I2C接口的128*64分辨率的OLED屏幕
二、编译代码
在Windows下编译操作
点击工具栏中的“系统配置”,打开配置界面.
选择Support I2C Sample.
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [*] Support I2C Sample. I2C Sample Configuration --->
设置I2C主设备的I2C id和GPIO,id设置为1,GPIO设置为15和16。
I2C Sample Configuration ---> [*] Support I2C Master Sample. (1) Choose I2C master bus id. (NEW) (15) Choose I2C SCL master pin. (NEW) (16) Choose I2C SDA master pin. (NEW)
按下"Save"键保存配置。
关键代码,位于
application\samples\peripheral\i2c\i2c_master_demo.c
static void *i2c_master_task(const char *arg) { unused(arg); uint32_t baudrate = I2C_SET_BAUDRATE; uint8_t hscode = I2C_MASTER_ADDR; /* I2C master init config. */ app_i2c_init_pin(); //设置引脚复用为I2C uapi_i2c_master_init(CONFIG_I2C_MASTER_BUS_ID, baudrate, hscode); //初始化I2C ..... return NULL; }
static uint32_t ssd1306_SendData(uint8_t *buffer, uint32_t size) { uint16_t dev_addr = I2C_SLAVE2_ADDR; i2c_data_t data = {0}; data.send_buf = buffer; data.send_len = size; uint32_t retval = uapi_i2c_master_write(CONFIG_I2C_MASTER_BUS_ID, dev_addr, &data); //发送I2C数据 if (retval != 0) { printf("I2cWrite(%02X) failed, %0X!\n", data.send_buf[1], retval); return retval; } return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Support I2C Sample.
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [*] Support I2C Sample. I2C Sample Configuration --->
设置I2C主设备的I2C id和GPIO,id设置为1,GPIO可设置为15和16。
I2C Sample Configuration ---> [*] Support I2C Master Sample. (1) Choose I2C master bus id. (NEW) (15) Choose I2C SCL master pin. (NEW) (16) Choose I2C SDA master pin. (NEW)
按下"ESC"键退出并保存配置。
关键代码,位于
application\samples\peripheral\i2c\i2c_master_demo.c
static void *i2c_master_task(const char *arg) { unused(arg); uint32_t baudrate = I2C_SET_BAUDRATE; uint8_t hscode = I2C_MASTER_ADDR; /* I2C master init config. */ app_i2c_init_pin(); //设置引脚复用为I2C uapi_i2c_master_init(CONFIG_I2C_MASTER_BUS_ID, baudrate, hscode); //初始化I2C ..... return NULL; }
static uint32_t ssd1306_SendData(uint8_t *buffer, uint32_t size) { uint16_t dev_addr = I2C_SLAVE2_ADDR; i2c_data_t data = {0}; data.send_buf = buffer; data.send_len = size; uint32_t retval = uapi_i2c_master_write(CONFIG_I2C_MASTER_BUS_ID, dev_addr, &data); //发送I2C数据 if (retval != 0) { printf("I2cWrite(%02X) failed, %0X!\n", data.send_buf[1], retval); return retval; } return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
按以下接线方式将开发板与屏幕连接,点击运行点亮OLED屏幕程序,观察屏幕显示。
开发板 ------------ 屏幕
TXD ------------ SDA
RXD ------------ SCL
3V3 ------------ VCC
GND ------------ GND
烧录固件后按下开发的复位按键,观察屏幕显示。