根据手册SSD1306我们可以知道,OLED屏幕一共有三种寻址方式。分别是页面寻址,水平寻址和垂直寻址。本篇文章将分别使用以上三种方式对OLED屏幕进行全部点亮和熄灭的操作。(使用到了IIC底层驱动代码)
寄存器操作说明:
页面寻址模式
/**
*函数名称:Oled_Clean_Page
*函数功能:清空屏幕 页面寻址模式
*备 注:
*/
unsigned int page_num[8]={0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7};
void Oled_Clean_Page()
{
int i,j;
for(i=0;i<8;i++)
{
Oled_Write_Cmd(page_num[i]);
for(j=0;j<128;j++)
{
Oled_Write_Dat(0x00);
}
}
}
/**
*函数名称:Oled_Full_Page
*函数功能:屏幕全亮 页面寻址模式
*备 注:
*/
void Oled_Full_Page()
{
int i,j;
for(i=0;i<8;i++)
{
Oled_Write_Cmd(page_num[i]);
for(j=0;j<128;j++)
{
Oled_Write_Dat(0xff);
}
}
}
水平寻址模式
/**
*函数名称:Oled_Clean_Horizontal
*函数功能:屏幕全灭 水平寻址模式
*备 注:
*/
void Oled_Clean_Horizontal()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0x00);
}
}
/**
*函数名称:Oled_Full_Horizontal
*函数功能:屏幕全亮 水平寻址模式
*备 注:
*/
void Oled_Full_Horizontal()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0xff);
}
}
垂直寻址模式
/**
*函数名称:Oled_Clean_Vertical
*函数功能:屏幕清空 垂直寻址模式
*备 注:
*/
void Oled_Clean_Vertical()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0x00);
}
}
/**
*函数名称:Oled_Full_Vertical
*函数功能:屏幕全亮 垂直寻址模式
*备 注:
*/
void Oled_Full_Vertical()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0xff);
}
}
项目总代码如下
main.c
#include <STC89C5xRC.H>
#include "intrins.h"
#include "OLED.h"
void test_1() //页面寻址模式
{
Oled_Init();
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x02);
Oled_Full_Page();
Oled_Clean_Page();
Oled_Write_Cmd(0xb0);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Cmd(0xb1);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Cmd(0xb2);
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x10);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
Oled_Write_Dat(0xff);
}
void test_2() //水平寻址模式
{
Oled_Init();
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x00);
Oled_Full_Horizontal();
Oled_Clean_Horizontal();
}
void test_3() //垂直寻址模式
{
Oled_Init();
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x01);
Oled_Full_Vertical();
Oled_Clean_Vertical();
}
void main()
{
test_1();
test_2();
test_3();
while(1);
}
IIC.c
#include <STC89C5xRC.H>
#include "intrins.h"
sbit SCL=P0^1;//时间线
sbit SDA=P0^3;//数据线
void IIC_Start()
{
SDA=1;
SCL=1;
_nop_();
SDA=0;
_nop_();
}
void IIC_Stop()
{
SDA=0;
SCL=1;
_nop_();
SDA=1;
_nop_();
}
char IIC_ACK()
{
char flag;
SDA=1;
_nop_();
SCL=1;
_nop_();
flag=SDA;
_nop_();
SCL=0;
_nop_();
return flag;
}
void IIC_Send_Byte(unsigned char dat)
{
char i;
for(i=0;i<8;i++)
{
SCL=0;
SDA=dat&0x80;
_nop_();
SCL=1;
_nop_();
SCL=0;
_nop_();
dat=dat<<1;
}
}
unsigned char IIC_Read_Byte()
{
unsigned char temp;
char i;
for(i=0;i<8;i++)
{
SCL=1;
_nop_();
temp=temp<<1;
if(SDA==1)
{
temp=temp|SDA;
}
SCL=0;
_nop_();
}
return temp;
}
IIC.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start();
void IIC_Stop();
char IIC_ACK();
void IIC_Send_Byte(unsigned char dat);
unsigned char IIC_Read_Byte();
#endif
OLED.c
#include <STC89C5xRC.H>
#include "intrins.h"
#include "IIC.h"
/**
*函数名称:Oled_Write_Cmd
*函数功能:向OLED中写入指令
*备 注:
*/
void Oled_Write_Cmd(unsigned char cmd)
{
IIC_Start();
IIC_Send_Byte(0x78);
IIC_ACK();
IIC_Send_Byte(0x00);
IIC_ACK();
IIC_Send_Byte(cmd);
IIC_ACK();
IIC_Stop();
}
/**
*函数名称:Oled_Write_Dat
*函数功能:向OLED中写入数据
*备 注:
*/
void Oled_Write_Dat(unsigned char dat)
{
IIC_Start();
IIC_Send_Byte(0x78);
IIC_ACK();
IIC_Send_Byte(0x40);
IIC_ACK();
IIC_Send_Byte(dat);
IIC_ACK();
IIC_Stop();
}
/**
*函数名称:Oled_Init
*函数功能:初始化OLED
*备 注:
*/
void Oled_Init()
{
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
}
/**
*函数名称:Oled_Clean_Page
*函数功能:清空屏幕 页面寻址模式
*备 注:
*/
unsigned int page_num[8]={0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7};
void Oled_Clean_Page()
{
int i,j;
for(i=0;i<8;i++)
{
Oled_Write_Cmd(page_num[i]);
for(j=0;j<128;j++)
{
Oled_Write_Dat(0x00);
}
}
}
/**
*函数名称:Oled_Full_Page
*函数功能:屏幕全亮 页面寻址模式
*备 注:
*/
void Oled_Full_Page()
{
int i,j;
for(i=0;i<8;i++)
{
Oled_Write_Cmd(page_num[i]);
for(j=0;j<128;j++)
{
Oled_Write_Dat(0xff);
}
}
}
/**
*函数名称:Oled_Clean_Horizontal
*函数功能:屏幕全灭 水平寻址模式
*备 注:
*/
void Oled_Clean_Horizontal()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0x00);
}
}
/**
*函数名称:Oled_Full_Horizontal
*函数功能:屏幕全亮 水平寻址模式
*备 注:
*/
void Oled_Full_Horizontal()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0xff);
}
}
/**
*函数名称:Oled_Clean_Vertical
*函数功能:屏幕清空 垂直寻址模式
*备 注:
*/
void Oled_Clean_Vertical()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0x00);
}
}
/**
*函数名称:Oled_Full_Vertical
*函数功能:屏幕全亮 垂直寻址模式
*备 注:
*/
void Oled_Full_Vertical()
{
int i;
Oled_Write_Cmd(0x22);//设置页面地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x07);
Oled_Write_Cmd(0x21);//设置列地址
Oled_Write_Cmd(0x00);
Oled_Write_Cmd(0x7f);
for(i=0;i<1024;i++)
{
Oled_Write_Dat(0xff);
}
}
OLED.h
#ifndef _OLED_H
#define _OLED_H
void Oled_Init();
void Oled_Clean_Page();//页面寻址模式
void Oled_Full_Page();//页面寻址模式
void Oled_Full_Horizontal();//水平寻址模式
void Oled_Clean_Horizontal();//水平寻址模式
void Oled_Clean_Vertical();//垂直寻址模式
void Oled_Full_Vertical();//垂直寻址模式
void Oled_Write_Dat(unsigned char dat);
void Oled_Write_Cmd(unsigned char cmd);
#endif