前言:
本次实验采用IIC接口的OLED屏幕实现,显示汉字和图片的实验,采用STM32RCT6型号单片机,PC12,PC11为模拟IIC模式驱动OLED.本次实验采用PcToLcd软件获取显示的值,本人采用的是:点阵格式-->阴码 取模方式-->列行式 取模走向-->逆向 输出选项-->紧凑型 格式-->C51格式
OLED部分驱动
此模块采用iic接口,iic的代码,我就不在编写了,不过要注意的是,IIC在发送数据后,额外给一个时钟,不处理应答信号,这是OLED的数据手册的规定
void IIC_SendByte(uint8_t bitdata)
{
for(uint8_t i=0;i<8;i++)
{
IIC_W_SDA(bitdata & (0x80>>i));
IIC_W_SCL(1);
IIC_W_SCL(0);
}
IIC_W_SCL(1); //在发送完毕后直接给一个额外时钟,不处理应答
IIC_W_SCL(0);
}
OLED写指令
void OLED_WriteCmd(uint8_t cmd)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_SendByte(0x00);
IIC_SendByte(cmd);
IIC_End();
}
OLED写数据
void OLED_WriteCData(uint8_t data)
{
IIC_Start();
IIC_SendByte(0x78);
IIC_SendByte(0x40);
IIC_SendByte(data);
IIC_End();
}
设置横纵坐标
void OLED_SetPos(uint8_t x,uint8_t y)
{
OLED_WriteCmd(0xB0 | y); //设置Y位置
OLED_WriteCmd(0x10 | ((x & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCmd(0x00 | (x & 0x0F));
}
1.显示8X16数字
显示代码
//字库代码
static uint8_t char8x16_1[] = {
0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00
};
/*
* x --> 横坐标值
* y --> 纵坐标值
* ch --> 字库数组
*/
void OLED_SetChar8x16(uint8_t x,uint8_t y,uint8_t *ch)//8*16
{
uint16_t i;
if(y==0)//判断Y是否为原点0
{
OLED_SetPos(x*8,y); //绘制高位
for(i=0;i<8;i++)
{
OLED_WriteCData(ch[i]);
}
OLED_SetPos(x*8,y+1); //绘制低位
for(i=8;i<16;i++)
{
OLED_WriteCData(ch[i]);
}
}
else //y不为原点
{
OLED_SetPos(x*8,y*2);
for(i=0;i<8;i++)
{
OLED_WriteCData(ch[i]);
}
OLED_SetPos(x*8,(y*2)+1);
for(i=8;i<16;i++)
{
OLED_WriteCData(ch[i]);
}
}
}
//主函数代码
int main(void)
{
OLED_Init();
while (1)
{
OLED_SetChar8x16(0,0,char8x16_1);
OLED_SetChar8x16(1,0,char8x16_1);
OLED_SetChar8x16(2,0,char8x16_1);
OLED_SetChar8x16(3,0,char8x16_1);
OLED_SetChar8x16(0,1,char8x16_1);
OLED_SetChar8x16(1,1,char8x16_1);
OLED_SetChar8x16(2,1,char8x16_1);
OLED_SetChar8x16(3,1,char8x16_1);
OLED_SetChar8x16(0,2,char8x16_1);
OLED_SetChar8x16(1,2,char8x16_1);
OLED_SetChar8x16(2,2,char8x16_1);
OLED_SetChar8x16(3,2,char8x16_1);
OLED_SetChar8x16(0,3,char8x16_1);
OLED_SetChar8x16(1,3,char8x16_1);
OLED_SetChar8x16(2,3,char8x16_1);
OLED_SetChar8x16(3,3,char8x16_1);
}
}
测试结果:
2.显示16X16汉字
显示代码:
//显示字库
static uint8_t char16x16_he[] = {
0x00,0x80,0x60,0xF8,0x07,0x04,0xE4,0x24,0x24,0xE4,0x04,0x04,0xFC,0x04,0x04,0x00,
0x01,0x00,0x00,0xFF,0x00,0x00,0x0F,0x04,0x04,0x0F,0x40,0x80,0x7F,0x00,0x00,0x00
};
static uint8_t char16x16_qiu[] = {
0x20,0x20,0x20,0xFF,0x20,0x20,0x00,0xFC,0x84,0x84,0x84,0x82,0x83,0x82,0x80,0x00,
0x10,0x30,0x10,0x0F,0x08,0x48,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x00
};
static uint8_t char16x16_hong[] = {
0x12,0x64,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,0x00,
0x04,0x7E,0x01,0x10,0x30,0x1F,0x08,0x08,0x00,0x13,0x12,0x12,0x52,0x82,0x7E,0x00
};
/*
* x --> 横坐标值
* y --> 纵坐标值
* ch --> 字库数组
*/
void OLED_SetChar16x16(uint8_t x,uint8_t y,uint8_t *ch)//16*16
{
uint16_t i;
if(y==0)
{
OLED_SetPos(x*16,y);
for(i=0;i<16;i++)
{
OLED_WriteCData(ch[i]);
}
OLED_SetPos(x*16,y+1);
for(i=16;i<32;i++)
{
OLED_WriteCData(ch[i]);
}
}
else
{
OLED_SetPos(x*16,y*2);
for(i=0;i<16;i++)
{
OLED_WriteCData(ch[i]);
}
OLED_SetPos(x*16,(y*2)+1);
for(i=16;i<32;i++)
{
OLED_WriteCData(ch[i]);
}
}
}
//主函数
int main(void)
{
OLED_Init();
while (1)
{
OLED_SetChar16x16(0,0,char16x16_he);
OLED_SetChar16x16(1,0,char16x16_qiu);
OLED_SetChar16x16(2,0,char16x16_hong);
OLED_SetChar16x16(0,1,char16x16_he);
OLED_SetChar16x16(1,1,char16x16_qiu);
OLED_SetChar16x16(2,1,char16x16_hong);
OLED_SetChar16x16(0,2,char16x16_he);
OLED_SetChar16x16(1,2,char16x16_qiu);
OLED_SetChar16x16(2,2,char16x16_hong);
OLED_SetChar16x16(0,3,char16x16_he);
OLED_SetChar16x16(1,3,char16x16_qiu);
OLED_SetChar16x16(2,3,char16x16_hong);
}
}
测试结果:
3.显示128X64位图
显示代码:
//图片数组值
static uint8_t bmp[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFE,0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xC0,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xE0,0xF8,
0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x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F,0x1F,0x3F,0x3E,0x7E,0x7C,0x7C,0xFC,0xF8,0xF8,0xF0,
0xF0,0xF0,0xE0,0xF0,0xF8,0xFE,0xFF,0xFF,0xFF,0x9F,0x8F,0x07,0x03,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x07,
0x0F,0x1F,0x7F,0xFF,0xFF,0xFF,0xFC,0xF8,0xF0,0xE0,0xF0,0xF0,0xF0,0xF8,0xF8,0xFC,
0x7C,0x7C,0x7E,0x3E,0x3F,0x1F,0x1F,0x1F,0x0F,0x0F,0x0F,0x07,0x07,0x03,0x03,0x03,
0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0xF1,
0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFC,
0xFC,0xF8,0xF8,0xF8,0xF8,0xF0,0xF0,0xE0,0xE0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x80,
0x80,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF8,0xF8,
0xF8,0xFC,0xFC,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF1,0xC1,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF8,0xFE,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x1F,0x3F,
0x3F,0x3F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,
0xF8,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0xC0,0xF0,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x1F,0x0F,0x0F,0x0F,0x07,0x07,0x07,0x03,
0x03,0x03,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x03,0x07,0x07,0x07,
0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x3F,0x3F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFC,0xF0,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
/*
* ch --> 图片数组值
*/
void OLED_SetBmp(uint8_t *ch)
{
u8 i,j;
for(i=0;i<8;i++)
{
OLED_SetPos(0,i);
for(j=0;j<128;j++)
{
OLED_WriteCData(ch[j+(i*128)]);
}
}
}
//主函数
int main(void)
{
OLED_Init();
while (1)
{
OLED_SetBmp(bmp);
}
}