📄 lcd12864_1.c
字号:
//=====================================================================
// LCD12864 液晶显示技术I
// Make Time: 2008-4-8
//=======================================================================
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//=======引脚定义==================
sbit lck = P3^5;//锁存信号
//======常用命令及参数定义==========
#define DISPON 0x3f
#define DISPOFF 0x3e
#define DISPFIRST 0xc0
#define SETX 0x40
#define SETY 0xb8
#define LCDBUZY 0x80
#define L 0x00
#define R 0x40
#define LIMIT 0x80
#define LINE 16 // 设置最多一行可以显示多少字符(1~~16)
//========全局变量===============
code uchar pixel[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar cbyte;
uchar data statu;
bit xy; // the position variable
uchar charNum=1;
//this named charNum variable recorded the char position,the whole screen can write 56 characters
//one line can be written 14 characters
//=========汉字序列定义
#define HZ_TIAN '~'+1
#define HZ_DAO '~'+3
#define HZ_CHOU '~'+5
#define HZ_QIN '~'+7
//=========函数声明============
void WrL(uchar x);
void WrR(uchar x);
void Lcmcls(void);
void delay1s(void);
void Lcminit (void);
void Putpicture(uchar flag);
void delay(unsigned int time);
void VtoH8x16change(uchar *hzbuf);
void Puthalf(uchar *strch,uchar row,uchar col);
void Wrdata(uchar x,uchar row,uchar col);
void Locatexy(uchar row,uchar col);
void vWrite8x16Character(uchar *ch,uchar row,uchar col,bit flag);
void vWrite8x16String(uchar *str,uchar col, uchar row, bit flag);
extern uchar code picture1[];
extern uchar code picture2[];
extern uchar code char_Table[95][16];
struct //时间结构体
{
unsigned char hour; //0~23
unsigned char min; //0~59
unsigned char sec; //0~59
unsigned int msec; //0~999
unsigned int speeder; // define the speed yourself in interrupt program
unsigned long allsec; //3600*hour+60*min+sec
}time;
//;***************************************************************
//; Name : InitialMain()
//; Usage: to do some initial work before enter the game
//;***************************************************************
void InitialMain(void)
{
/* set initail time */
time.hour=0;
time.min=0;
time.sec=0;
time.msec=0;
time.allsec=0;
TMOD=0x11;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
PT1=0;PT0=1;
}
//============================================
// Usage: print the character in format
//=============================================
void Printc(uint chr)
{
uchar lie,hang;
hang=(charNum-1)/LINE;
hang=hang*2;
lie=(charNum-1)%LINE;
lie=8*lie;
vWrite8x16Character(char_Table[chr-' ' ],hang,lie,0);
charNum++; //add one every time you write a character
}
//=============================================
// name : Prints(uchar *str)
// usage: print a stream in format
//=============================================
void Prints(uchar *str)
{
while(*(str)!='\0' )
{ // 一些格式的处理
if(*(str)=='\n' ) //回车处理
{
charNum=(charNum+15)/16;
charNum=charNum*16+1;
str++;
if(*(str)=='\0' ) return;
}
if(*(str)=='\r' ) //跳到所在行的行头
{
charNum=(charNum+15)/16;
charNum=(charNum)*16-15;
str++;
if(*(str)=='\0' ) return;
}
Printc(*(str));
str++;
}
}
//================================================
// Name : Format(uchar *str)
// usage: this function is printing some format
// 格式化显示字符串
//=================================================
void Format(uchar *str )
{
if(*(str)=='\n' ) //回车处理
{
charNum=(charNum+15)/16;
charNum=charNum*16+1;
return;
}
if(*(str)=='\r' ) //回到本行头
{
charNum=(charNum+15)/16;
charNum=charNum*16-15;
return;
}
if(*(str)=='\p' ) //回到上一行头
{
charNum=(charNum+15)/16;
charNum=charNum*16-31;
return;
}
if(*(str)=='\b' ) //退格,并删除
{
charNum--;
Prints(" " );
charNum--;
return;
}
}
//================================================
// 名称: Printd(int integer)
// 功能: just can display -32768~~~32768
//================================================
void Printd(int integer)
{
if(integer==0)
{Printc('0' );return;}
{char i,j;
char lenth[6];
if(integer<0)
{lenth[5]=1;integer=-integer;} //负数
else
lenth[5]=0; //正数
lenth[4]=integer%10;
integer/=10;
lenth[3]=integer%10;
integer/=10;
lenth[2]=integer%10;
integer/=10;
lenth[1]=integer%10;
lenth[0]=integer/10;
i=0;
while(i<5)
{
if( lenth[i]!=0)
{ j=i;
break;
}
i++;
}
//display in lcd12864
if(lenth[5]==1) Printc('-' );
for(i=j;i<5;i++)
{ Printc(lenth[i]+'0' ); }
}
}
//=================================================================
// 名称: DisplayInLine(*str)
// 功能: 在某一行,流动显示一个句子
// 参数: str是要显示的句子,thisLine为显示行的行首位置
//=================================================================
void DisplayInLine(uchar* str,uchar thisLine)
{
static uint i,j;
if(j<i+15) //+15
{
Printc( *(str+j) );
j++;
}
else
{
j=i; charNum=thisLine;
if(i<strlen(str)-16) //-15
{
i++;
}
else
i=0;
}
}
//====================================================================
// 显示一张128*64 的函数
// 参数: pictureName 是你需要显示的图片数组名称,可以放在外部文件
//====================================================================
void DrawPicture(uchar* pictureName)
{
unsigned char i,j,row, col ;
unsigned int x;
row = 0; col=0;
for(j=0;j<128;j++){
x=j*0x08;
for(i=0;i<8;i++)
{
cbyte=*(pictureName+x);
Wrdata(cbyte,row,col);
x++;
row++;
}
row=0;
col++;
}
}
//=============================================
// SetPixel(uchar x, uchar y)
// 0<x<128 , 0<y<64
//=============================================
SetPixel(uchar x, uchar y)
{
uchar pixeler;
pixeler=pixel[y%8];
y=y/8;
Wrdata(pixeler,y,x);
}
//=============================================
//
//==============================================
/*
void Line(uchar x0, uchar y0, uchar x1, uchar y1)
{
float k; //斜率
float x;
uchar y; // 点坐标
int buf;
if(x0)
if(x0>x1) //起点的X坐标要是较少的
{
buf=x0;
x0=x1;
x1=buf;
buf=y0;
y0=y1;
y1=buf;
}
if(x1!=x0)
k=(float)(y1-y0)/(x1-x0);
else
k=9999;
for(x=x0;x<x1;x+=0.01)
{ y=k*(x-x0)+y0;
SetPixel(x,y);
}
}*/
//===================================================
// 主函数
//===================================================
void main(void)
{
// 你可以增加任意长度的英文单词,程序会循环显示
code uchar introduce[]=
"Hello,Welcome to ## 51kaifa ## ^_^ "
"This sentance is used for testing the moving LCD."
"Firstly,you must know the principle of LCD driver."
"Secondly,you can add the code here, what ever enlish characters!! "
"Welcome contacting me with my email: wabil2007@yahoo.com.cn "
;
void wait();
ulong disdplayLenth=4500; //设置你需要显示这句子的长度
uchar temp1,temp2; //临时变量,用于各种运算
TMOD=0x11; //开启计数器,在中断部分计算时间变量
ET0=EA=1;
TR0=1;
// 初始化时间结构成员
time.hour=11;
time.min=11;
time.sec=11;
Lcminit(); //初始化LCM
Lcmcls();
DrawPicture(picture2); //显示载入界面图
delay1s();
charNum=37;
Prints("Loading..." ); //显示Loading..字样
charNum=49;
while(charNum<65)
{Prints(" " );} //清理最后一行,用来显示进度条
charNum=49;
while(charNum<65)
{Prints(">" ); delay(20000);} //进度条用>>>>>>>>>
Lcmcls();
charNum=23;
Prints("^_^ " );
Printc(HZ_TIAN ); //输出一个汉字需要两个字节
Printc(HZ_TIAN+1 );
Printc(HZ_DAO ); //输出一个汉字需要两个字节
Printc(HZ_DAO+1 );
Printc(HZ_CHOU ); //输出一个汉字需要两个字节
Printc(HZ_CHOU+1 );
Printc(HZ_QIN ); //输出一个汉字需要两个字节
Printc(HZ_QIN+1 );
wait();
Lcmcls();
charNum=1; //记录屏幕中字符的位置变量,令它归1
while(disdplayLenth--)
{
DisplayInLine(introduce,1);
}
Lcmcls();
Format("\n" );
Prints(" you have taken " );
temp1=time.sec;
Printd(temp1-11);
Prints(" seconds since beginning C51." ); //计算处从你开机到现在用了多少秒
while(time.sec-temp1<4) //wait 4 second
;
Lcmcls();
charNum=1;
Prints("===============" );
charNum=49;
Prints("===============" );
while(1)
{
charNum=18;
Prints("TIME NOW IS_ " );
if(time.hour<10) Printd(0);
Printd(time.hour);
Prints(":" );
if(time.min<10) Printd(0);
Printd(time.min);
Prints(":" );
if(time.sec<10) Printd(0);
Printd(time.sec);
}
}
/***************约1S延时********************/
void delay1s(void)
{
delay(50000);
delay(50000);
}
/***************用于测试的延时子程序********************/
void wait()
{
delay(50000);
delay(50000);
delay(50000);
delay(50000);
delay(50000);
}
//***************************************************************
// Name : Ontimer0()
// Usage: to count some time register and do some time-work
//****************************************************************
void OnTimer0(void) interrupt 1 // using as
{
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
// handle some time work code here...
time.msec++;
if(time.msec>999)
{
time.msec=0;
time.sec++;
time.allsec++;
// time.allsec records the seconds time
// that since the program is start 32bit large~~136 years' seconds
}
if(time.sec>59)
{
time.sec=0;
time.min++;
}
}
/***************微秒级延时*****************/
void delay(unsigned int time)
{ unsigned int i;
for(i=0;i<time;i++);
}
//***********初始化LCD*******************/
void Lcminit (void)
{
cbyte=DISPOFF;
WrL(cbyte);
WrR(cbyte);
cbyte=DISPON;
WrL(cbyte);
WrR(cbyte);
cbyte=DISPFIRST;
WrL(cbyte);
WrR(cbyte);
Lcmcls();
Locatexy(0,0);
}
/***************LCD清屏********************/
void Lcmcls(void)
{ uchar i,j;
for(i=0;i<8;i++){
delay(6);
for(j=0;j<LIMIT;j++){
delay(6);
Wrdata(0x0,i,j);
}
}
}
/**************写左区***************/
void WrL(uchar x)
{
P0=0xFF;//P0口送FF,准备读
lck = 0;
P1 = 0x2a;//ELCD=1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -