#include "stm8s.h"
#include "LCD.h"
/********* 时间延时程序 *********/
void delay(u16 t)
{
u16 i,j;
for (i=0;i<t;i++)
for (j=0;j<2900;j++);
}
/******** 写“指令”程序 ********/
void write_com(u8 com)
{
u8 i;
SCK_H;
CS_L;
A0_L;
for(i=0;i<8;i++)
{
if(com&0x80)
SDA_H;
else
SDA_L;
SCK_L;
SCK_H;
com<<=1;//左移一位
}
CS_H;
}
/******** 写“数据”程序 ********/
void write_data(u8 dat)
{
u8 i;
SCK_H;
CS_L;
A0_H;
for(i=0;i<8;i++)
{
if(dat&0x80)
SDA_H;
else
SDA_L;
SCK_L;
SCK_H;
dat<<=1; //左移一位
}
CS_H;
}
/******** 窗口设置程序 ********/
void window(u8 page0,u8 page1,u16 col0,u16 col1)
{
write_com(0x75); //Set Page Address
write_data(page0); // Page Start Address
write_data(page1);// Page End Address
write_com(0x15); //Set Column Address
write_data(col0); // Column Start Address
write_data(col1); // Column End Address
}
/******** 全屏显示程序********/
void fill(u8 dat)
{
u16 i,j;
window(0x00,0x14,0x00,0xef);
write_com(0x5c); //写指令
for(i=0;i<20;i++)
{
for(j=0;j<240;j++)
{
write_data(dat);
}
}
}
/******** 字符显示程序 ********/
/* 取模方式:横向(先左—>右, 先上—>下*/
void chhzes(u8 x,u8 y,u8 m,u8 n,u8 kk,const u8 *s) //写入点阵n*m字符
{
u8 i,j,k,sum=0;
for(k=y;k<WD&&sum<kk;k+=n,sum++)
{
for(i=0;i<m/8;i++)
{
window(x+i,x+m/8-1,k,k+n-1);
write_com(0x5c);//写指令
for(j=0;j<n;j++)
write_data(*s++);
}
}
}
/******** 画矩形方框显示程序 ********/
void block()
{
u16 i,j;
//上边框
window(0,0,0,WD-1);
write_com(0x5c);
for(i=0;i<WD;i++)
{
write_data(0x01);}
//下边框
window(HD/8-1,HD/8-1,0,WD-1);
write_com(0x5c);
for(i=0;i<WD;i++)
{
write_data(0x80);}
//左边框
window(0,HD/8-1,0,0);
write_com(0x5c);
for(i=0;i<WD;i++)
{
write_data(0xFF);}
//右边框
window(0,HD/8-1,WD-1,WD-1);
write_com(0x5c);
for(i=0;i<WD;i++)
{
write_data(0xFF);}
}
/******** 液晶屏初始化程序 ********/
void Init()
{
CONTROL1_OUTPUT_MODE(); //控制口1为输出
CONTROL2_OUTPUT_MODE(); //控制口2为输出
CONTROL3_OUTPUT_MODE(); //控制口3为输出
IF2_L;IF1_L;IF0_L;
delay(20);
REST_L;
delay(20);
REST_H;
delay(50);
/******************************
*** Extension Command 2
******************************/
write_com(0x31); // Extension Command 2
write_com(0xd7); // Disable Auto Read
write_data(0x9f);
write_com(0xe0); // Enable OTP Read
write_data(0x00); //
delay(10);
write_com(0xe3); // OTP Up-Load
delay(20);; //
write_com(0xe1); // OTP Control Out
/******************************
*** Extension Command 1
******************************/
write_com(0x30); // Extension Command 1
write_com(0x94); // Sleep Out
write_com(0xae); // Display OFF
delay(50);
write_com(0x20); // Power Control
write_data(0x0b); // VB, VR, VF All ON
write_com(0x81); // Set
write_data(0x2d); //给定初值0x37 // 0x1d:Vop = 15.5V
write_data(0x04); //
/******************************
*** Extension Command 2
******************************/
write_com(0x31); // Extension Command 2
write_com(0x32); // Analog Circuit Set
write_data(0x00); //
write_data(0x00); // Booster Efficiency =6KHz
write_data(0x01); // Bias=1/12
/******************************
*** Extension Command 2
******************************/
write_com(0x31); // Extension Command 2
write_com(0x49); // HPM=1;
//
write_com(0x51); // Booster Level x8
write_data(0xfa); //
/******************************
*** Extension Command 1
******************************/
write_com(0x30); // Extension Command 1
write_com(0xf0); // Display Mode
write_data(0x10); // Mono Mode
write_com(0xca); // Display Control
write_data(0x00); // CL Dividing Ratio ? Not Divide
write_data(159); // Duty Set 128 Duty
write_data(0x0C); // Frame Inversion
write_com(0xbc); //Data Scan Direction
write_data(0x00); // seg0-seg240 com0-com159
write_com(0xa6); // 3.Inverse display:a6=normal,a7=inverse
write_com(0x0c); //26.Data Format Select DO=0(0x08); LSB on bottom (Default) ; DO=1(0x0c); LSB on top
/******************************
*** Extension Command 2
******************************/
write_com(0x31); // Extension Command 2
write_com(0xf0); //SET Frame Rate HZ
write_data(0x00);
write_data(0x09);
write_data(0x09);
write_data(0x0b);
write_com(0xf4); //SET Temperature Compensation
write_data(0x00);
write_data(0x00);
write_data(0x00);
write_data(0x03);
/******************************
*** Extension Command 1
******************************/
write_data(0x30); //old 0x50
write_data(0x50); //old 0xb0
write_data(0x84);
write_data(0x05);
write_com(0x40); // Internal Power Supply
/******************************
*** Extension Command 2
******************************/
write_com(0x30); // Extension Command 1
write_com(0xaf); // Display ON
}