//#define unsigned char unsigned char
//#define uint unsigned int
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
//sbit Spk=P1^3;//speaker
////延时函数//////////
void msec(unsigned int x)
{
unsigned char j;
while(x--)
{
for (j=0;j <125;j++)
{;}
}
}
//液晶配置函数
void configure(unsigned char com) //com配置参数,初始化时需要配置若干次
{
unsigned char m;
RS=0;
RW=1;
do
{
DB=0x0ff;
E=1;
m=DB;
E=0;
m=m&0x80;
}
while (m==0x80);
RW=0;
DB=com;
E=1;
E=0;
}
//写数据函数
void writeData(unsigned char buf)
{
unsigned char m;
RS=0;
RW=1;
do
{
DB=0x0ff;
E=1;
m=DB;
E=0;
m=m&0x80;
}
while (m==0x80);
RS=1;
RW=0;
DB=buf;
E=1;
E=0;
}
//---------------------------------
//显示函数
void printchar(unsigned char row,unsigned char col, buf )
{
unsigned char cod;
unsigned char m;
cod=0x80;
if(row==1) cod=cod+0x40;
if(row==2) cod=cod+0x14;
if(row==3) cod=cod+0x54;
cod=cod+col;
//cod=0x80 + 0x10 * row + col/2;
configure(cod);
RS=0;
RW=1;
do
{
DB=0x0ff;
E=1;
m=DB;
E=0;
m=m&0x80;
}
while (m==0x80);
RS=1;
RW=0;
DB=buf;
E=1;
E=0;
}
//--------------------------
void print(unsigned char row,unsigned char col,char * buf,unsigned char len )//行,列
{
unsigned char i;
unsigned char cod;
unsigned char m;
cod=0x80;
if(row==1) cod=cod+0x10;
if(row==2) cod=cod+0x08;
if(row==3) cod=cod+0x18;
cod=cod+col;
//cod=0x80 + 0x10 * row + col/2;
configure(cod);
for(i=0;i <len;i++)
{
RS=0;
RW=1;
do
{
DB=0x0ff;
E=1;
m=DB;
E=0;
m=m&0x80;
}
while (m==0x80);
RS=1;
RW=0;
DB=buf;
E=1;
E=0;
// msec(0x10);
}
}
void LcdIni()
{
DB=0x30;
RS=0;
RW=0;
msec(0x15);
DB=0x38;
E=1;
E=0;
configure(0x01);
configure(0x0c);
configure(0x06);
print(1,2,"陕西创新", strlen("陕西创新"));
print(2,0,"科技有限公司", strlen("科技有限公司"));
msec(1000);
configure(0x01);//清屏
msec(2000);
// msec(100);
// configure(0x01); //清除显示,并且设定地址指针为00H
print(0,0,"名称:", strlen("名称:"));
print(0,5,"型号", strlen("型号"));
print(2,0,"电压:", strlen("电压:"));
print(3,0,"状态:", strlen("状态:"));
print(1,0,"方向:", strlen("方向:"));
}
//单片机初始化:
void InitialCpu()
{
//开串行中断
ET0=1; //开定时中断0
ET1=0; //关定时中断1
ES=1; //开串行中断
EX0=0; //开外部中断0
EX1=0; //开外部中断1
EA=1; //中断允许
IP=0x00; //中断优先级
//串行中断设置:
SCON=0x50; //SM0 SM1 SM2 REN =0101,模式1,允许接收;
//假设波特率19.2k,晶震为11.059M.
PCON=PCON | 0x80; //SMOD=1;
TMOD=0x21 ; //定时器1工作模式2,8位自动重装.当溢出后自动装载TH1到TL1.定时器1工作模式1:16位
TL1=0xFD;
TH1=0xFD; //初值
TR1=1; //启动订时器
TR0=1;
TL0=76; //50毫秒定时
TH0=247; //初值
}
void Timer0() interrupt 1
{
static unsigned int Timer0i=0;
Timer0i++;
if(Timer0i==20)
{
Timer0i=0;
TL0=76; //50毫秒定时
TH0=247; //初值
}
}
//键盘键值获取Get_Key值
void kk()
{
switch(Get_Key)
{
case 1: configure(0x85);
configure(0x0d);
TR0=0;
key1++;
if(key1> 4)
{
key1=0;
}
else //利用一个按键实现3种不同内容显示,
{
if(key1==1)
{msec(100);print(0,5,"型号1", strlen("型号1")); TR0=1;}
if(key2==2)
{msec(100);print(0,5,"型号2", strlen("型号1"));TR0=1;}
if(key3==3)
{msec(100);print(0,5,"型号3", strlen("型号3"));TR0=1;}
}
break;
case 2:
{ TR0=0;
configure(0x95);
configure(0x0d);
key2++;
if(key2 <3)
{
if(key2==1)
{
msec(100);
print(1,5,"水平", strlen("水平"));
}
if(key2==2)
{
msec(100);
print(1,5,"垂直", strlen("垂直"));
}
}
else
{key2=0;}
}break;
case 8:
{
configure(0x0c);
configure(0x06);
}break;
}
}
/////
void main()
{
LcdIni();
InitialCpu();
msec(100);
key1=0;
key2=0;
key3=0;
key4=0;
while(1)
{
Get_Key=keyscan();//键值检测
kk();
}
}
//这是键盘扫描程序,
// 键消抖的延时函数
//矩阵键盘,使用左 右 上 下 确定 取消,按键
//P1口为键盘扫描口
/*******************************************************************************/
//矩阵键盘,使用左 右 上 下 确定 取消,按键
//P1口为键盘扫描口
/*******************************************************************************/
#ifndef __key_H__
#define __key_H__
void delay(unsigned char x)
{
unsigned char y,z;
for(y=x;y> 0;y--)
for(z=110;z> 0;z--);
}
unsigned char keyscan()
{ unsigned char num;
unsigned char temp;
P1=0xfe;
temp=P1;
temp=temp&0xf0;//获取高位数据
while(temp!=0xf0)
{
delay(20);
temp=P1;
temp=temp&0xf0;
temp=P1;//继续读取键值
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:num=1;break;//读取后退出
case 0xde:num=2;break;
case 0xbe:num=3;break;
case 0x7e:num=4;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
delay(10);
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;//获取高位数据
while(temp!=0xf0)
{
delay(20);
temp=P1;
temp=temp&0xf0;
temp=P1;//继续读取键值
while(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:num=5;break;//读取后退出
case 0xdd:num=6;break;
case 0xbd:num=7;break;
case 0x7d:num=8;break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
}
delay(10);
}
}
return num;
}
#endif
我现在是能够实现按键显示第一个数据,然后再次按下按键后,显示不能得到改变,
研究了好长时间,不知道问题出在哪里,麻烦论坛上的高手帮忙指点一下,谢谢!!