OLED之显示一张图片

OLED之显示一张图片

#include "reg52.h"
#include "intrins.h"

sbit scl = P0^1;
sbit sda = P0^3;

// 起始信号
void IIC_Start()
{
	scl = 0; // 防止雪花
	scl = 1;
	sda = 1;
	_nop_();
	sda = 0;
	_nop_();
}

// 终止信号
void IIC_Stop()
{
	scl = 0; // 防止雪花
	scl = 1;
	sda = 0;
	_nop_();
	sda = 1;
	_nop_();
	sda = 0;
}

// 应答
char IIC_ACK()
{
	char flag;
	sda = 1; // 在时钟脉冲9期间释放数据线  拉高 == 释放
	_nop_();  // 一个nop()相当于 5us
	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的高位
		sda = dataSend & 0x80;
		_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_ACK();
	// 6. 写入数据
	IIC_Send_Byte(dataData);
	// 7. ACK
	IIC_ACK();
	// 8. STOP
	IIC_Stop();
}

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
}

// 清屏函数
void Oled_Clear()
{
	unsigned char i,j;  // -128 --- 127 
	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);
		}
	}
}

/*--  调入了一幅图像:E:\Users\LENOVO\Desktop\截图\无标题.bmp  --*/
/*--  宽度x高度=128x64  --*/
code unsigned char bmpImager[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,
0x20,0x10,0x00,0x08,0x08,0x08,0x08,0x10,0x10,0x70,0x98,0x08,0x28,0x28,0x48,0x80,
0x10,0x60,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,0x00,0x00,0x00,0x00,0x00,0x00,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,
0xF8,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0xC4,0x02,0x01,0x00,0x00,0x00,0x00,0x00,
0x03,0x04,0x00,0x08,0x09,0x09,0x09,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x20,0x22,0x22,0x22,
0x21,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x1E,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x40,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x20,
0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x40,0x40,0x40,
0x00,0x00,0xC0,0x40,0x00,0x00,0x20,0x20,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x06,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x78,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE2,0x0C,0x12,0x32,0x3B,
0x0A,0x00,0x3E,0x00,0x00,0x00,0xFE,0x38,0x00,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,
0x40,0x3C,0x1B,0x95,0x91,0x10,0x10,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x30,0x0E,0x00,0x00,0x00,0x80,0x40,0x30,0x0C,0x03,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x00,0x01,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,
0x04,0x04,0x05,0x02,0x02,0x02,0x00,0x00,0x00,0x02,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,0x1F,0x20,0x20,0x10,0x08,0x06,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void Oled_show_Image(unsigned char *image)
{
	unsigned char i;  // -128 --- 127
	unsigned int 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 = 128 * i; j < 128 * (i + 1); j++){
			Oled_Write_Data(image[j]);
		}
	}
}

void main()
{
	// 1.OLED初始化
	Oled_Init();
	// 2. 选择一个位置
	Oled_Write_Cmd(0x20);
	// 2.1 确认是页选址模式
	Oled_Write_Cmd(0x02);
	Oled_Clear();
	Oled_show_Image(bmpImager);
	
	while(1);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值