赞
踩
根据IIC协议来操作oled
IIC操作可以看我的上一篇文章:IIC协议,利用io模拟
传输中的参数介绍
SA0:通过翻译可以知道SA0使用来确定地址的,配置0和1分别代表2个不同的地址
原文:The slave address is following the start condition for recognition use. The slaveaddress is either “b0111100” or “b0111101” by changing the SA0 to LOW or HIGH(D/C pin acts as SA0).R/W#:通过翻译可以知道R/W#配置为0时模块变为写模式
原文:The write mode is established by setting the R/W# bit to logic “0”.
CO:通过翻译可以知道当CO被设置为0时表示将开始传输信息了
原文:If the Co bit is set as logic “0”, the transmission of the following information willcontain data bytes only.D/C:通过翻译可以知道当D/C为0 时下面一个字节传输的是一个命令,当设置为1时传输的字节就是数据
原文: The D/C# bit determines the next data byte is acted as a command or a data. If theD/C# bit is set to logic “0”, it defines the following data byte as a command. If theD/C# bit is set to logic “1”, it defines the following data byte as a data which will bestored at the GDDRAM. The GDDRAM column address pointer will be increasedby one automatically after each data write.
根据写命令时序图,我们可以分别写出写命令和写指令两个函数
- void IIC_Oled_send_byte(char SendByte)//发送数据
- {
- Start_signal();//起始信号
- IIC_send_byte(0x78);//确定地址01111000倒数第二个是地址位倒数第一个0写入模式
- IIC_ACK();//应答信号
- IIC_send_byte(0x40);//写数据
- IIC_ACK();//应答信号
- IIC_send_byte(SendByte);//发送一个字节
- IIC_ACK();//应答信号
- TerminationSignal();//终止信号
- }
-
- void IIC_Oled_send_cmd(char Sendcmd)//发送指令
- {
- Start_signal();//起始信号
- IIC_send_byte(0x78);//确定地址
- IIC_ACK();//应答信号
- IIC_send_byte(0x00);//写指令
- IIC_ACK();//应答信号
- IIC_send_byte(Sendcmd);//发送一个字节
- IIC_ACK();//应答信号
- TerminationSignal();//终止信号
- }

我们在使用OLED显示屏的时候要先对其进行初始化,初始化的方法文档里有介绍,就是发送一系列指令
- void OLED_Initial()//oled初始化发送以下指令
- {
- //(01)display off (0xae)
- IIC_Oled_send_cmd(0xae);
- //(02)set low column address (0x00)
- IIC_Oled_send_cmd(0x00);
- //(03)set high column address (0x10)
- IIC_Oled_send_cmd(0x10);
- //(04)set start line address (0x40)
- IIC_Oled_send_cmd(0x40);
- //(05)set page address (0xb0)
- IIC_Oled_send_cmd(0xb0);
- //(06)contract control (0x81)
- IIC_Oled_send_cmd(0x81);
- //(07)send 0xff (多字节指令)
- IIC_Oled_send_cmd(0xff);
- //(08)set segment remap (0xa1)
- IIC_Oled_send_cmd(0xa1);
- //(09)set normal/reverse (0xa6)
- IIC_Oled_send_cmd(0xa6);
- //(10)set multiplex ratio (1 to 64) (0xa8 )
- IIC_Oled_send_cmd(0xa8);
- //(11)set duty 1/32 (0x3f)
- IIC_Oled_send_cmd(0x3f);
- //(12)com scan direction (0xc8)
- IIC_Oled_send_cmd(0xc8);
- //(13)set display offset (0xd3)
- IIC_Oled_send_cmd(0xd3);
- //(14)send 0x00
- IIC_Oled_send_cmd(0x00);
- //(15)set osc division (0xd5)
- IIC_Oled_send_cmd(0xd5);
- //(16)send 0x80
- IIC_Oled_send_cmd(0x80);
- //(17)set area color mode off (0xd8)
- IIC_Oled_send_cmd(0xd8);
- //(18)send 0x05
- IIC_Oled_send_cmd(0x05);
- //(19)set pre-charge period (0xd9)
- IIC_Oled_send_cmd(0xd9);
- //(20)send 0xf1
- IIC_Oled_send_cmd(0xf1);
- //(21)set com pin configuration (0xda)
- IIC_Oled_send_cmd(0xda);
- //(22)send 0x12
- IIC_Oled_send_cmd(0x12);
- //(23)set Vcomh (0xdb)
- IIC_Oled_send_cmd(0xdb);
- //(24)send 0x30
- IIC_Oled_send_cmd(0x30);
- //(25)set charge pump enable (0x8d)
- IIC_Oled_send_cmd(0x8d);
- //(26)send 0x14
- IIC_Oled_send_cmd(0x14);
- //(27)turn on oled panel(0xaf)
- IIC_Oled_send_cmd(0xaf);
- }

清屏函数实际上是将所有的显示都设置为0,就是灭的状态
- void CLS()//清屏函数
- {
- int i,j;
- for(i = 0; i<8 ;i++){
- IIC_Oled_send_cmd(0xB0+i);//page0--page7
- //每次写的时候从第一列开始
- IIC_Oled_send_cmd(0x00);
- IIC_Oled_send_cmd(0x10);
- for(j = 0;j<128;j++){
- IIC_Oled_send_byte(0x00);//每写入数据地址自动偏移
- }
- }
- }
有了以上内容我们就可以对OLED 进行操作了
具体操作步骤
//模块初始化
//设置页寻址模式
//00100000
//0x02
//设置大行64行分为8个大行page
//10110xxx
//配置列,用一个高位一个低位设置
//0000xxxx
//0001xxxx
寻址模式的设置
设置page
设置页寻址模式下的列起始地址
- #include "reg52.h"
- #include "intrins.h"
-
- #include "reg52.h"
-
- sbit scl = P0^0;
- sbit sda = P0^1;
-
- void Start_signal()//起始信号
- {
- scl = 1;
- sda = 0;
- sda = 1;
- _nop_();
- sda = 0;
- _nop_();
- scl = 0;
- }
- void TerminationSignal()//终止信号
- {
- scl = 0;
- sda = 0;
- scl = 1;
- _nop_();
- sda = 1;
- _nop_();
- sda = 0;
- _nop_();
- }
-
- char IIC_ACK()//应答信号
- {
- char ACK;
- scl = 0;
- _nop_();
- scl = 1;
- _nop_();
- ACK = sda;
- scl = 0;
- _nop_();
- return ACK;
- }
-
- void IIC_send_byte(char SendByte)//发送一个字节
- {
- int i = 0;
- for(;i<8;i++){
- scl = 0;//数据翻转
- sda = SendByte & 0x80;//发送最高位,发送顺序是由高往低的
- _nop_();//数据准备时间
- scl = 1;//发送数据
- _nop_();//数据发送时间
- SendByte<<= 1;
- }
- }
-
- void IIC_Oled_send_byte(char SendByte)//发送数据
- {
- Start_signal();//起始信号
- IIC_send_byte(0x78);//确定地址01111000倒数第二个是地址位倒数第一个0写入模式
- IIC_ACK();//应答信号
- IIC_send_byte(0x40);//写数据
- IIC_ACK();//应答信号
- IIC_send_byte(SendByte);//发送一个字节
- IIC_ACK();//应答信号
- TerminationSignal();//终止信号
- }
-
- void IIC_Oled_send_cmd(char Sendcmd)//发送指令
- {
- Start_signal();//起始信号
- IIC_send_byte(0x78);//确定地址
- IIC_ACK();//应答信号
- IIC_send_byte(0x00);//写指令
- IIC_ACK();//应答信号
- IIC_send_byte(Sendcmd);//发送一个字节
- IIC_ACK();//应答信号
- TerminationSignal();//终止信号
- }
-
- void OLED_Initial()//oled初始化发送以下指令
- {
- //(01)display off (0xae)
- IIC_Oled_send_cmd(0xae);
- //(02)set low column address (0x00)
- IIC_Oled_send_cmd(0x00);
- //(03)set high column address (0x10)
- IIC_Oled_send_cmd(0x10);
- //(04)set start line address (0x40)
- IIC_Oled_send_cmd(0x40);
- //(05)set page address (0xb0)
- IIC_Oled_send_cmd(0xb0);
- //(06)contract control (0x81)
- IIC_Oled_send_cmd(0x81);
- //(07)send 0xff (多字节指令)
- IIC_Oled_send_cmd(0xff);
- //(08)set segment remap (0xa1)
- IIC_Oled_send_cmd(0xa1);
- //(09)set normal/reverse (0xa6)
- IIC_Oled_send_cmd(0xa6);
- //(10)set multiplex ratio (1 to 64) (0xa8 )
- IIC_Oled_send_cmd(0xa8);
- //(11)set duty 1/32 (0x3f)
- IIC_Oled_send_cmd(0x3f);
- //(12)com scan direction (0xc8)
- IIC_Oled_send_cmd(0xc8);
- //(13)set display offset (0xd3)
- IIC_Oled_send_cmd(0xd3);
- //(14)send 0x00
- IIC_Oled_send_cmd(0x00);
- //(15)set osc division (0xd5)
- IIC_Oled_send_cmd(0xd5);
- //(16)send 0x80
- IIC_Oled_send_cmd(0x80);
- //(17)set area color mode off (0xd8)
- IIC_Oled_send_cmd(0xd8);
- //(18)send 0x05
- IIC_Oled_send_cmd(0x05);
- //(19)set pre-charge period (0xd9)
- IIC_Oled_send_cmd(0xd9);
- //(20)send 0xf1
- IIC_Oled_send_cmd(0xf1);
- //(21)set com pin configuration (0xda)
- IIC_Oled_send_cmd(0xda);
- //(22)send 0x12
- IIC_Oled_send_cmd(0x12);
- //(23)set Vcomh (0xdb)
- IIC_Oled_send_cmd(0xdb);
- //(24)send 0x30
- IIC_Oled_send_cmd(0x30);
- //(25)set charge pump enable (0x8d)
- IIC_Oled_send_cmd(0x8d);
- //(26)send 0x14
- IIC_Oled_send_cmd(0x14);
- //(27)turn on oled panel(0xaf)
- IIC_Oled_send_cmd(0xaf);
- }
- void CLS()//清屏函数
- {
- int i,j;
- for(i = 0; i<8 ;i++){
- IIC_Oled_send_cmd(0xB0+i);//page0--page7
- //每次写的时候从第一列开始
- IIC_Oled_send_cmd(0x00);
- IIC_Oled_send_cmd(0x10);
- for(j = 0;j<128;j++){
- IIC_Oled_send_byte(0x00);//每写入数据地址自动偏移
- }
- }
- }
-
- /*-- 文字: S --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
- char s1[8]={0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00};
- char s2[8]={0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00};
-
- void main()
- {
- int i;
- //1.OLED初始化
- OLED_init();
- //2.选择一个位置
- //2.1 选择寻址模式——确认页寻址模式
- OLED_write_order(0x20);
- OLED_write_order(0x02);
- CLS();
- //2.2 选择PAGE0
- IIC_Oled_send_cmd(0xb0);
- IIC_Oled_send_cmd(0x00);
- IIC_Oled_send_cmd(0x10);
- //3.显示
- for(i = 0;i < 8;i++){
- IIC_Oled_send_byte(s1[i]);
- }
-
- IIC_Oled_send_cmd(0xb1);
- IIC_Oled_send_cmd(0x00);
- IIC_Oled_send_cmd(0x10);
- for(i = 0;i < 8;i++){
- IIC_Oled_send_byte(s2[i]);
- }
-
- while(1);
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。