1.OLED写命令
写命令/数据的代码
/
*
1. start()
2. 写入 b0111 1000 0x78
3. ACK
4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
5. ACK
6. 写入指令/数据
7. ACK
8. STOP
*/
void Oled_Write_Cmd(char dataCmd)5.1.4 OLED的寻址模式
如何显示一个点?
有三种,分别位页地址模式,水平地址模式和垂直地址模式,可以通过一下表格进行配置
内存管理
{
// 1. start()
IIC_Start();
//
// 2. 写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
// 3. ACK
IIC_ACK();
// 4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
IIC_Send_Byte(0x00);
// 5. ACK
IIC_ACK();
//6. 写入指令/数据
IIC_Send_Byte(dataCmd);
//7. ACK
IIC_ACK();
//8. STOP
IIC_Stop();
}
void Oled_Write_Data(char dataData)
{
// 1. start()
IIC_Start();
//
// 2. 写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
// 3. ACK
IIC_ACK();
// 4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
IIC_Send_Byte(0x00);
// 5. ACK
IIC_ACK();
///6. 写入指令/数据
IIC_Send_Byte(dataData);
//7. ACK
IIC_ACK();
//8. STOP
IIC_Stop();
}
2.OLED的寻址模式
如何显示一个点?
有三种,分别位页地址模式,水平地址模式和垂直地址模式,可以通过一下表格进行配置内存管理
页寻址模式
#include "reg52.h"
#include "intrins.h"
sbit scl = P0^1;
sbit sda = P0^3;
void IIC_Start()
{
scl = 0; //防止雪花
sda = 1;
scl = 1;
_nop_(); //耗时5个微妙
sda = 0;
_nop_();
}
void IIC_Stop()
{
scl = 0; //防止雪花
sda = 0;
scl = 1;
_nop_(); //耗时5个微妙
sda = 1;
_nop_();
}
char IIC_ACK()
{
char flag;
sda = 1; //就在时钟脉冲9期间释放数据线
_nop_();
scl = 1;
_nop_();
flag = sda;
_nop_();
scl = 0;
_nop_();
return flag;
}
void IIC_Send_Byte(char dataSend) //发送一个字节
{
int i;
for(i = 0 ;i<8;i++)
{
scl = 0;//scl 拉低,让sda做好数据准备
sda = dataSend & 0x80; // 1000 0000 获得datasend的最高位
_nop_(); //发送数据建立的时间
scl = 1;//scl拉高开始发送
_nop_(); //数据发送时间
scl = 0; // 发送完毕拉低
_nop_();//
dataSend = dataSend <<1;
}
}
void Oled_Write_Cmd(char dataCmd)
{
//1.start()
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据
IIC_Send_Byte(0x00);
//5.ACK
IIC_ACK();
//6.写入指令/数据
IIC_Send_Byte(dataCmd);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
void Oled_Write_Data(char dataData)
{
//1.start()
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据
IIC_Send_Byte(0x40);
//5.ACK
IIC_Stop();
//6.写入指令/数据
IIC_Send_Byte(dataData);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
//oled初始函数
void Oled_Init(void){
Oled_Write_Cmd(0xAE);//--display off
Oled_Write_Cmd(0x00);//---set low column address
Oled_Write_Cmd(0x10);//---set high column address
Oled_Write_Cmd(0x40);//--set start line address
Oled_Write_Cmd(0xB0);//--set page address
Oled_Write_Cmd(0x81); // contract control
Oled_Write_Cmd(0xFF);//--128
Oled_Write_Cmd(0xA1);//set segment remap
Oled_Write_Cmd(0xA6);//--normal / reverse
Oled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
Oled_Write_Cmd(0x3F);//--1/32 duty
Oled_Write_Cmd(0xC8);//Com scan direction
Oled_Write_Cmd(0xD3);//-set display offset
Oled_Write_Cmd(0x00);//
Oled_Write_Cmd(0xD5);//set osc division
Oled_Write_Cmd(0x80);//
Oled_Write_Cmd(0xD8);//set area color mode off
Oled_Write_Cmd(0x05);//
Oled_Write_Cmd(0xD9);//Set Pre-Charge Period
Oled_Write_Cmd(0xF1);//
Oled_Write_Cmd(0xDA);//set com pin configuartion
Oled_Write_Cmd(0x12);//
Oled_Write_Cmd(0xDB);//set Vcomh
Oled_Write_Cmd(0x30);//
Oled_Write_Cmd(0x8D);//set charge pump enable
Oled_Write_Cmd(0x14);//
Oled_Write_Cmd(0xAF);//--turn on oled panel
}
//清屏函数
void Oled_Clear()
{
unsigned char i,j;
for(i = 0;i<8;i++)
{
Oled_Write_Cmd(0xB0 + i); //page0 -- page7
//每个page从第0列开始
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
//每个page从0列到127列,依次写入0,每写入数据,列地址自动偏移
for(j = 0;j<128;j++)
{
Oled_Write_Data(0);
}
}
}
void main()
{
int a = 10;
//1.Oled初始化
Oled_Init();
//2.选择一个位置
//2.1选择页寻址模式
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x02);
Oled_Clear();
//2.2选择PAGE0 1011 0000 0xB0
Oled_Write_Cmd(0xB0);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
//3.显示一个点
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Cmd(0xB5);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Data(0x08);
Oled_Write_Cmd(0xB0);
Oled_Write_Cmd(0x0f);
Oled_Write_Cmd(0x17);
Oled_Write_Data(0x08);
while(1); //程序不能退出
}
OLED显示一行字
思路:1.防止雪花bug
2. Oled_Write_Cmd(0xB0); //page0开始
Oled_Write_Cmd(0x00); //从第0列开始
Oled_Write_Cmd(0x10);
后面等同
#include "reg52.h"
#include "intrins.h"
sbit scl = P0^1;
sbit sda = P0^3;
void IIC_Start()
{
scl = 0; //防止雪花
sda = 1;
scl = 1;
_nop_(); //耗时5个微妙
sda = 0;
_nop_();
}
void IIC_Stop()
{
scl = 0; //防止雪花
sda = 0;
scl = 1;
_nop_(); //耗时5个微妙
sda = 1;
_nop_();
}
char IIC_ACK()
{
char flag;
sda = 1; //就在时钟脉冲9期间释放数据线
_nop_();
scl = 1;
_nop_();
flag = sda;
_nop_();
scl = 0;
_nop_();
return flag;
}
void IIC_Send_Byte(char dataSend) //发送一个字节
{
int i;
for(i = 0 ;i<8;i++)
{
scl = 0;//scl 拉低,让sda做好数据准备
sda = dataSend & 0x80; // 1000 0000 获得datasend的最高位
_nop_(); //发送数据建立的时间
scl = 1;//scl拉高开始发送
_nop_(); //数据发送时间
scl = 0; // 发送完毕拉低
_nop_();//
dataSend = dataSend <<1;
}
}
void Oled_Write_Cmd(char dataCmd)
{
//1.start()
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据
IIC_Send_Byte(0x00);
//5.ACK
IIC_ACK();
//6.写入指令/数据
IIC_Send_Byte(dataCmd);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
void Oled_Write_Data(char dataData)
{
//1.start()
IIC_Start();
//2.写入从机地址 b0111 1000 0x78
IIC_Send_Byte(0x78);
//3.ACK
IIC_ACK();
//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据
IIC_Send_Byte(0x40);
//5.ACK
IIC_Stop();
//6.写入指令/数据
IIC_Send_Byte(dataData);
//7.ACK
IIC_ACK();
//8.STOP
IIC_Stop();
}
void Oled_Init(void){
Oled_Write_Cmd(0xAE);//--display off
Oled_Write_Cmd(0x00);//---set low column address
Oled_Write_Cmd(0x10);//---set high column address
Oled_Write_Cmd(0x40);//--set start line address
Oled_Write_Cmd(0xB0);//--set page address
Oled_Write_Cmd(0x81); // contract control
Oled_Write_Cmd(0xFF);//--128
Oled_Write_Cmd(0xA1);//set segment remap
Oled_Write_Cmd(0xA6);//--normal / reverse
Oled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
Oled_Write_Cmd(0x3F);//--1/32 duty
Oled_Write_Cmd(0xC8);//Com scan direction
Oled_Write_Cmd(0xD3);//-set display offset
Oled_Write_Cmd(0x00);//
Oled_Write_Cmd(0xD5);//set osc division
Oled_Write_Cmd(0x80);//
Oled_Write_Cmd(0xD8);//set area color mode off
Oled_Write_Cmd(0x05);//
Oled_Write_Cmd(0xD9);//Set Pre-Charge Period
Oled_Write_Cmd(0xF1);//
Oled_Write_Cmd(0xDA);//set com pin configuartion
Oled_Write_Cmd(0x12);//
Oled_Write_Cmd(0xDB);//set Vcomh
Oled_Write_Cmd(0x30);//
Oled_Write_Cmd(0x8D);//set charge pump enable
Oled_Write_Cmd(0x14);//
Oled_Write_Cmd(0xAF);//--turn on oled panel
}
void Oled_Clear()
{
unsigned char i,j;
for(i = 0;i<8;i++)
{
Oled_Write_Cmd(0xB0 + i); //page0 -- page7
//每个page从第0列开始
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
//每个page从0列到127列,依次写入0,每写入数据,列地址自动偏移
for(j = 0;j<128;j++)
{
Oled_Write_Data(0);
}
}
}
/*-- 文字: 爱 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char a1[16] = {0x80,0x64,0x2C,0x34,0x24,0x24,0xEC,0x32,0x22,0x22,0x32,0x2E,0x23,0xA2,0x60,0x00};
code char a2[16] = {0x00,0x41,0x21,0x91,0x89,0x87,0x4D,0x55,0x25,0x25,0x55,0x4D,0x81,0x80,0x80,0x00};
/*-- 文字: 我 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char w1[16] = {0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00};
code char w2[16] = {0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00};
/*-- 文字: 的 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char d1[16] = {0x00,0xF8,0x0C,0x0B,0x08,0x08,0xF8,0x40,0x30,0x8F,0x08,0x08,0x08,0xF8,0x00,0x00};
code char d2[16] = {0x00,0x7F,0x21,0x21,0x21,0x21,0x7F,0x00,0x00,0x00,0x43,0x80,0x40,0x3F,0x00,0x00};
/*-- 文字: 宝 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char b1[16] = {0x10,0x4C,0x44,0x44,0x44,0x44,0x45,0xC6,0x44,0x44,0x44,0x44,0x44,0x54,0x0C,0x00};
code char b2[16] = {0x40,0x40,0x44,0x44,0x44,0x44,0x44,0x7F,0x44,0x44,0x54,0x64,0x44,0x40,0x40,0x00};
/*-- 文字: 贝 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char bb1[16] = {0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0xE2,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00};
code char bb2[16] = {0x00,0x00,0x80,0x47,0x20,0x10,0x0C,0x03,0x08,0x10,0x20,0x47,0x80,0x00,0x00,0x00};
/*-- 文字: 猪 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
code char z1[16] = {0x22,0x14,0x08,0xF4,0x02,0x20,0x24,0x24,0xA4,0x7F,0x24,0x34,0x28,0x26,0x20,0x00};
code char z2[16] = {0x44,0x82,0x41,0x3F,0x00,0x04,0x02,0xFF,0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00};
void main()
{
unsigned char i;
//1.Oled初始化
Oled_Init();
//2.选择一个位置
//2.1选择页寻址模式
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x02);
Oled_Clear();
//2.2选择PAGE0 1011 0000 0xB0
Oled_Write_Cmd(0xB0); //page0
Oled_Write_Cmd(0x00); //从第0列开始
Oled_Write_Cmd(0x10);
for(i = 0; i<16;i++)
{
Oled_Write_Data(a1[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(w1[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(d1[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(b1[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(bb1[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(z1[i]);
}
Oled_Write_Cmd(0xB1); //page1
Oled_Write_Cmd(0x00); //从第0列开始
Oled_Write_Cmd(0x10);
for(i = 0; i<16;i++)
{
Oled_Write_Data(a2[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(w2[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(d2[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(b2[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(bb2[i]);
}
for(i = 0; i<16;i++)
{
Oled_Write_Data(z2[i]);
}
while(1); //程序不能退出
}