首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

按键如何控制LCD工作

按键如何控制LCD工作

请哪位高手帮我看看,这个程序错哪里?左右键控制光标的移动,上下控制光标所在位置数的大小

#define uchar unsigned char
#define uint unsigned int
#define key_sure P2_4 //确认键
#define key_down P2_5 //控制数据位加减1,S1减1,S2加1
#define key_up P2_3
#define key_left P2_6 //控制光标移动,S3左移,S4右移
#define key_right P2_2
#define LCM_RW P1_1//定义引脚
#define LCM_RS P1_0
#define LCM_E P1_2
#define LCM_Data P0
#define Busy 0x80 //用于检测LCM状态字中的Busy标识

uchar xdata keyleftflag,keyrightflag,keyupflag,keydownflag,keysureflag;
uchar xdata keydatabuff[4];
void WriteDataLCM(uchar WDLCM);
void WriteCommandLCM(uchar WCLCM,BuysC);
uchar ReadDataLCM(void);
uchar ReadStatusLCM(void);
void LCMInit(void);
void device_init(void);
void displayonechar(uchar X, uchar Y, uchar DData);
//void lcddisplay(void);
void keyscan(void);
void delay10ms(void);
void delay05s(void);
uchar str1[4] ={'5','6','0','7'};
uchar str2[8] ={'1','9','8','3','1','2','0','7'};

#include <at89x51.h>
#include <math.h>
#include <intrins.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

//10ms
void delay10ms(void)
{uchar i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}

//05s
void delay05s(void)
{uchar i;
for(i=0;i<50;i++)
delay10ms();
}

//写数据
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
}
//写指令
void WriteCommandLCM(uchar WCLCM,BuysC) //BuysC为0时忽略忙检测
{
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
delay10ms();
LCM_E = 0;
LCM_E = 1;
}

//读数据
uchar ReadDataLCM(void)
{
ReadStatusLCM();
LCM_RS = 1;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
return(LCM_Data);
}

//读状态
uchar ReadStatusLCM(void)
{
LCM_Data = 0x0FF;
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 0;
LCM_E = 0;
LCM_E = 1;
while (LCM_Data & Busy); //检测忙信号
return(LCM_Data);
}
//LCM初始化
void LCMInit(void)
{
LCM_Data = 0;
P2=0x7c;
WriteCommandLCM(0x38,0);
delay10ms();
WriteCommandLCM(0x8f,0);
WriteCommandLCM(0x0f,0);
delay10ms();
//WriteCommandLCM(0x06,0);
WriteCommandLCM(0x38,0);
//WriteCommandLCM(0x8f,0);
WriteCommandLCM(0x01,1); //清屏并光标复位
}
//按指定位置显示一个字符
void displayonechar(uchar X, uchar Y, uchar DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80; //算出指令码
WriteCommandLCM(X, 0); //这里不检测忙信号,发送地址码
WriteDataLCM(DData);
}
/*
//液晶显示部分
void lcddisplay()
{displayonechar(keydatabuff[1],keydatabuff[0],keydatabuff[2]);
}
*/
//按键控制部分
void keyscan()
{
EA=0;
if(key_left==0)
{

delay05s();
if(keyleftflag==0)
{
WriteCommandLCM(0x10,0);
keyleftflag=1;
if(keydatabuff[0]==0)
{if(keydatabuff[1]==0)
keydatabuff[1]=15;
else keydatabuff[1]--;
}
if(keydatabuff[0]==1)
keydatabuff[0]=0;
}
keyleftflag=0;
}
if(key_right==0)
{
delay05s();
if(keyrightflag==0)
{
WriteCommandLCM(0x14,0);
keyrightflag=1;
if(keydatabuff[0]==0)
{if(keydatabuff[1]==15)
keydatabuff[1]=0;
else keydatabuff[1]++;
}
if(keydatabuff[0]==1)
keydatabuff[0]=0;
}
keyrightflag=0;
}
if(key_up==0)
{
delay05s();
if(keyupflag==0)
{

keyupflag=1;
keydatabuff[2]=keydatabuff[2]-'0';
if(keydatabuff[2]==9)
keydatabuff[2]=0;
else keydatabuff[2]++;
keydatabuff[2]=keydatabuff[2]+'0';
str1[keydatabuff[0]]=keydatabuff[2];
}
keyupflag=0;
displayonechar(keydatabuff[1],0,str1[keydatabuff[0]]);
}
if(key_down==0)
{
delay05s();
if(keydownflag==0)
{

keydownflag=1;
keydatabuff[2]=keydatabuff[2]-'0';
if(keydatabuff[2]==0)
keydatabuff[2]=9;
else keydatabuff[2]--;
keydatabuff[2]=keydatabuff[2]+'0';
str1[keydatabuff[0]]=keydatabuff[2];
}
keydownflag=0;
displayonechar(keydatabuff[1],0,str1[keydatabuff[0]]);
}
// displayonechar(keydatabuff[1],keydatabuff[0],keydatabuff[2]);
if(key_sure==0)
{if(keysureflag==0)
{keysureflag=1;
if(keydatabuff[3]==0)
keydatabuff[3]=1;
}
keysureflag=0;
}

EA=1;
}

void device_init()
{uchar i;
for(i=0;i<4;i++)
keydatabuff=0;
keyupflag=keydownflag=keyleftflag=keyrightflag=0;
P2=0x7c;
TMOD=0x01;
TH0=0x0b1;
TL0=0x0e0;
// PCON=0x80;
SCON=0x58;
TR0=1;
ET0=1;
EA=1;
}

Timer0() interrupt 1 using 0
{ TH0=0x0b1;
TL0=0x0e0;
}

void main(void)
{uchar i,j;
delay10ms(); //启动等待,等LCM讲入工作状态
LCMInit(); //LCM初始化
// delay10ms(); //延时片刻
device_init();
for(i=0;i<4;i++)
displayonechar(i, 0, str1);
for(j=0;j<8;j++)
displayonechar(j, 1, str2[j]);
WriteCommandLCM(0x8f,0);
P2=0x7c;
EA=1;
while(1)
{keyscan();}
}

现在遇到的问题就是左右键工作,但是上下键不受控制,按上或下键数字不动,光标乱闪,不知道是怎么回事
程序的问题  好好看看 吧
返回列表