|
如题,基于51单片机的1602液晶显示的模拟电话系统中,矩阵按键设置反了,比如按下#显示的是“1”,按下显示的是“*”,这怎么调代码,没学过液晶不太懂琢磨了好久没有用,代码图片如下
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define DelayNOPx() {_nop_();_nop_();_nop_();_nop_();}
sbit BEEP = P1^0;
sbit RS = P2^0;
sbit RW = P2^1;
sbit E = P2^2;
void DelayMS (uint ms); //声明延时函数
bit LCD_Busy ();
void LCD_Pos (uchar);
void LCD_Wcmd (uchar);
void LCD_Wdat (uchar);
//--------------------------------------------------------------------
// 标题字符串
char code Title_Text[] = {"-- Phone Code --"};
// 键盘序号与键盘符号映射表
uchar code Key_Table[]={'1','2','3','4','5','6','7','8','9','*','0','#'};
// 键盘拨号数字缓冲
uchar Dial_Code_Str[] = {" "};
uchar KeyNo = 0xFF;
int tCount = 0;
//-------------------------------------------------------------------
// 延时
//-------------------------------------------------------------------
void DelayMS (uint x)
{
uchar i;
while(x--) for (i = 0;i < 120; i++);
}
//------------------------------------------------------------------
// 在LCD指定键盘上显示字符串
//-------------------------------------------------------------------
void Display_String(uchar * str,uchar LineNo)
{
uchar k;
LCD_Pos(LineNo);
for (k = 0; k< 16; k++) LCD_Wdat (str[k]);
}
//--------------------------------------------------------------------
// LCD状态检测
//--------------------------------------------------------------------
bit LCD_Busy ()
{
bit result;
RS =0;RW =1;E=1;DelayNOPx();result = (bit)(P0 & 0x80); E= 0;
return result;
}
//--------------------------------------------------------------------
// 写LCD命令
//--------------------------------------------------------------------
void LCD_Wcmd (uchar cmd)
{
while (LCD_Busy ()); //判断LCD是否忙碌
RS=0;RW=0;E=0;_nop_();_nop_();
P0=cmd;DelayNOPx();E=1;DelayNOPx();E=0;
}
//-------------------------------------------------------------------
// 写LCD数据
//--------------------------------------------------------------------
void LCD_Wdat (uchar str)
{
while (LCD_Busy ()); //判断LCD是否忙碌
RS =1;RW =0;E=0;
P0=str; DelayNOPx ();
E=1;DelayNOPx();E=0;
}
//--------------------------------------------------------------------
// LCD初始化
//--------------------------------------------------------------------
void LCD_Init()
{
LCD_Wcmd(0x38);DelayMS(1);//设定LCD为16*2显示,5*7点阵,8位数据接口
LCD_Wcmd(0x0c);DelayMS(1);//开显示,不显示光标
LCD_Wcmd(0x06);DelayMS(1);//显示光标,自动右移,整屏不要动
LCD_Wcmd(0x01);DelayMS(1);//显示清屏
}
//--------------------------------------------------------------------
// 设置LCD显示位置
//--------------------------------------------------------------------
void LCD_Pos (uchar pos)
{
LCD_Wcmd(pos | 0x80);
}
//--------------------------------------------------------------------
// T0控制按键声音
//--------------------------------------------------------------------
void T0_INT () interrupt 1
{
TH0 = -600 / 256;
TL0 = -600 % 256;
BEEP = ~BEEP;
if (++tCount == 200)
{
tCount = 0; TR0 = 0;
}
}
//--------------------------------------------------------------------
// 键盘扫描
//--------------------------------------------------------------------
uchar GetKey ()
{
uchar i, j, k = 0;
uchar KeyScanCode [] = {0xEF, 0xDF, 0xBF, 0x7F}; //键盘扫描码
uchar KeyCodeTable[] ={0xEE,0xED,0xEB,0xDE,0xDD,0xDB,
0xBE,0xBD,0xBB,0x7E,0x7D,0x7B}; //键盘特征码
P3 = 0x0F;//扫描键盘获取按键序号
if (P3!=0x0F) DelayMS(20);//延时去机械抖动
if (P3!=0x0F)
{
for (i = 0; i< 4;i++)
{
P3 = KeyScanCode[i];
for (j = 0;j < 3;j++)
{
k = i * 3 + j;
if (P3 == KeyCodeTable[k]) return k;
}
}
}
else return 0xFF;
}
//--------------------------------------------------------------------
// 主程序
//--------------------------------------------------------------------
void main()
{
uchar i = -1,j;
P0 = P2 = P1 = 0xFF;
IE = 0x82;
TMOD = 0X01;//寄存器工作方式设定为模式1
LCD_Init();//初始化LCD
Display_String (Title_Text,0x00);//在第一行显示标题
while(1)
{
KeyNo = GetKey();//获取按键
if (KeyNo ==0xFF) continue;//无按键时继续扫描
i++ ;
if(KeyNo==9)
{ Dial_Code_Str[i-1]= ' ';i=i-2; } //按”*”键退格
else
{ if (KeyNo==11)
{for (j = 0;j< 16;j++) Dial_Code_Str[j] = ' '; i=-1;}//按键“#”键清空
else if (i == 11)
{for (j = 0;j < 16;j++) Dial_Code_Str[j] = ' ';i = 0;} //超过11位清空
Dial_Code_Str[i] = Key_Table [KeyNo]; }
Display_String (Dial_Code_Str, 0x40); //在第二行显示号码
TR0 = 1;//T0中断控制按键声音
while(GetKey()!=0xFF);//等待释放
}
}
|
|