1.思路:
void Oled_Show_Image(unsigned char *image) //
{
unsigned char i; //-128 ~ 127位
unsigned int j; //j要重新定义,因为要到达图片的最后一位
//行 i没有问题,j有问题 i为1时,j要从128开始这样子才会刷屏
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++) //列 i等于0的时候 ,j<128 //i到7的时候 i+1=8, 其实就是j<8(i+1)最后j要<128*8
{
Oled_Write_Data(image[j]);
}
}
}
bug:
code unsigned char bmpImager[]={};数组后面要加分号
效果演示:
代码:
#include "reg52.h"
#include "intrins.h"sbit scl = P0^1;
sbit sda = P0^3;void IIC_Start()
{scl = 0; //防止雪花sda = 1;scl = 1;_nop_(); //耗时5个微妙sda = 0;_nop_();
}void IIC_Stop()
{scl = 0; //防止雪花sda = 0;scl = 1;_nop_(); //耗时5个微妙sda = 1;_nop_();
}char IIC_ACK()
{char flag;sda = 1; //就在时钟脉冲9期间释放数据线_nop_();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 & 0x80; // 1000 0000 获得datasend的最高位_nop_(); //发送数据建立的时间scl = 1;//scl拉高开始发送_nop_(); //数据发送时间scl = 0; // 发送完毕拉低_nop_();//dataSend = dataSend <<1;}
}void Oled_Write_Cmd(char dataCmd)
{//1.start()IIC_Start();//2.写入从机地址 b0111 1000 0x78IIC_Send_Byte(0x78);//3.ACKIIC_ACK();//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据IIC_Send_Byte(0x00);//5.ACKIIC_ACK();//6.写入指令/数据IIC_Send_Byte(dataCmd);//7.ACKIIC_ACK();//8.STOPIIC_Stop();
}void Oled_Write_Data(char dataData)
{//1.start()IIC_Start();//2.写入从机地址 b0111 1000 0x78IIC_Send_Byte(0x78);//3.ACKIIC_ACK();//4.cotrol byte : (0)(0) 000000 写入命令 (0)(1) 000000写入数据IIC_Send_Byte(0x40);//5.ACKIIC_Stop();//6.写入指令/数据IIC_Send_Byte(dataData);//7.ACKIIC_ACK();//8.STOPIIC_Stop();
}void Oled_Init(void){Oled_Write_Cmd(0xAE);//--display offOled_Write_Cmd(0x00);//---set low column addressOled_Write_Cmd(0x10);//---set high column addressOled_Write_Cmd(0x40);//--set start line addressOled_Write_Cmd(0xB0);//--set page addressOled_Write_Cmd(0x81); // contract controlOled_Write_Cmd(0xFF);//--128Oled_Write_Cmd(0xA1);//set segment remapOled_Write_Cmd(0xA6);//--normal / reverseOled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)Oled_Write_Cmd(0x3F);//--1/32 dutyOled_Write_Cmd(0xC8);//Com scan directionOled_Write_Cmd(0xD3);//-set display offsetOled_Write_Cmd(0x00);//Oled_Write_Cmd(0xD5);//set osc divisionOled_Write_Cmd(0x80);//Oled_Write_Cmd(0xD8);//set area color mode offOled_Write_Cmd(0x05);//Oled_Write_Cmd(0xD9);//Set Pre-Charge PeriodOled_Write_Cmd(0xF1);//Oled_Write_Cmd(0xDA);//set com pin configuartionOled_Write_Cmd(0x12);//Oled_Write_Cmd(0xDB);//set VcomhOled_Write_Cmd(0x30);//Oled_Write_Cmd(0x8D);//set charge pump enableOled_Write_Cmd(0x14);//Oled_Write_Cmd(0xAF);//--turn on oled panel
}void Oled_Clear()
{unsigned char i,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 = 0;j<128;j++){Oled_Write_Data(0);}}}code unsigned char bmpImager[]={/*-- 调入了一幅图像:C:\Users\r7000p\Desktop\路飞.bmp --*/
/*-- 宽度x高度=128x64 128*8*8--*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0xC0,0x20,0x00,0x10,0x10,0x08,0x00,0x80,0x10,0x14,0x00,0x00,0x88,0x8C,0x8C,
0xCC,0x4C,0x2C,0x1C,0x1C,0x18,0x9C,0x98,0x10,0x20,0x00,0x04,0x40,0x10,0x80,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x90,0x10,0x10,
0xF0,0x80,0xC0,0xF0,0xB0,0x20,0x20,0xE0,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x20,0xE0,0x00,0x80,0xC0,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,0x08,0x00,0x20,0x80,0x00,0x50,0xF0,
0x60,0xF0,0xF9,0xFB,0xFB,0x3F,0x1F,0x0E,0x06,0x0E,0x7E,0x87,0x0F,0x0F,0x3F,0x7F,
0xFF,0xDF,0x3F,0x7F,0x3F,0x7F,0xFE,0xFE,0xFE,0xFE,0xF4,0xF4,0xB0,0x60,0x20,0x21,
0x20,0x20,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF9,0x81,0x81,0xFF,
0x89,0x00,0x08,0x34,0x17,0x13,0x13,0x14,0xFC,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xC3,0x04,0x04,0x0C,0x88,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x00,0x28,0xC0,0x90,0x98,0x18,0x9C,0xFE,
0xFF,0x1F,0x03,0x07,0x00,0x00,0x42,0x42,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,
0x00,0x03,0x04,0x03,0x41,0x40,0x01,0x13,0x0F,0x1F,0x7F,0x7F,0x7F,0x6E,0x98,0x30,
0xB8,0x28,0x60,0x80,0x02,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,
0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x01,0x01,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,0x40,
0x00,0x00,0x20,0x00,0x00,0x00,0x80,0x84,0x80,0x83,0x83,0x87,0x03,0x0A,0xE0,0x00,
0x13,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x09,0x08,0x08,0x02,0x08,0x08,0x0A,0x08,
0x08,0x09,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x01,0x88,0xF0,0x88,0x01,0x00,
0x01,0x23,0x22,0x63,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,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,0x02,0x00,0x08,0x00,0x00,0x00,0x08,0x00,0x11,0x80,0x10,0x08,0x01,0x03,0x07,
0x08,0x00,0x04,0x00,0x00,0x00,0xA0,0x02,0x02,0x02,0x02,0x00,0x00,0x40,0x00,0x00,
0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x44,0x0F,0x07,0x13,0x01,0x14,
0x02,0x11,0x00,0x00,0x00,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,0x20,0x20,0x08,0x08,0x08,
0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x40,0x40,
0xC0,0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x00,0x02,0x06,0x0E,0x0E,0x1E,0x3E,0x7E,
0x7E,0x7E,0xEE,0x80,0x80,0xC0,0xC0,0x80,0x80,0xC0,0xC0,0xE0,0x00,0x20,0x40,0x60,
0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x04,0x00,0x00,
0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0xE0,0xE0,
0xFF,0x80,0xFF,0xFF,0xF0,0xEC,0xDD,0xF2,0xC8,0x90,0xC0,0x80,0x80,0x00,0x80,0xC0,
0xE1,0x31,0xBC,0xDF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0xC0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x38,0x3F,0x3F,0x3F,
0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x39,0x26,0x27,0x27,0x3F,
0x3E,0x3F,0x3F,0x3F,0x3F,0x3F,0x37,0x3F,0x3B,0x3F,0x3F,0x3F,0x3F,0x3F,0x3E,0x3A,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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;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.选择一个位置//2.1选择页寻址模式Oled_Write_Cmd(0x20);Oled_Write_Cmd(0x02);Oled_Clear();Oled_Show_Image(bmpImager);while(1); //程序不能退出
}