本帖最后由 hanfei08131106 于 2010-5-8 08:54 编辑
下面的程序是一个触摸式液晶显示的,但是每次正常运行一段时间后就会停止,或者硬件自动死机,麻烦哪位有兴趣的帮忙看看啊,谢谢、、、由于数据过大,图像的数据被省略号代替了。
#include <reg52.h>
sbit lcd_rs =P1^5; //L:存取缓存器,H:存取DDRAM
sbit lcd_wr =P1^6; //L有效 写入脚
sbit lcd_rd =P1^7; //L有效 读入脚
sbit lcd_cs1 =P1^3; //L有效 当/CS1为低和CS2为高时,模块处于致能,可接受指令,反之不可接收指令。
//sbit lcd_cs2 =P3^4; //H有效(可直接上拉)
sbit lcd_busy =P1^4; //用以回应模块内部的执行使用状况,可设成高或低电平触发。
sbit lcd_int =P3^2; //接MCU的INT0
//sbit lcd_rst =P3^7; //L有效(可悬空)
#define lcd_regname P2
#define lcd_regdata P2
#define lcd_regnada P2
#define lcd_data P2
#define lcd_control P1
#define uchar unsigned char
#define uint unsigned int
uchar code inittab1[]={
0x00,0x01,0x02,0x03,0x10,0x11,0x12,0x21,0x31,0x41,0x51,0x20,0x30,0x40,0x50,0x60,
0x61,0x70,0x71,0x72,0x80,0x81,0x91,0x90,0xA0,0xA1,0xA2,0xA3,0xB0,0xB1,0xC0,0xC1,
0xC8,0xC9,0xCA,0xD0,0xE0,0xF0,0xF1
};
uchar code inittab2[]={
0xC9,0xE2,0x10,0x80,0x28,0x00,0x91,0x13,0x7F,0x00,0x00,0x13,0x7F,0x00,0x00,0x00,
0x00,0x00,0x00,0x7F,0xAA,0x04,0x00,0x2D,0x11,0x00,0x00,0x00,0x13,0x7F,0x00,0x36,
0x80,0x80,0x00,0x80,0x00,0xA0,0x0F
};
uchar code tab1[]={
" 触摸式液晶显示 "
" 2010年5月5日 "
" 图片显示项目 "
" 触摸此显示图片1 "
" 触摸此显示图片2 "
" 触摸此显示图片3 "
" 触摸此显示图片4 "
" 触摸此显示图片5 "
};
uchar code tab2[]="No-Touch!!";
uchar code tab3[]="Touch-Now!";
uint touchx,touchy;
unsigned char idata key_x1,key_x2,key_y1,key_y2,cc,dd=0x00,ee=0x00,key,key_fg=0x00,start;
unsigned char code xytab[] = {11,12,13,14,15,16,17, //这里是触摸地共有7*7=49个触摸块
21,22,23,24,25,26,27,
31,32,33,34,35,36,37,
41,42,43,44,45,46,47,
51,52,53,54,55,56,57,
61,62,63,64,65,66,67,
71,72,73,74,75,76,77,
};
uchar tabx[4];
uchar taby[4];
uchar code tab4[]={、、、、、、、、、、、、、
};
uchar code tab5[]={、、、、、、、、、、、、、
};
uchar code tab6[]={、、、、、、、、、、、、、、、、、
};
uchar code tab7[]={、、、、、、、、、、、、、、、、
};
uchar code tab8[]={、、、、、、、、、、、、、、、、、、、、
};
void Delay_Nus (int n) //微秒延时
{
int i;
for(i=0;i<n;i++);
}
void Delay_Nms (int n) //毫秒延时
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
//=============================================
void SendCmd(uchar regnada) small
{
lcd_regnada = regnada;
lcd_cs1 =0; // chip enable.
lcd_rd = 1; //
lcd_rs = 0; // rs = 0;
lcd_wr = 0; // wr = 0;
;
lcd_wr = 1; // wr = 1;
lcd_rs = 1; // rs = 1;
lcd_cs1 =1; // chip disable.
}
void WriteDataToREG(uchar regname,uchar regdata) small
{ //写资料到缓存器
SendCmd(regname);
Delay_Nus(1);
SendCmd(regdata);
}
//.............................................
void WriteDataToDDRAM(uchar wrdata) small
{ //写资料到DDRAM
while(lcd_busy == 0);
lcd_data = wrdata;
lcd_cs1 =0; // chip enable. 模块处于致能,可接受指令
lcd_rd = 1; //
lcd_rs = 1; // rs = 1; 存取DDRAM
lcd_wr = 0; // wr = 0;
;
lcd_wr = 1; // wr = 1;
lcd_rs = 1; // rs = 1;
lcd_cs1 =1; // chip disable.
}
//.............................................
uchar ReadDataFromREG(uchar regname) small
{ //读缓存器
uchar reg_rddata;
SendCmd(regname);
lcd_data = 0xff;
lcd_cs1 =0; // chip enable.
lcd_wr = 1; // wr = 1;
lcd_rs = 0; // rs = 0;
lcd_rd = 0; // rd = 0;
;
reg_rddata = lcd_data;
lcd_rd = 1; // rd = 1;
lcd_rs = 1; // rs = 1;
lcd_cs1 =1; // chip disable.
return(reg_rddata);
}
//=============================================
void LCD_Reset(void) small
{ //上电复位
Delay_Nms(240); // delay 240ms 等待上电复位
lcd_control = 0xff; // lcd_rs/wr/rd/cs1/cs2 normal - skeep high.
}
//.............................................
void LCD_Initial(void) small
{ //初始化
uchar i;
for(i=0;i<39;i++)
{
WriteDataToREG(inittab1,inittab2);
}
}
//.............................................
void LCD_CLR(void) small
{ //清屏
uchar reg_rddata;
WriteDataToREG(0xe0,0x00);
reg_rddata = ReadDataFromREG(0xf0);
reg_rddata |= 0x08;
WriteDataToREG(0xf0,reg_rddata);
}
//.............................................
void SetXY(uchar x,uchar y) small
{ //定坐标
WriteDataToREG(0x60,x); // active window top register(awtr)
WriteDataToREG(0x70,y); // active window top register(awtr)
}
//.............................................
void Display_Chinese(uchar code *cha,uint count) small
{ //显示中文或字符
uint i;
for(i=0;i<count;i++)
{
WriteDataToDDRAM(*cha);
++cha;
}
}
//.............................................
void Display_Grid(uchar x0,uchar y0) small
{ //显示图形
uchar j;
for(j=0;j<16;j++)
{
SetXY(x0,y0+j);
WriteDataToDDRAM(0xff);
WriteDataToDDRAM(0xff);
}
}
//=============================================
uchar touch_test(void) small
{
uchar reg_rddata,reg_rddata1;
reg_rddata = ReadDataFromREG(0xc0);
reg_rddata &= 0xf8;
reg_rddata |= 0x08;
WriteDataToREG(0xc0,reg_rddata);
Delay_Nms (6);
reg_rddata1 = ReadDataFromREG(0xc1);
reg_rddata &= 0xf7;
WriteDataToREG(0xc0,reg_rddata);
return(reg_rddata1);
}
//.............................................
uint touch_x(void) small
{
uchar reg_rddata,touchxl;
uint touchx;
reg_rddata = ReadDataFromREG(0xc0);
reg_rddata &= 0xf3;
reg_rddata |= 0x03;
WriteDataToREG(0xc0,reg_rddata);
Delay_Nms (6);
touchx = ReadDataFromREG(0xc8); //TPXR
touchxl = (ReadDataFromREG(0xca)&0xc0); //TPZR
touchx = (touchx<<2)|(touchxl>>6);
return(touchx);
}
//.............................................
uint touch_y(void) small
{
uchar reg_rddata,touchyl;
uint touchy;
reg_rddata = ReadDataFromREG(0xc0);
reg_rddata &= 0xfc;
reg_rddata |= 0x0c;
WriteDataToREG(0xc0,reg_rddata);
Delay_Nms (6);
touchy = ReadDataFromREG(0xc9); //TPYR
touchyl = (ReadDataFromREG(0xca)&0x0c); //TPZR
touchy = (touchy<<2)|(touchyl>>2);
return(touchy);
}
//.............................................
void get_touch(void) small
{
//uchar reg_rddata,i;
WriteDataToREG(0xc0,0xc0); //TOUCH ON
Delay_Nms (6);
if((touch_test() & 0x40))
{
while(!(ReadDataFromREG(0xc1) & 0x80));
touchy = touch_y();
touchx = touch_x();
WriteDataToREG(0xc0,0x00);
key_fg=0x01;
}
}
//.............................................
void lcd_photo(uchar code *pho) small
{ //显示图形
uchar i,j;
uchar code *pp;
SetXY(0,0);
pp=pho;
for(j=0;j<128;j++)
{
for(i=0;i<20;i++)
{
WriteDataToDDRAM(*pp);
++pp;
}
}
}
//.............................................
void int_0 (void) interrupt 0
{
uchar reg_rddata;
reg_rddata = ReadDataFromREG(0xa0);
if(reg_rddata & 0x40)
{
reg_rddata = ReadDataFromREG(0xa0);
reg_rddata &= 0xfb; //禁能触摸中断
WriteDataToREG(0xa0,reg_rddata);
get_touch();
}
reg_rddata = ReadDataFromREG(0xa0);
reg_rddata |= 0x04; //致能触摸中断
WriteDataToREG(0xa0,reg_rddata);
}
//=============================================
void main (void)
{
uchar reg_rddata;
LCD_Reset();
LCD_Initial();
LCD_CLR();
WriteDataToREG(0x00,0xcd);
SetXY(0x00,0x00);
Display_Chinese(tab1,160);
reg_rddata = ReadDataFromREG(0xa0);
reg_rddata |= 0x04; //致能触摸中断
WriteDataToREG(0xa0,reg_rddata);
IT0=0;
//EA=1;
//EX0=1;
while(1)
{
reg_rddata = ReadDataFromREG(0xa0);
if(reg_rddata & 0x40)
{
reg_rddata = ReadDataFromREG(0xa0);
reg_rddata &= 0xfb; //禁能触摸中断
WriteDataToREG(0xa0,reg_rddata);
get_touch(); //接收触摸键程序
}
reg_rddata = ReadDataFromREG(0xa0);
reg_rddata |= 0x04; //致能触摸中断
WriteDataToREG(0xa0,reg_rddata);
if(key_fg)
{
key_fg=0x00;
key_x2=key_x1;key_x1=touchx/100;
key_y2=key_y1;key_y1=touchy/100;
Delay_Nms(20);
if((key_x2!= key_x1)||(key_y2!=key_y1)) continue;
key= xytab[(key_y1-1)*7+key_x1-1];
switch(key / 10)
{
case 3 :{WriteDataToREG(0x10,0x2b); //显示图形1
WriteDataToREG(0x00,0xc5);
lcd_photo(tab4);
Delay_Nms (3000);break; }
case 4 :{ WriteDataToREG(0x10,0x2b); //2b写汉字,3d写英文(粗字),
WriteDataToREG(0x00,0xc5); //0xcd写文字,0xc5写图形
lcd_photo(tab5);
Delay_Nms (3000);break; }
case 5 :{WriteDataToREG(0x10,0x2b); //2b写汉字,3d写英文(粗字),
WriteDataToREG(0x00,0xc5); //0xcd写文字,0xc5写图形
lcd_photo(tab6);
Delay_Nms (3000);break; }
case 6 :{WriteDataToREG(0x10,0x2b); //2b写汉字,3d写英文(粗字),
WriteDataToREG(0x00,0xc5); //0xcd写文字,0xc5写图形
lcd_photo(tab7);
Delay_Nms (3000);break; }
case 7 :{WriteDataToREG(0x10,0x2b); //2b写汉字,3d写英文(粗字),
WriteDataToREG(0x00,0xc5); //0xcd写文字,0xc5写图形
lcd_photo(tab8);
Delay_Nms (3000);break; } // 减键
default :
break;
}
} }
} |