7.SPI 驱动OLED屏幕测试
该教程演示如何通过开发板的SPI 驱动OLED屏幕
一、准备工作
- 准备1块BearPi-Pico H3863开发板
- 准备1块 SPI接口的128*64分辨率的OLED屏幕
二、编译代码
在Windows下编译操作
点击工具栏中的“系统配置”,打开配置界面.
选择Support SPI Sample.
[*] 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. [ ] Support PINCTRL Sample. [ ] Support PWM Sample. [*] Support SPI Sample. SPI Sample Configuration --->
设置SPI主设备的SPI id和GPIO,id设置为0,GPIO引脚按以下设置。
SPI Sample Configuration ---> [*] Support SPI Master Sample. (0) Choose SPI master bus id. (NEW) (11) Choose SPI DI master pin. (NEW) (9) Choose SPI DO master pin. (NEW) (7) Choose SPI CLK master pin. (NEW) (8) Choose SPI CS master pin. (NEW)
按下"Save"键保存配置。
关键代码,位于
application\samples\peripheral\spi\spi_master_demo.c
static void *spi_master_task(const char *arg) { unused(arg); /* SPI pinmux. */ app_spi_init_pin(); //设置引脚复用为SPI /* SPI master init config. */ app_spi_master_init_config(); //初始化SPI ... return NULL; }
static uint32_t ssd1306_SendData(uint8_t *buffer, uint32_t size) { spi_xfer_data_t data = {0}; data.tx_buff = buffer; data.tx_bytes = size; uapi_gpio_set_val(CONFIG_SPI_CS_MASTER_PIN, GPIO_LEVEL_LOW); uint32_t ret = uapi_spi_master_write(CONFIG_SPI_MASTER_BUS_ID, &data, 0xFFFFFFFF); //发送SPI数据 if (ret != 0) { printf("spi = %0x master send failed\r\n", ret); return ret; } osal_mdelay(1); uapi_gpio_set_val(CONFIG_SPI_CS_MASTER_PIN, GPIO_LEVEL_HIGH); return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Support SPI Sample.
[*] 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. [ ] Support PINCTRL Sample. [ ] Support PWM Sample. [*] Support SPI Sample. SPI Sample Configuration --->
设置SPI主设备的SPI id和GPIO,id设置为0,GPIO引脚按以下设置。
SPI Sample Configuration ---> [*] Support SPI Master Sample. (0) Choose SPI master bus id. (NEW) (17) Choose SPI DI master pin. (NEW) (9) Choose SPI DO master pin. (NEW) (7) Choose SPI CLK master pin. (NEW) (8) Choose SPI CS master pin. (NEW)
按下"ESC"键退出并保存配置。
关键代码,位于
application\samples\peripheral\spi\spi_master_demo.c
static void *spi_master_task(const char *arg) { unused(arg); /* SPI pinmux. */ app_spi_init_pin(); //设置引脚复用为SPI /* SPI master init config. */ app_spi_master_init_config(); //初始化SPI ... return NULL; }
static uint32_t ssd1306_SendData(uint8_t *buffer, uint32_t size) { spi_xfer_data_t data = {0}; data.tx_buff = buffer; data.tx_bytes = size; uapi_gpio_set_val(CONFIG_SPI_CS_MASTER_PIN, GPIO_LEVEL_LOW); uint32_t ret = uapi_spi_master_write(CONFIG_SPI_MASTER_BUS_ID, &data, 0xFFFFFFFF); //发送SPI数据 if (ret != 0) { printf("spi = %0x master send failed\r\n", ret); return ret; } osal_mdelay(1); uapi_gpio_set_val(CONFIG_SPI_CS_MASTER_PIN, GPIO_LEVEL_HIGH); return 0; }
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
按以下接线方式将开发板与屏幕连接,点击运行点亮OLED屏幕程序,观察屏幕显示。
开发板 ------------ 屏幕
GPIO8 ------------ CS
GPIO10 ------------ DC
GPIO6 ------------ RES
GPIO9 ------------ D1
GPIO7 ------------ D0
3V3 ------------ VCC
GND ------------ GND
烧录固件后按下开发的复位按键,观察屏幕显示。