//参考 中景园电子程序
//*******************
#include "oled.h"
#include "STC15W408AS.h"
#include "oledfont.h"
#include "delay.h"
/*
sbit ST7567_CS =P1^0; //片选
sbit ST7567_RST =P1^1;//复位
sbit ST7567_A0 =P1^2;//数据/命令控制
sbit ST7567_SCL =P1^3;//时钟 D0(SCLK?
sbit ST7567_SI =P1^4; //D1(MOSI) 数据
*/
sbit ST7567_CS =P5^5; //片选
sbit ST7567_RST =P5^4;//复位
sbit ST7567_A0 =P1^7;//数据/命令控制
sbit ST7567_SCL =P1^6;//时钟 D0(SCLK?
sbit ST7567_SI =P1^3; //D1(MOSI) 数据
//写入一个字节。
//dat:要写入的数据/命令
//cmd:数据/命令标志 0,表示命令;1,表示数据;
void ST7567_Write_Byte(u8 dat,u8 cmd)
{
u8 i;
ST7567_A0=cmd;
ST7567_CS=0;
for(i=0;i<8;i++)
{
//delay_ms((1);
ST7567_SCL=0;
//delay_ms((1);
if(dat&0x80) ST7567_SI=1;
else ST7567_SI=0;
ST7567_SCL=1;
dat<<=1;
}
ST7567_CS=1;
ST7567_A0=1;
}
/*********************************
函数名称:Lcd_set_pos
功能:LCD设置坐标
输入:x坐标,y坐标
输出:无
备注:x<=131,y<=7
*********************************/
void ST7567_Set_Pos(unsigned char x,unsigned char y)
{
x=x+4;
if(x>127)
{
x=x-127;
y++;
}
ST7567_Write_Byte(0XB0+y,0); //页地址0xb0//页地址
ST7567_Write_Byte(0X00|x&0x0f,0); //低地址0x00//列地址低位
ST7567_Write_Byte(0X10|((x&0xf0)>>4),0);//高地址0x10//列地址高位
}
//***************************************************************************
void ST7567_clear_screen(u8 xx)//全屏填充
{
u8 i,j;
for(i=0;i<8;i++)
{
ST7567_Set_Pos(j,i);
for(j=0;j<128;j++)
{
ST7567_Write_Byte(xx,1);
}
}
}
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//mode:0,反白显示;1,正常显示
//size:选择字体 16/12
void ST7567_ShowChar(u8 x,u8 y,u8 chr)
{
unsigned char c=0,i=0;
c=chr-' ';//得到偏移后的值
if(x>Max_Column-1){x=0;y=y+2;}
if(SIZE == 16)
{
ST7567_Set_Pos(x,y);
for(i=0;i<8;i++)
ST7567_Write_Byte(F8X16[c*16+i],1);
ST7567_Set_Pos(x,y+1);
for(i=0;i<8;i++)
ST7567_Write_Byte(F8X16[c*16+i+8],1);
}
else
{
ST7567_Set_Pos(x,y+1);
for(i=0;i<6;i++)
ST7567_Write_Byte(F6x8[c][i],1);
}
}
//m^n函数
u32 my_pow(u8 m,u8 n)
{
u32 result=1;
while(n--)result*=m;
return result;
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void ST7567_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size2)
{
u8 t,temp;
u8 enshow=0;
for(t=0;t<len;t++)
{
temp=(num/my_pow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
ST7567_ShowChar(x+(size2/2)*t,y,' ');
continue;
}
else enshow=1;
}
ST7567_ShowChar(x+(size2/2)*t,y,temp+'0');
}
}
//显示一个字符号串
void ST7567_ShowString(u8 x,u8 y,u8 *chr)
{
unsigned char j=0;
while (chr[j]!='\0')
{
ST7567_ShowChar(x,y,chr[j]);
x+=8;
if(x>120){x=0;y+=2;}
j++;
}
}
//显示汉字
void ST7567_ShowCHinese(u8 x,u8 y,u8 no)
{
u8 t,adder=0;
ST7567_Set_Pos(x,y);
for(t=0;t<16;t++)
{
ST7567_Write_Byte(Hzk[2*no][t],1);
adder+=1;
}
ST7567_Set_Pos(x,y+1);
for(t=0;t<16;t++)
{
ST7567_Write_Byte(Hzk[2*no+1][t],1);
adder+=1;
}
}
//--------------------------------------------------------------------------
void ST7567_Init()//屏幕初始化
{
ST7567_RST=0;
delay_ms(40);
ST7567_RST=1;
delay_ms(40);
ST7567_CS=0;
ST7567_Write_Byte(0xe2,0); //SOFT RESET//寄存器复位
ST7567_Write_Byte(0x25,0); //0x20-0x27为V5电压内部电阻调整设置
ST7567_Write_Byte(0x2f,0); //0x2f升压电路,电压管理电路,
ST7567_Write_Byte(0x80,0); //起始行对应RAM
ST7567_Write_Byte(0x81,0); // SET EV 对比度寄存器模式0x81
ST7567_Write_Byte(0x1f,0); //0x01-0x3f//对比度调整
ST7567_Write_Byte(0xa1,0); //列地址移动方向:(SEG1->SEG132)0xa0为正向,0xa1为反向
ST7567_Write_Byte(0xc0,0); //行扫描方向:SHL=1(COM64->COM1)0xc0正向扫描,0xc8反射扫描
ST7567_Write_Byte(0xa6,0); //0xa6正向显示,0xa7反向显示
ST7567_Write_Byte(0xa4,0); //0xa4正常显示,0xa5全屏点亮
// ST7567_Write_Byte(0xa3,0);
// ST7567_Write_Byte(0xac,0);
ST7567_Write_Byte(0xf8,0); //背压比设置0xf8
ST7567_Write_Byte(0x08,0); //00--10
ST7567_Write_Byte(0xaf,0); //0xaf显示器开
ST7567_Write_Byte(0x40,0); //00--10
ST7567_clear_screen(1);
ST7567_Set_Pos(0,0);
}