鸿蒙之OLED显示

270 阅读3分钟

「这是我参与2022首次更文挑战的第2天,活动详情查看:2022首次更文挑战」。

介绍

HarmonyOS是一款面向万物互联时代的、全新的分布式操作系统。

在传统的单设备系统能力基础上,HarmonyOS提出了基于同一套系统能力、适配多种终端形态的分布式理念,能够支持手机、平板、智能穿戴、智慧屏、车机等多种终端设备,提供全场景(移动办公、运动健康、社交通信、媒体娱乐等)业务能力。

前期

首先在
└── applications
└── sample
└── wifi-iot
└── app
在app这个文件夹下建立一个oled文件夹

设置I2C引脚复用
确定i2c引脚,查看原理图,可以看到OLED屏幕使用到的是I2C0,引脚是GPIO13、GPIO14
所以我们需要修改源码,在vendor hisi hi3861 hi3861 app wifiiot_appinit app_io_init.c 文件中,初始化I2C引脚的代码修改成如下:

#ifdef CONFIG_I2C_SUPPORT

    /* I2C IO复用也可以选择3/4; 9/10,根据产品设计选择 */

    hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);

    hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);

#endif
  1. 开启I2C功能
    修改文件:vendor hisi hi3861 hi3861 buildconfig usr_config.mk
    增加 CONFIG_I2C_SUPPORT=y

具体设计

我们都知道,正常的OLED显示开发,我们普遍需要建立三个文件。分别是oled.c、oled.h、oled_font.h
最后一个oled_font.h用来存放我们取模后的程序。
前两个是我们主要实现功能的程序。

关键程序

oled.h

#ifndef __OLED_H#define __OLED_H#define OLED_MODE 0#define SIZE 8#define XLevelL     0x00#define XLevelH     0x10#define Max_Column  128#define Max_Row     64#define Brightness  0xFF #define X_WIDTH     128#define Y_WIDTH     64      #define OLED_CMD  0 //写命令#define OLED_DATA 1 //写数据#define u8 unsigned char#define u16 unsigned short#define u32 unsigned int#endif

oled.c

void led_init(void){    OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel    OLED_WR_Byte(0x00,OLED_CMD);//---set low column address    OLED_WR_Byte(0x10,OLED_CMD);//---set high column address    OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)    OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register    OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness    OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常    OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常    OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display    OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)    OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty    OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset   Shift Mapping RAM Counter (0x00~0x3F)    OLED_WR_Byte(0x00,OLED_CMD);//-not offset    OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency    OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec    OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period    OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock    OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration    OLED_WR_Byte(0x12,OLED_CMD);    OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh    OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level    OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)    OLED_WR_Byte(0x02,OLED_CMD);//    OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable    OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable    OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)    OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)     OLED_WR_Byte(0xAF,OLED_CMD);    OLED_Clear();}void my_oled_demo(void){    int h=50;    //初始化    hi_i2c_init(HI_I2C_IDX_0, 100000); /* baudrate: 100000 */    led_init();    OLED_ColorTurn(0);//0正常显示,1 反色显示    OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示    OLED_ShowString(8,16,"hello world",16);    OLED_ShowNum(18,30,h,2,16);    OLED_Refresh();}

实现效果

===