器材
oled、单片机、杜邦线
源码
#include <REGX52.H>
#include “intrins.h”
sbit sda = P0^3;
sbit scl = P0^1;
ICC起始信号
void ICC_check_start()
{
scl = 0;//清雪花清stop后面的scl = 1
sda = 1;
scl = 1;
_nop_();
sda = 0;
_nop_();
}
ICC停止信号
void ICC_check_stop()
{
scl = 0;//清雪花清stop后面的scl = 1
sda = 0;
scl = 1;
_nop_();
sda = 1;
_nop_();
}
IIC应答信号
char IIC_ACK() //IIC应答信号
{
char flag;
sda = 1; //时钟脉冲9期间释放数据线
_nop_();
scl = 1;
_nop_();
flag = sda;
_nop_();
scl = 0;
_nop_();
return flag;
}
IIC发送字节
void IIC_Send_Byte(char senddate) //发送一个字节
{
int i;
for(i=0;i<8;i++)
{
scl = 0;//让sda做好数据传输准备
sda = senddate & 0x80;//找到发的字节高位是1还是0(走上下线)
_nop_();//发送数据需要准备过程
scl = 1;//开始发送数据
_nop_();//发送数据的过程
scl = 0;//关闭发送数据通道
_nop_();
senddate = senddate << 1;//把最高位挤走(一个字节有8bit,senddate是字节)
}
} //IIC协议
oled写地址
void Oled_Write_Cmd(char cmddate) //写地址
{
//起始信号
ICC_check_start();
//0111 10 0(区分两个OLED位)0(R/W表示写入接受数据)
IIC_Send_Byte(0x78);
//ACK
IIC_ACK();//回应好的
//0(control默认后面为date数据) 0(写入指令)000000
IIC_Send_Byte(0x00);
//ACK
IIC_ACK();//回应好的
//写入oled数据
IIC_Send_Byte(cmddate);
//ACK
IIC_ACK();//回应好的
//STOP(终止信号)
ICC_check_stop();
}
oled写数据
void Oled_Write_date(char datedate) //写数据
{
//起始信号
ICC_check_start();
//0111 10 0(区分两个OLED位)0(R/W表示写入接受数据)
IIC_Send_Byte(0x78);
//ACK
IIC_ACK();//回应好的
//0(control默认后面为date数据) 1(写入数据)000000
IIC_Send_Byte(0x40);
//ACK
IIC_ACK();//回应好的
//写入oled数据
IIC_Send_Byte(datedate);
//ACK
IIC_ACK();//回应好的
//STOP(终止信号)
ICC_check_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
}
oled清屏
void oled_clean()
{
int i,j; //用int不用char(-127—127)
for(i=0;i<8;i++)
{
Oled_Write_Cmd(0xB0 + i); //从page0—page7行清空
Oled_Write_Cmd(0x00); //最左列
Oled_Write_Cmd(0x10); //只用到7bit,16列中8小列最左列
//列会自动偏移一共128列,导致为清空完
for(j=0;j<128;j++)
{
//在128*64中依次写入0
Oled_Write_date(0);
}
}
}
图像数据
code unsigned char bmpimage[] = {
/*-- 调入了一幅图像:C:\Users\14895\Desktop\无标题.bmp --*/
/*-- 宽度x高度=128x64 --*/ //128*8*8
0x00,0x00,0x00,0xC0,0xC0,0xE0,0xC0,0xC0,0x80,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,0x80,0x80,0x80,0x80,
0x80,0x80,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,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xF0,0xFC,0xFF,0x0F,0x01,0x00,0x01,0x01,0x03,0x83,0xF7,0xFF,0x7E,0x0E,0x0E,0x07,
0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x07,0x0E,0x0E,0x0E,0x0E,0x1C,
0x1C,0x1C,0x38,0x38,0x70,0x78,0xFC,0xFC,0xCE,0x86,0x87,0x03,0x07,0x03,0x03,0x03,
0x03,0x03,0x3F,0xFF,0xF8,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,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x7F,0xFF,0xFF,0x80,0x80,0xC0,0x80,0x00,0xC0,0xFF,0xFF,0x3F,0x00,0x00,0x00,0x0C,
0x3E,0x3F,0x3E,0x3E,0x3C,0x18,0x00,0x80,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
0x18,0x7C,0x7E,0x7C,0x7C,0x78,0x30,0x00,0x01,0x03,0x03,0x07,0xFF,0xFE,0xF8,0x38,
0x1C,0x1C,0x0E,0x0F,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,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,0x01,0x03,0x03,0x03,0x01,0x01,0x00,0x1F,0x7F,0xFF,0xC0,0x80,0x80,0x00,0x00,
0x00,0x00,0x10,0x7C,0x7E,0xE7,0xE7,0xE3,0xC7,0xD3,0xC3,0xC3,0xC3,0xF3,0xF3,0xE3,
0xF7,0x7E,0x3E,0x08,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,0xF0,0x7F,0x1F,0x07,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,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,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,0x01,0x03,0x03,0x03,0x07,
0x07,0x07,0x0E,0x0E,0x0E,0x0E,0x1C,0x1C,0x1D,0x1D,0x1D,0x1D,0x1D,0x1D,0x1D,0x1D,
0x1C,0x1C,0x0C,0x0E,0x0E,0x0E,0x07,0x07,0x03,0x03,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,0x04,0x04,0x04,0x04,0x84,0x7C,0x3F,0xC4,0x04,0x04,0x04,0x04,0x00,
0x00,0x00,0x38,0x18,0x00,0x80,0x5F,0x91,0x9F,0x97,0x97,0x99,0xD1,0x9F,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x3C,0x06,0x03,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,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,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x04,0x08,
0x00,0x00,0x00,0x00,0x00,0x08,0x09,0x01,0x06,0x06,0x04,0x05,0x00,0x08,0x00,0x00,
0x00,0x00,0x00,0x00,0x04,0x04,0x07,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,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,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,
};//起始就是16列中8隐藏了 (8)*16*64
图像显示
void oled_showimage(unsigned char *image) //8*128
{
unsigned int i,j;
for(i=0;i<8;i++) //行
{
Oled_Write_Cmd(0xB0 + i); //从page0—page7行清空
Oled_Write_Cmd(0x00); //最左列
Oled_Write_Cmd(0x10); //只用到7bit,16列中8小列最左列
//列会自动偏移一共128列,导致为清空完
for(j=128*i;j<(128*(i+1));j++)//128*8=64*16
{
Oled_Write_date(image[j]);
}
}
}
主函数
void main()
{
//oled初始化
Oled_Init();
//找到在哪地方显示
//1、什么模式
Oled_Write_Cmd(0x20);
Oled_Write_Cmd(0x02);
oled_clean(); //清空oled
oled_showimage(bmpimage); //字符数组
while(1); //卡住
}