lcd12864简单例程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//#pragma sm

#include
#include
#include"WH12864.h"
//#include"CONTROL.c"

uchar counter = 0;
uchar counter0;

uchar tempSL = 0;
uchar tempSH = 0;
uchar tempML = 0;
uchar tempMH = 0;
uchar tempHL = 0;
uchar tempHH = 0;
uchar tempmSL = 0;
uchar tempmSH = 0;

uchar count_temp = 0;

uchar SEC_counter = 0;
uchar MIN_counter= 0;
uchar Hour_counter = 0;
uchar DAY_counter = 0;
uchar NONGLI_DAY;
uchar WEEK_counter = 0;
uchar NONGLI_MON;
uchar MON_counter = 0;
uchar YEAR_counter = 0;
uchar tempDL = 0;
uchar tempDH = 0;
uchar tempMNL = 0;
uchar tempMNH = 0;
uchar tempYL = 0;
uchar tempYH = 0;
/**********************************************************/
BIT No_FUNC_KEY = 1; //1:表示FUNC没有按下
BIT EN_CHANGE;
uchar counterF;
//uchar counterA;
//uchar counterD;
uchar counterC;

uchar sc0;
uchar page0;
uchar column1,column2;
uchar number_b; //空白
uchar num_for_changed;

/**********************************************************/

/****************************************************/
/*
函数名:delay1ms,delayNms
功能:延时函数
特点:

*/
/****************************************************/
BIT FUNC;
BIT ADD_da;
BIT DEC_da;
BIT CONF;
uchar reg0;
uchar reg1;

void delay(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<163;j++)
;
}


void SCAN_KEYBOARD(void)
{
P1 = 0x0f;
if((P1 & 0x0f)!=0x0f)
{
delay();
//delay();
if((P1 & 0x0f)!=0x0f)
{
P1 = 0x0e;
if((P1 & 0x0f)!=0x0f)
{
if((P1&0x0f) == 0x0e)
{
reg0 = 1;
//P3 = 0x0e;
}
}
P1 = 0x0d;
if((P1 & 0x0f)!=0x0f)
{
if((P1&0x0f) == 0x0d)
{
reg0 = 2;
//P3 = 0x0d;
}
}
P1 = 0x0b;
if((P1 & 0x0f)!=0x0f)
{
if((P1&0x0f) == 0x0b)
{
reg0 = 3;
//P3 = 0x0b;
}
}
P1 = 0x07;
if((P1 & 0x0f)!=0x0f)
{
if((P1&0x0f) == 0x07)
{
reg0 = 4;
//P3 = 0x07;
}
}
}
P1 = 0x0f;
if((P1&0x0f)==0x0f)
{
//reg1 = reg0;

if(reg0 == 1)
{
FUNC = 1;
P3 = 0x0e;
delay();
}
else if(reg0 == 2)
{
ADD_da = 1;
P3 = 0x0d;
}
else if(reg0 == 3)
{
DEC_da = 1;
P3 = 0x0b;
}
else
if(reg0 == 4)
{
CONF = 1;
P3 = 0x07;
}

reg0 = 0;
//P3 = 0x0f;
}
}
}
/****************************************************/
/*
函数名:BusyTest,WriteData,WriteInstruction
功能:屏幕状态检测,写入数据,写指令等功能函数
特点:

*/
/****************************************************/

unsigned char BusyTest(void)
{
bit result;
P0 = 0x00;
RS = 0;
RW = 1;
E = 1;
while(P0 & 0x80);
result = BF;
E = 0;
return result;
}

void WriteData(uchar Y)
{
while(BusyTest()==1);
RS = 1;
RW = 0;
E = 0;
P0 = Y;
_nop_();
_nop_();
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_()

;
_nop_();
E = 0;
}

void WriteInstruction(uchar dictate)
{
while(BusyTest()==1);
RS = 0;
RW = 0;
E = 0;
_nop_();
_nop_();
P0 = dictate;
_nop_();
_nop_();
_nop_();
_nop_();
E = 1;
_nop_();
_nop_();
_nop_();
_nop_();
E = 0;
}

/****************************************************/
/*
函数名:Set_page,Set_line,Set_column,Set_on_off
功能:页,行,列,开关屏等的设置函数
特点:

*/
/****************************************************/

void Set_page(uchar page)
{
page = 0xb8 | page;
WriteInstruction(page);
}

void Set_line(uchar startline)
{
startline = 0xc0 | startline;
WriteInstruction(startline);
}

void Set_column(uchar column)
{
column = column & 0x3f;
column = 0x40 | column;
WriteInstruction(column);
}

void Set_on_off(uchar on_off)
{
on_off = 0x3e | on_off;
WriteInstruction(on_off);
}

/****************************************************/
/*
函数名:SelectScreen
功能:选屏
特点:

*/
/****************************************************/

void SelectScreen(uchar screen)
{
switch(screen)
{
case 0:CS1 = 0;CS2 = 0;break;//全屏
case 1:CS1 = 0;CS2 = 1;break;//左半屏
case 2:CS1 = 1;CS2 = 0;break;//右半屏
default:break;
}
}

/****************************************************/
/*
函数名:ClearScreen
功能:清屏
特点:

*/
/****************************************************/

void ClearScreen(uchar screen)
{
uchar i,j;
SelectScreen(screen);
for(i=0;i<8;i++)
{
Set_page(i);
Set_column(0);
for(j=0;j<64;j++)//控制列
{
WriteData(0x00);//写入0,地址指针自加1
}
}
}

/****************************************************/
/*
函数名:LcdInitiate
功能:LCD初始化
特点:

*/
/****************************************************/

void LcdInitiate(void)
{
/*delayNms(15);
WriteInstruction(0x38);
delayNms(5);
WriteInstruction(0x38);
delayNms(5);
WriteInstruction(0x38);
delayNms(5);
WriteInstruction(0x0f);
delayNms(5);
WriteInstruction(0x06);
delayNms(5);
WriteInstruction(0x01);
delayNms(5);*/
BusyTest();
SelectScreen(0);
Set_on_off(0);
SelectScreen(0);
Set_on_off(1);
SelectScreen(0);
ClearScreen(0);
Set_line(0);
}

/****************************************************/
/*
函数名:Display,Display_Photo_Panda
功能:写入显示数据
特点:
列行式
逆向
阴码
*/
/****************************************************/
void Display(uchar ss,uchar page,uchar column,uchar *p)
{
uchar i;
SelectScreen(ss);
Set_page(page);
Set_column(column);
for(i=0;i<5;i++)
{
WriteData(p[i]);
}
Set_page(page+1);
Set_column(column);
for(i=0;i<5;i++)
{
WriteData(p[i+5]);
}
}

void Display0(uchar ss,uchar page,uchar column,uchar *p)
{
uchar i;
SelectScreen(ss);
Set_page(page);
Set_column(column);
for(i=0;i<12;i++)
{
WriteData(p[i]);


}
Set_page(page+1);
Set_column(column);
for(i=0;i<12;i++)
{
WriteData(p[i+12]);
}
}

void Display1(uchar ss,uchar page,uchar column,uchar *p)
{
uchar i;
SelectScreen(ss);
Set_page(page);
Set_column(column);
for(i=0;i<10;i++)
{
WriteData(p[i]);
}
Set_page(page+1);
Set_column(column);
for(i=0;i<10;i++)
{
WriteData(p[i+10]);
}
}

void Display2(uchar ss,uchar page,uchar column,uchar *p)
{
uchar i;
SelectScreen(ss);
Set_page(page);
Set_column(column);
for(i=0;i<16;i++)
{
WriteData(p[i]);
}
Set_page(page+1);
Set_column(column);
for(i=0;i<16;i++)
{
WriteData(p[i+16]);
}
}

void Display_Photo_Panda(uchar ss,uchar page,uchar column,uchar *p)
{
uchar i,j;
for(i=0;i<8;i++)
{
SelectScreen(ss);
Set_page(page+i);
Set_column(column);
for(j=0;j<64;j++)//左半页
{
WriteData(p[j+i*79]);
}
SelectScreen(ss+1);
Set_page(page+i);
Set_column(column);
for(j=64;j<79;j++)//右半页
{
WriteData(p[j+i*79]);
}
}
}

/****************************************************/
/*
函数名:Pro_picture,Pro_time
功能:显示图像,时间的函数
特点:

*/
/****************************************************/
void DISP_KEY_PRO(uchar SC,uchar PAGE,uchar COLUMN,uchar NUM)
{
if(FUNC)
{
Display(SC,PAGE,COLUMN,&ShuZi[NUM*10]);
//delay();
//delay();
//delay();
}
}

void Pro_time(void)
{
Display1(2,0,15,&SJDW[(0*20)]);
Display1(2,0,25,&SJDW[(1*20)]);
Display(2,0,35,&ShuZi[(2*10)]);
Display(2,0,40,&ShuZi[(0*10)]);
tempYL = YEAR_counter/10;
tempYH = YEAR_counter%10;
Display(2,0,45,&ShuZi[(tempYL*10)]);
Display(2,0,50,&ShuZi[(tempYH*10)]);
Display1(2,0,55,&SJDW[(2*20)]);

tempMNL = MON_counter/10;
tempMNH = MON_counter%10;
if(MON_counter > 9)
Display(2,2,20,&ShuZi[(tempMNL*10)]);
Display(2,2,25,&ShuZi[(tempMNH*10)]);
Display0(2,2,30,&TEST[2*24]);
//Display1(2,2,30,&SJDW[3*20]);

tempDL = DAY_counter/10;
tempDH = DAY_counter%10;
if(DAY_counter > 9)
Display(2,2,40,&ShuZi[(tempDL*10)]);
Display(2,2,45,&ShuZi[(tempDH*10)]);
Display0(2,2,50,&TEST[3*24]);
//Display1(2,2,50,&SJDW[4*20]);

Display0(2,4,24,&TEST[0*24]);
Display0(2,4,36,&TEST[1*24]);
Display0(2,4,48,&ShuZi0[(WEEK_counter*24)]); //

tempHL = Hour_counter/10;
tempHH = Hour_counter%10;
Display(2,6,20,&ShuZi[(tempHL*10)]);
Display(2,6,25,&ShuZi[(tempHH*10)]);

Display(2,6,30,&Maoh[0]);

tempML = MIN_counter/10;
tempMH = MIN_counter%10;
Display(2,6,35,&ShuZi[(tempML*10)]);
Display(2,6,40,&ShuZi[(tempMH*10)]);

Display(2,6,45,&Maoh[0]);

tempSL = SEC_counter/10;
tempSH = SEC_counter%10;
Display(2,6,50,&ShuZi[(tempSL*10)]);
Display(2,6,55,&ShuZi[(tempSH*10)]);

}
/***************************************************/
/*
修改算法
*/
/***************************************************/

void Pro_nongli(uchar Mnum,uchar Dnum)
{
uchar tempMN;
uchar DAYL,DA

YH;
//if(EN_NL)
//{

Display2(2,0,20,&nongli[0*32]);
Display2(2,0,36,&nongli[1*32]);
/*==================农历:月=====================*/
tempMN = Mnum%10;
if(Mnum == 0x01)
Display0(2,3,24,&ShuZi0[11*24]);
else if(Mnum <= 0x0a)
Display0(2,3,24,&ShuZi0[(Mnum-1)*24]);
else
{
Display0(2,3,16,&ShuZi0[9*24]);
Display0(2,3,28,&ShuZi0[(tempMN-1)*24]);
}
Display0(2,3,40,&TEST[2*24]);
/*==================农历:日=====================*/
DAYL = Dnum/10;
DAYH = Dnum%10;
if(Dnum <= 10)
{
Display0(2,5,16,&ShuZi0[10*24]);
Display0(2,5,28,&ShuZi0[(Dnum-1)*24]);
}
else if((Dnum < 20)&&(Dnum > 10))
{
Display0(2,5,16,&ShuZi0[9*24]);
Display0(2,5,28,&ShuZi0[(DAYH-1)*24]);
}
else if(Dnum == 20 || Dnum == 30)
{
Display0(2,5,16,&ShuZi0[(DAYL-1)*24]);
Display0(2,5,28,&ShuZi0[9*24]);
//Display0(2,5,28,&ShuZi0[DAYH*24]);
}
else
{
Display0(2,5,16,&ShuZi0[(DAYL-1)*24]);
Display0(2,5,28,&ShuZi0[(DAYH-1)*24]);
}
Display0(2,5,40,&TEST[3*24]);
//}
}

uint picN;
void Pro_picture(void)
{
if(picN == 1)
Display_Photo_Panda(1,0,0,&Panda1[0]);

if(picN == 2)
Display_Photo_Panda(1,0,0,&Panda2[0]);

if(picN == 3)
Display_Photo_Panda(1,0,0,&Panda3[0]);

if(picN == 4)
Display_Photo_Panda(1,0,0,&Panda4[0]);

if(picN == 5)
Display_Photo_Panda(1,0,0,&Panda5[0]);

if(picN == 6)
Display_Photo_Panda(1,0,0,&Panda6[0]);

if(picN == 7)
Display_Photo_Panda(1,0,0,&Panda7[0]);

if(picN == 8)
Display_Photo_Panda(1,0,0,&Panda8[0]);

if(picN == 9)
Display_Photo_Panda(1,0,0,&Panda9[0]);

if(picN == 10)
Display_Photo_Panda(1,0,0,&Panda10[0]);
}

/**********************************************************/
/**********************************************************/
/**********************************************************/

void CLOCK(void)
{
// tempHL = Hour_counter/10;
// tempHH = Hour_counter%10;
Display(2,4,25,&ShuZi[(tempHL*10)]);
Display(2,4,30,&ShuZi[(tempHH*10)]);
//}
//else
//{

//}

Display(2,4,35,&Maoh[0]);

//if(!EN_CHANGE)
//{
// tempML = MIN_counter/10;
// tempMH = MIN_counter%10;
Display(2,4,40,&ShuZi[(tempML*10)]);
Display(2,4,45,&ShuZi[(tempMH*10)]);
}
/*
void Adjust_time(void)
{

}*/
/**********************************************************/
/**********************************************************/

/****************************************************/
/*
函数名:TimerInit
功能:定时器初始化
特点:

*/
/****************************************************/


void TimerInit(void)
{
YEAR_counter = 0x0c;
MON_counter = 0x0a;
DAY_counter = 0x10;
Hour_counter = 11;
MIN_counter = 22;
SEC_counter = 0;

TMOD = 0x01;
TH0 = NUMCLK/256;
TL0 = NUMCLK%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}

/****************************************************/
/*
函数名:main
功能:主函数
特点:

*/
/***

*************************************************/

BIT Rep_dis_temp = 0;//公历农历显示转换
BIT flash0;//进入时间调整状态
BIT flash1;//显示闹钟时间
BIT counter12;
uchar counter13;
uchar flash1_counter;

void main(void)
{
unsigned char i = 0;


LcdInitiate();
ClearScreen(0);
Set_line(0);
TimerInit();
while(1)
{
P3 = 0xff;
delay();
if(!flash1)
{
if(Rep_dis_temp)
Pro_nongli(NONGLI_MON,NONGLI_DAY);
else
Pro_time();
}
else
{
CLOCK();

}

Pro_picture();
//Adjust_time();

if(FUNC)
{
FUNC = 0;
//No_FUNC_KEY = 0;
No_FUNC_KEY = ~No_FUNC_KEY;
//counter13 = 0;
counter12 = 0;

column1 = 50;
column2 = 55;
page0 = 6;
sc0 = 2;
num_for_changed = SEC_counter;
i = 1;
}

if(!No_FUNC_KEY)
{
counter12 = 1;

if(flash0)
{
flash0 = 0;
//counter12 = 1;
counter13++;
switch(counter13)
{


case 1:
{
column1 = 35;
column2 = 40;
page0 = 6;
sc0 = 2;
num_for_changed = MIN_counter;
i = 2;
};
break;
case 2:
{
column1 = 20;
column2 = 25;
page0 = 6;
sc0 = 2;
num_for_changed = Hour_counter;
i = 3;
};
break;
case 3:
{
column1 = 40;
column2 = 45;
page0 = 2;
sc0 = 2;
num_for_changed = DAY_counter;
i = 4;
};
break;
case 4:
{
column1 = 20;
column2 = 25;
page0 = 2;
sc0 = 2;
num_for_changed = MON_counter;
i = 5;
};
break;
case 5:
{
column1 = 50;
column2 = 45;
page0 = 0;
sc0 = 2;
num_for_changed = YEAR_counter;
i = 6;
};
break;
}
if(counter13 == 6)
{
No_FUNC_KEY = 1;
EN_CHANGE = 0;
counter13 = 0;
counter12 = 0;
}
}


}
//if(counter13)
if(counter12)
{
Display(sc0,page0,column1,&ShuZi[10*10]);
Display(sc0,page0,column2,&ShuZi[10*10]);
delay();
}
if(ADD_da)
{
ADD_da = 0;
//if(counter13)
if(counter12)
{
num_for_changed++;
if(i==1)
SEC_counter = num_for_changed;
else if(i==2)
MIN_counter = num_for_changed;
else if(i==3)
Hour_counter = num_for_changed;
else if(i==4)
DAY_counter = num_for_changed;
else if(i==5)
MON_counter = num_for_changed;
else if(i==6)
YEAR_counter = num_for_changed;

//EN_CHANGE = 1;
}
else
{
flash1 = 1;
ClearScreen(0);
}
}
if(DEC_da)
{
DEC_da = 0;
//if(counter13)
if(counter12)
{
num_for_changed--;
if(i==1)
SEC_counter = num_for_changed;
else if(i==2)
MIN_counter = num_for_changed;
else if(i==3)
Hour_counter = num_for_changed;


else if(i==4)
DAY_counter = num_for_changed;
else if(i==5)
MON_counter = num_for_changed;
else if(i==6)
YEAR_counter = num_for_changed;
//EN_CHANGE = 1;
}
else
{
flash1 = 1;
ClearScreen(0);
}
}
if(CONF)
{
CONF = 0;
if(!No_FUNC_KEY)
{
flash0 = 1;
}
else
{
ClearScreen(0);
Rep_dis_temp = (~Rep_dis_temp);
}

/*if(flash1 && !Rep_dis_temp)//!!!需按两下才能退出闹钟时间显示,待解决澹!!!
{
flash1 = 0;
ClearScreen(0);
}*/
}
}
}

/****************************************************/
/*
函数名:Timer0
功能:定时器0中断处理函数
特点:

*/
/****************************************************/

void Timer0(void) interrupt 1
{

//TR0 = 0;
TH0 = NUMCLK/256;
TL0 = NUMCLK%256;

SCAN_KEYBOARD();

counter0++;
if(counter0 == 3)
{
counter0 = 0;
picN ++;
if(picN > 10)
picN = 1;
}

counter++;
if(counter == 20)
{
counter = 0;

flash1_counter++;//3秒后退出闹钟时间显示
if(flash1_counter == 3)
{
flash1 = 0;
flash1_counter = 0;
}

SEC_counter ++;
if(SEC_counter == 60)
{
SEC_counter = 0;
MIN_counter ++;
if(MIN_counter == 60)
{
MIN_counter = 0;
Hour_counter ++;
if(Hour_counter == 24)
{
Hour_counter = 0;
DAY_counter++;
WEEK_counter++;
if(WEEK_counter == 7)
WEEK_counter = 0;
}
}
}
}

if(MON_counter == 2)
{
if(DAY_counter == 29)
{
DAY_counter = 1;
MON_counter ++;
}
}
else
if(MON_counter%2)
{
if(MON_counter>0 || MON_counter<8)
{
if(DAY_counter == 31)
{
DAY_counter = 1;
MON_counter ++;
}
}
else if(MON_counter>7 || MON_counter<13)
{
if(DAY_counter == 32)
{
DAY_counter = 1;
MON_counter ++;
}
}
}
else
{
if(MON_counter>0 || MON_counter<8)
{
if(DAY_counter == 32)
{
DAY_counter = 1;
MON_counter ++;
}
}
else if(MON_counter>7 || MON_counter<13)
{
if(DAY_counter == 31)
{
DAY_counter = 1;
MON_counter ++;
}
}
}
if(MON_counter == 13)
{
MON_counter = 1;
YEAR_counter ++;
}

NONGLI_DAY = 21;
NONGLI_MON = 2;

//TH0 = NUMCLK/256;
//TL0 = NUMCLK%256;
TR0 = 1;
}
/*
水平方向是以字节为单位,竖直方向以位为单位
写数据时,先高字节,再低字节
(因此不能一行行的写数据)
为防止花屏,画点前应先清除GDRAM显示
*/

相关文档
最新文档