该开发板自带了一片0.91寸OLED,分辨率为128×32,不过厂家好像没提供例程和芯片介绍,经查找资料,发现应该是SSD1306驱动的。
因此我们可以找到SSD1306的驱动程序,稍加修改以适配该显示屏。
由于我手上之前做过128×64的,因此只要稍加修改就可以了。
根据开发板原理图,先定义出OLED所用的IIC接口的管脚
#define SCL_PORT (PortD)
#define SCL_PIN (Pin00)
#define SDA_PORT (PortD)
#define SDA_PIN (Pin01)
只要用到的文件都复制一份,这样阅读代码更方便
在oled.h
定义如下内容
//-----------------OLED端口定义----------------
#define OLED_SCL_Clr() PORT_ResetBits(SCL_PORT,SCL_PIN)
#define OLED_SCL_Set() PORT_SetBits(SCL_PORT,SCL_PIN)
#define OLED_SDA_Clr() PORT_ResetBits(SDA_PORT,SDA_PIN)
#define OLED_SDA_Set() PORT_SetBits(SDA_PORT,SDA_PIN)
这样就对应上原来的库函数了。
接下来修改原来的SSD1315的初始化代码,来对应SSD1306的驱动器
//初始化
void OLED_Init(void)
{
OLED_WR_Byte(0xAE,OLED_CMD);//--display off
OLED_WR_Byte(0x40,OLED_CMD);//--setstart line address
OLED_WR_Byte(0xB0,OLED_CMD);//--setpage address
OLED_WR_Byte(0xC8,OLED_CMD);//Comscan direction
OLED_WR_Byte(0x81,OLED_CMD);// contract control
OLED_WR_Byte(0xFF,OLED_CMD);//--128
OLED_WR_Byte(0xA1,OLED_CMD);//setsegment remap
OLED_WR_Byte(0xA6,OLED_CMD);//--normal/ reverse
OLED_WR_Byte(0xA8,OLED_CMD);//--setmultiplex ratio(1 to 64)
OLED_WR_Byte(0x1F,OLED_CMD);//--1/32duty
OLED_WR_Byte(0xD3,OLED_CMD);//-setdisplay offset
OLED_WR_Byte(0x00,OLED_CMD);//
OLED_WR_Byte(0xD5,OLED_CMD);//setosc division
OLED_WR_Byte(0xf0,OLED_CMD);//
OLED_WR_Byte(0xD9,OLED_CMD);//SetPre-Charge Period
OLED_WR_Byte(0x22,OLED_CMD);//
OLED_WR_Byte(0xDA,OLED_CMD);//setcom pin configuartion
OLED_WR_Byte(0x02,OLED_CMD);//
OLED_WR_Byte(0xDB,OLED_CMD);//setVcomh
OLED_WR_Byte(0x49,OLED_CMD);//
OLED_WR_Byte(0x8D,OLED_CMD);//setcharge pump enable
OLED_WR_Byte(0x14,OLED_CMD);//
OLED_WR_Byte(0xAF,OLED_CMD);//--turnon oled panel
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
}
接下来完成PIN管脚的初始化配置
依葫芦画瓢,设置为输出开漏模式
void oled_pin_init(void)
{
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
stcPortInit.enPinMode = Pin_Mode_Out;
stcPortInit.enPinOType = Pin_OType_Od;
stcPortInit.enExInt = Enable;
stcPortInit.enPullUp = Enable;
PORT_Init(SCL_PORT, SCL_PIN, &stcPortInit);
PORT_Init(SDA_PORT, SDA_PIN, &stcPortInit);
}
然后差不多了,该包含的头文件包含进来后。编译,通过,ISP烧录,重启运行,OK
运行效果还是很不错的。
烧录上需要注意,开发板本身的串口竟然是不能用的,需要额外提供一个串口,然后用跳线连接到ISP接口。选择这个额外的串口进行烧录,同时复位按键上面的
J7跳线帽应短接,点执行后,按下复位键,即可完成烧录,重启需要去掉短接的跳线帽,然后按复位即可。有点小麻烦。
打包的完整工程,以供大家测试。
GPIO_LED.rar (453.54 KB)
开发板相关资料
HC32F460 EVB Introduction.pdf (932.55 KB) HC32F460系列的MCU开发工具用户手册Rev1.0.pdf (759.45 KB)
经过测试,在配置好GPIO作为开漏给IIC模拟使用的情况下,配置好后如果不延时给系统以响应时间,一次操作无法点亮屏幕,需要重启复位一次才行,因此需要在GPIO的初始化后就应该延时100ms,给IO电路以彻底的充放电反应时间。
完善后的OLED管脚硬件初始化函数如下
void oled_pin_init(void)
{
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
stcPortInit.enPinMode = Pin_Mode_Out;
stcPortInit.enPinOType = Pin_OType_Od;
stcPortInit.enExInt = Enable;
stcPortInit.enPullUp = Enable;
PORT_Init(SCL_PORT, SCL_PIN, &stcPortInit);
PORT_Init(SDA_PORT, SDA_PIN, &stcPortInit);
//配置好后,等待片刻,给硬件以准备时间。
Ddl_Delay1ms(100);
}
#include "hc32_ddl.h"
#include "oled.h"
/* LED0 Port/Pin definition */
#define LED0_PORT (PortE)
#define LED0_PIN (Pin06)
/* LED1 Port/Pin definition */
#define LED1_PORT (PortA)
#define LED1_PIN (Pin07)
/* LED2 Port/Pin definition */
#define LED2_PORT (PortB)
#define LED2_PIN (Pin05)
/* LED3 Port/Pin definition */
#define LED3_PORT (PortB)
#define LED3_PIN (Pin09)
/* LED0~3 toggle definition */
#define LED0_TOGGLE() (PORT_Toggle(LED0_PORT, LED0_PIN))
#define LED1_TOGGLE() (PORT_Toggle(LED1_PORT, LED1_PIN))
#define LED2_TOGGLE() (PORT_Toggle(LED2_PORT, LED2_PIN))
#define LED3_TOGGLE() (PORT_Toggle(LED3_PORT, LED3_PIN))
#define DLY_MS (100ul)
void led_init(void)
{
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
stcPortInit.enPinMode = Pin_Mode_Out;
stcPortInit.enExInt = Enable;
stcPortInit.enPullUp = Enable;
PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
PORT_Init(LED1_PORT, LED1_PIN, &stcPortInit);
PORT_Init(LED2_PORT, LED2_PIN, &stcPortInit);
PORT_Init(LED3_PORT, LED3_PIN, &stcPortInit);
}
void led_run(void)
{
LED0_TOGGLE();
Ddl_Delay1ms(DLY_MS);
LED1_TOGGLE();
Ddl_Delay1ms(DLY_MS);
LED2_TOGGLE();
Ddl_Delay1ms(DLY_MS);
LED3_TOGGLE();
Ddl_Delay1ms(DLY_MS);
}
void oled_pin_init(void)
{
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcPortInit);
stcPortInit.enPinMode = Pin_Mode_Out;
stcPortInit.enPinOType = Pin_OType_Od;
stcPortInit.enExInt = Enable;
stcPortInit.enPullUp = Enable;
PORT_Init(SCL_PORT, SCL_PIN, &stcPortInit);
PORT_Init(SDA_PORT, SDA_PIN, &stcPortInit);
//配置好后,等待片刻,给硬件以准备时间。
Ddl_Delay1ms(100);
}
int32_t main(void)
{
int i;
int num=0;
led_init();
oled_pin_init();
OLED_Init();//初始化OLED
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
for(i=0;i<8;i++)
OLED_ShowChinese(i*16,0,i,16); //第一个是列坐标,字符是16×16,第二个是行开始的坐标,单位是8。第三个是写入第几个字符的数组编号
for(i=0;i<5;i++)
OLED_ShowChinese(24+i*16,2,i+8,16); //第一个加24,为了居中对齐。
Ddl_Delay1ms(1500);
OLED_ShowString(0,2,"-21ic-HC32F460-",16);
Ddl_Delay1ms(1500);
OLED_ShowString(0,2,"HC32F460--0000--",16);
while(1)
{
led_run();
OLED_ShowNum(80,2,num++,4,16);
if(num>10000) num=0;
}
}
---------------------
作者:qjp1988113
链接:https://bbs.21ic.com/icview-3150820-1-1.html
来源:21ic.com
此文章已获得原创/原创奖标签,著作权归21ic所有,任何人未经允许禁止转载。