IIC驱动0.96寸OLED屏幕(C51/52)

IIC的初始化

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

 2.停止信号

void iic_Stop()
{
	scl=0;
	scl=1;
	sda=0;
	_nop_();
	sda=1;
	_nop_();

}

 3.应答

char iic_ACK()
{
	char flag;
	sda=1;
	_nop_();
	scl=1;
	_nop_();
	flag = sda;
	_nop_();
	scl=0;
	_nop_();
	return flag;
}

4.发送字节

void iic_SendByte(char byte)
{
		int i;
		for(i=0;i<8;i++){
		scl=0;
		sda = byte & 0x80;
		_nop_();
		scl=1;
		_nop_();
		scl=0;
		_nop_();
		byte<<=1;
		}
}

 5.发送命令模式

void olde_Write_cmd(char cmd){
		iic_Start();//开始信号
		iic_SendByte(0x78); //设备地址 0111 1000
		iic_ACK(); //应答信号
		iic_SendByte(0x00); //控制位选择 命令模式 0000 0000
		iic_ACK();
		iic_SendByte(cmd); //命令
		iic_ACK();
		iic_Stop();
}

6.发送数据模式

void olde_Write_data(char datainfo){
		iic_Start();//开始信号
		iic_SendByte(0x78); //设备地址
		iic_ACK(); //应答信号
		iic_SendByte(0x40); //控制位选择 数据模式  0100 0000
		iic_ACK();
		iic_SendByte(datainfo); //数据
		iic_ACK();
		iic_Stop();
}

7.OLED屏幕初始化

void Oled_Init(void){
		olde_Write_cmd(0xAE);//--display off
		olde_Write_cmd(0x00);//---set low column address
		olde_Write_cmd(0x10);//---set high column address
		olde_Write_cmd(0x40);//--set start line address
		olde_Write_cmd(0xB0);//--set page address
		olde_Write_cmd(0x81); // contract control
		olde_Write_cmd(0xFF);//--128
		olde_Write_cmd(0xA1);//set segment remap
		olde_Write_cmd(0xA6);//--normal / reverse
		olde_Write_cmd(0xA8);//--set multiplex ratio(1 to 64)
		olde_Write_cmd(0x3F);//--1/32 duty
		olde_Write_cmd(0xC8);//Com scan direction
		olde_Write_cmd(0xD3);//-set display offset
		olde_Write_cmd(0x00);//
		olde_Write_cmd(0xD5);//set osc division
		olde_Write_cmd(0x80);//
		olde_Write_cmd(0xD8);//set area color mode off
		olde_Write_cmd(0x05);//
		olde_Write_cmd(0xD9);//Set Pre-Charge Period
		olde_Write_cmd(0xF1);//
		olde_Write_cmd(0xDA);//set com pin configuartion
		olde_Write_cmd(0x12);//
		olde_Write_cmd(0xDB);//set Vcomh
		olde_Write_cmd(0x30);//
		olde_Write_cmd(0x8D);//set charge pump enable
		olde_Write_cmd(0x14);//
		olde_Write_cmd(0xAF);//--turn on oled panel
}

8.OLDE清屏


void Oled_Clear()
{
	unsigned char i,j; //-128 --- 127
	
	for(i=0;i<8;i++){
		olde_Write_cmd(0xB0 + i);//page0--page7
		//每个page从0列
		olde_Write_cmd(0x00);
		olde_Write_cmd(0x10);
		//0到127列,依次写入0,每写入数据,列地址自动偏移
		for(j = 0;j<128;j++){
			olde_Write_data(0);
		}
	}
}

演示效果,显示XWD 

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

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






void main()
{
	char i;
	Oled_Init(); //初始化Oled
	//确认位置
	olde_Write_cmd(0x20);
	//选择页寻址模式
	olde_Write_cmd(0x02);
		Oled_Clear();
	//选择页0 1011 0000
	//XWD
	olde_Write_cmd(0xB0);
	olde_Write_cmd(0x00);
		olde_Write_cmd(0x10);
	for(i=0;i<8;i++){
	olde_Write_data(X1[i]);
	}
	for(i=0;i<8;i++){
	olde_Write_data(W1[i]);
	}
	for(i=0;i<8;i++){
	olde_Write_data(D1[i]);
	}
	
	olde_Write_cmd(0xB1);
	olde_Write_cmd(0x00);
	olde_Write_cmd(0x10);
	for(i=0;i<8;i++){
	olde_Write_data(X2[i]);
	}
	for(i=0;i<8;i++){
	olde_Write_data(W2[i]);
	}
	for(i=0;i<8;i++){
	olde_Write_data(D2[i]);
	}
	while(1);
	
}

                                        

你可能感兴趣的:(单片机,嵌入式硬件)