#include"oled12864.h"
void delay_ms(unsigned int ms)//延时函数
{
unsigned char i, j;
while(ms--)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
}
void SPI_delay()
{
_nop_();
_nop_();
_nop_();
}
void OLED_WR_Byte(u8 dat,u8 cmd)//MCU写一个字节的数据
{
u8 i;
if(cmd)
OLED_DC=1;//数据/命令线
else
OLED_DC=0;
OLED_CS=0;//片选线
for(i=0;i<8;i++)
{
OLED_SCL=0;//时钟线
SPI_delay();
if(dat&0x80)
{
OLED_SDA=1;//数据线
}
else
{
OLED_SDA=0;
}
SPI_delay();
OLED_SCL=1;
SPI_delay();
dat<<=1;
}
OLED_CS=1;
OLED_DC=1;
}
void OLED_Set_Pos(u8 x, u8 y) //坐标设置
{
x+=2;//SH1106驱动芯片需要移2位
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
/*void OLED_Display_On(void)//开启OLED显示
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
}*/
/*void OLED_Display_Off(void)//关闭OLED显示
{
OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
}*/
void OLED_Clear(void) //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!! 赋值0全黑 赋值0xFF全亮
{
u8 i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
OLED_WR_Byte (0x02,OLED_CMD); //设置显示位置—列低地址
OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址
for(n=0;n<128;n++)
{
OLED_WR_Byte(0,OLED_DATA);
}
} //更新显示
}
void OLED_ShowChinese(u8 x,u8 y,u16 n)//16*16汉字
{
unsigned char wm=0;
unsigned int adder=n; //
OLED_Set_Pos(x , y);
for(wm = 0;wm <16;wm++) //
{
OLED_WR_Byte(F16x16[adder],OLED_DATA);
adder += 1;
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 16;wm++) //
{
OLED_WR_Byte(F16x16[adder],OLED_DATA);
adder += 1;
}
}
void OLED_ShowZiMu(u8 x,u8 y,u16 n)//8*16字母
{
unsigned char wm=0;
unsigned int adder=n; //
OLED_Set_Pos(x , y);
for(wm = 0;wm <8;wm++) //
{
OLED_WR_Byte(F16x16[adder],OLED_DATA);
adder += 1;
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 8;wm++) //
{
OLED_WR_Byte(F16x16[adder],OLED_DATA);
adder += 1;
}
}
void ShowChinese()//显示16*16汉字
{
OLED_ShowChinese(28,1,0); //点阵显示 橫起始地址 列0-7 数据 电
OLED_ShowChinese(44,1,32); //点阵显示 橫起始地址 列0-7 数据 子
OLED_ShowChinese(60,1,64); //点阵显示 橫起始地址 列0-7 数据 温
OLED_ShowChinese(76,1,96); //点阵显示 橫起始地址 列0-7 数据 度
OLED_ShowChinese(92,1,128); //点阵显示 橫起始地址 列0-7 数据 计
}
void ShowZiMu()//显示8*16字母
{
OLED_ShowZiMu(14,3,160); //点阵显示 橫起始地址 列0-7 数据 T
OLED_ShowZiMu(22,3,176); //点阵显示 橫起始地址 列0-7 数据 E
OLED_ShowZiMu(30,3,192); //点阵显示 橫起始地址 列0-7 数据 M
OLED_ShowZiMu(38,3,208); //点阵显示 橫起始地址 列0-7 数据 P
OLED_ShowZiMu(46,3,224); //点阵显示 橫起始地址 列0-7 数据 :
}
void OLED_ShowNumber(u8 x,u8 y,u16 n)//8*16数字 //黑底白字
{
unsigned char wm=0;
unsigned int adder=n; //
OLED_Set_Pos(x , y);
for(wm = 0;wm <8;wm++) //
{
OLED_WR_Byte(heidibaizi[adder][wm],OLED_DATA);
}
OLED_Set_Pos(x,y + 1);
for(wm = 0;wm < 8;wm++) //
{
OLED_WR_Byte(heidibaizi[adder][wm+8],OLED_DATA);
}
}
void OLED_Init(void)//屏初始化
{
P0=0xFF;
OLED_SCL=1;
OLED_RES=0;
delay_ms(200);
OLED_RES=1; //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕
OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/
OLED_WR_Byte(0x02,OLED_CMD); /*set lower column address*/
OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
OLED_WR_Byte(0x40,OLED_CMD); /*set display start line*/
OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/
OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/
OLED_WR_Byte(0xcf,OLED_CMD); /*128*/
OLED_WR_Byte(0xA1,OLED_CMD); /*set segment remap*/
OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/
OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/
OLED_WR_Byte(0x3F,OLED_CMD); /*duty = 1/64*/
OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/
OLED_WR_Byte(0x8b,OLED_CMD); /* 0x8B 内供 VCC */
OLED_WR_Byte(0x33,OLED_CMD); /*0X30---0X33 set VPP 9V */
OLED_WR_Byte(0xC8,OLED_CMD); /*Com scan direction*/
OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/
OLED_WR_Byte(0x00,OLED_CMD); /* 0x20 */
OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/
OLED_WR_Byte(0x1f,OLED_CMD); /*0x22*/
OLED_WR_Byte(0xDA,OLED_CMD); /*set COM pins*/
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/
OLED_WR_Byte(0x40,OLED_CMD);
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
}