#申请原创# @21小跑堂
这块0.96的OLED是I2C接口的,控制芯片是SSD1315
卖家给的资料里有驱动的例程,接下来参考例程实现对这块屏幕的控制
首先定义一个设备的结构体,里面定义要用到的I2C控制指针和发送方法,这样做为的是方便移植用
typedef struct
{
void *iicx;
YUYY_IIC_MasterSendRegDatasFunc_Type iic_senddatas_func;
}YUYY_OLED_SSD1315_DEV_Type;
实现发送命令和数据的方法,原例程中一次I2C通讯只发送2字节的数据,感觉有点浪费
看一下SSD1315的手册,一次I2C通讯是可以发送2个字节以上的数据的
看一下控制位的描述
意思是co这位为0时控制字节后面就全是数据字节,这样就能得到下面的命令格式
发送命令数据:start addr+w 0x00 byte1 byte2 … byteN stop
发送显示数据:start addr+w 0x40 byte1 byte2 … byteN stop
按此实现发送方法
uint8_t YUYY_OLED_SSD1315_SendCmds(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t *cmds,uint8_t cmdlen)
{
uint8_t cbyte = 0x00;
if(!oled_ssd1315_dev->iic_senddatas_func)
return 1;
if(oled_ssd1315_dev->iic_senddatas_func(oled_ssd1315_dev->iicx,YUYY_OLED_SSD1315_IIC_ADDR,0x00,1,cmds,cmdlen) > 0)
return 2;
return 0;
}
uint8_t YUYY_OLED_SSD1315_SendDatas(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t *datas,uint8_t datalen)
{
uint8_t cbyte = 0x40;
if(!oled_ssd1315_dev->iic_senddatas_func)
return 1;
if(oled_ssd1315_dev->iic_senddatas_func(oled_ssd1315_dev->iicx,YUYY_OLED_SSD1315_IIC_ADDR,0x40,1,datas,datalen) > 0)
return 2;
return 0;
}
这样初始化时可以直接发送初始化的命令数组
const uint8_t oled_ssd1315_init_cmd[25]=
{
0xAE,//关闭显示
0xD5,//设置时钟分频因子,震荡频率
0x80, //[3:0],分频因子;[7:4],震荡频率
0xA8,//设置驱动路数
0X3F,//默认0X3F(1/64)
0xD3,//设置显示偏移
0X00,//默认为0
0x40,//设置显示开始行 [5:0],行数.
0x8D,//电荷泵设置
0x14,//bit2,开启/关闭
0x20,//设置内存地址模式
0x02,//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;
0xA1,//段重定义设置,bit0:0,0->0;1,0->127;
0xC8,//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数
0xDA,//设置COM硬件引脚配置
0x12,//[5:4]配置
0x81,//对比度设置
0xEF,//1~255;默认0X7F (亮度设置,越大越亮)
0xD9,//设置预充电周期
0xF1,//[3:0],PHASE 1;[7:4],PHASE 2;
0xDB,//设置VCOMH 电压倍率
0x30,//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
0xA4,//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
0xA6,//设置显示方式;bit0:1,反相显示;0,正常显示
0xAF,//开启显示
};
uint8_t YUYY_OLED_SSD1315_Init(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev)
{
uint8_t err = 0;
err = YUYY_OLED_SSD1315_SendCmds(oled_ssd1315_dev,oled_ssd1315_init_cmd,25);
return err;
}
写入显示数据时一次写入一行
uint8_t YUYY_OLED_SSD1315_SetPageCol(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t page,uint8_t col)
{
uint8_t cmds[3];
if(page < 8 && col < 128)
{
cmds[0] = 0xB0+page;
cmds[1] = 0x10|(col>>4);
cmds[2] = 0x00|(col&0x0f);
return YUYY_OLED_SSD1315_SendCmds(oled_ssd1315_dev,cmds,3);
}
return 1;
}
uint8_t YUYY_OLED_SSD1315_ClearScreen(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev)
{
uint8_t i=0,datas[128]={0};
while(i<8)
{
if(YUYY_OLED_SSD1315_SetPageCol(oled_ssd1315_dev,i,0))
return 1;
if(YUYY_OLED_SSD1315_SendDatas(oled_ssd1315_dev,datas,128))
return 1;
i += 1;
}
return 0;
}
uint8_t YUYY_OLED_SSD1315_TestScreen(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t d1,uint8_t d2)
{
uint8_t i=0,datas[128]={0};
while (i < 128)
{
datas[i] = d1;
datas[i+1] = d2;
i += 2;
}
i = 0;
while(i<8)
{
if(YUYY_OLED_SSD1315_SetPageCol(oled_ssd1315_dev,i,0))
return 1;
if(YUYY_OLED_SSD1315_SendDatas(oled_ssd1315_dev,datas,128))
return 1;
i += 1;
}
return 0;
}
void YUYY_OLED_SSD1315_CopyDatas(uint8_t *dst,uint8_t *src,uint8_t len,uint8_t rev)
{
uint8_t i = 0;
while (i < len)
{
if(rev)
dst[i] = ~src[i];
else
dst[i] = src[i];
i += 1;
}
}
void YUYY_OLED_SSD1315_DisplayString8x16(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t option,uint8_t page,uint8_t column,char *text)
{
uint8_t i=0,j = 0,rev = option&YUYY_FONT_DATAS_REVERSE;
uint8_t datas1[128] = {0},datas2[128] = {0};
while(text[i]>0x00 && j < 128)
{
if((text[i] > 0x1F)&&(text[i] < 0x7F))
{
YUYY_OLED_SSD1315_CopyDatas(&datas1[j],&YUYY_FONT_ASCII_TABLE_8x16[text[i]-0x20][0],8,rev);
YUYY_OLED_SSD1315_CopyDatas(&datas2[j],&YUYY_FONT_ASCII_TABLE_8x16[text[i]-0x20][8],8,rev);
}
j += 8;
i += 1;
}
if(YUYY_OLED_SSD1315_SetPageCol(oled_ssd1315_dev,page,column))
return;
if(YUYY_OLED_SSD1315_SendDatas(oled_ssd1315_dev,datas1,j))
return;
if(YUYY_OLED_SSD1315_SetPageCol(oled_ssd1315_dev,page+1,column))
return;
if(YUYY_OLED_SSD1315_SendDatas(oled_ssd1315_dev,datas2,j))
return;
}
void YUYY_OLED_SSD1315_DisplayString6x8(YUYY_OLED_SSD1315_DEV_Type *oled_ssd1315_dev,uint8_t option,uint8_t page,uint8_t column,char *text)
{
uint8_t i=0,j = 0,rev = option&YUYY_FONT_DATAS_REVERSE;
uint8_t datas[128] = {0};
while(text[i]>0x00 && j < 128)
{
if((text[i] > 0x1F)&&(text[i] < 0x7F))
{
if(j < 126)
YUYY_OLED_SSD1315_CopyDatas(&datas[j],YUYY_FONT_ASCII_TABLE_6x8[text[i]-0x20],6,rev);
else
YUYY_OLED_SSD1315_CopyDatas(&datas[j],YUYY_FONT_ASCII_TABLE_6x8[text[i]-0x20],2,rev);
}
j += 6;
i += 1;
}
if(j > 128)
j = 128;
if(YUYY_OLED_SSD1315_SetPageCol(oled_ssd1315_dev,page,column))
return;
if(YUYY_OLED_SSD1315_SendDatas(oled_ssd1315_dev,datas,j))
return;
}
先用软件I2C测试
YUYY_OLED_SSD1315_DEV_Type oled_dev;
void oled_init()
{
oled_dev.iicx = &siic_dev;
oled_dev.iic_senddatas_func = (YUYY_IIC_MasterSendRegDatasFunc_Type)YUYY_SOFTIIC_MasterSendRegDatas;
YUYY_OLED_SSD1315_Init(&oled_dev);
YUYY_OLED_SSD1315_ClearScreen(&oled_dev);
YUYY_OLED_SSD1315_DisplayString8x16(&oled_dev,0,0,0," AT32F423 TEST");
YUYY_OLED_SSD1315_DisplayString8x16(&oled_dev,0,2,0," I2C OLED");
YUYY_OLED_SSD1315_DisplayString8x16(&oled_dev,0,6,0,"Code by yuyy1989");
}
uint8_t testnum = 0;
void oled_test()
{
char out[20];
sprintf(out," Test Num:%02X",testnum);
YUYY_OLED_SSD1315_DisplayString8x16(&oled_dev,0,4,0,out);
testnum += 1;
}
效果
再用硬件I2C试试,只修改这里就行了
//oled_dev.iicx = &siic_dev;
//oled_dev.iic_senddatas_func = (YUYY_IIC_MasterSendRegDatasFunc_Type)YUYY_SOFTIIC_MasterSendRegDatas;
oled_dev.iicx = I2C1;
oled_dev.iic_senddatas_func = (YUYY_IIC_MasterSendRegDatasFunc_Type)YUYY_HARDIIC_MasterSendRegDatas;
YUYY_OLED_SSD1315_Init(&oled_dev);
效果
|
|