多谢龙之谷的提点,我已经将程序做了调整,现在可以运行了,下面附程序
[mw_shl_code=c,true]#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
uchar code table[]={'1','2','3','4','5','6','7','8','9','0'};
uchar code table1[]={'A','B','C'};
uchar code table2[]={'D','E','F'};
uchar code table3[]={'G','H','I'};
uchar code table4[]={'J','K','L'};
uchar code table5[]={'M','O','N'};
uchar code table6[]={'O','P','Q','R'};
uchar code table7[]={'S','T','U','V'};
uchar code table8[]={'W','X','Y','Z'};
uchar num1,num2,num3,num4,num5,num6,num7,num8,key=15;
uint flag=0;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void LCD_Write_Com(uchar com)
{
// while(LCD_Check_Busy()); //???ò????
delayms(5);
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
void LCD_Write_Data(uchar Data)
{
//while(LCD_Check_Busy());
delayms(5);
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
delayms(5);
}
void LCD_Write_Char(uchar x,uchar y,uchar Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
void LCD_Init(void)
{
LCD_Write_Com(0x38);
delayms(5);
LCD_Write_Com(0x38);
delayms(5);
LCD_Write_Com(0x38);
delayms(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08);
LCD_Write_Com(0x01);
LCD_Write_Com(0x06);
delayms(5);
LCD_Write_Com(0x0C);
}
void matrixkeyscan()
{
uchar temp;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=10;
break;
case 0xde:
key=2;
if(flag==1) num2++;
break;
case 0xbe:
key=1;
if(flag==1) num1++;
break;
case 0x7e:
key=0;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
key=11;
break;
case 0xdd:
key=5;
if(flag==1) num5++;
break;
case 0xbd:
key=4;
if(flag==1) num4++;
break;
case 0x7d:
key=3;
if(flag==1) num3++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
key=12;
break;
case 0xdb:
key=8;
if(flag==1) num8++;
break;
case 0xbb:
key=7;
if(flag==1) num7++;
break;
case 0x7b:
key=6;
if(flag==1) num6++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
flag=1-flag;
break;
case 0xd7:
key=14;
break;
case 0xb7:
key=13;
break;
case 0x77:
key=9;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
uchar moni()
{
if(flag==1){
switch(key)
{
case 0:return table[0];break;
case 1:return table1[(num1-1)%3];break;
case 2:return table2[(num2-1)%3];break;
case 3:return table3[(num3-1)%3];break;
case 4:return table4[(num4-1)%3];break;
case 5:return table5[(num5-1)%3];break;
case 6:return table6[(num6-1)%4];break;
case 7:return table7[(num7-1)%4];break;
case 8:return table8[(num8-1)%4];break;
case 9:return table[9];break;
default:return 0xff;break;
}
}
else
if(key<=9) return table[key];
else return 0xff;
}
void main()
{
uchar j,k,str;
LCD_Init();
LCD_Write_Com(0x0F);
while(1)
{
matrixkeyscan();
str=moni();
if(str!=0xff)
{
if((k==0)&&(j==0))
LCD_Clear();
LCD_Write_Char(0+k,0+j,str);
k++;
if(k==16)
{
k=0;j++;
if(j==2)
{
j=0;
}
}
}
key=15;
}
}
[/mw_shl_code]
|