#include <reg52.h>
#define Dataport P0
#define keyport P1
#define uchar unsigned char
#define uint unsigned int
sbit LATCH1=P2^1;//定义锁存使能端口 段锁存
sbit LATCH2=P2^2;// 位锁存
uchar code Duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code Wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar tempdata[10]; //存储显示值的全局变量
void delayMs (uchar t);
void display ( uchar j );
uchar keyboard(void);
void main()
{
uchar num;
while(1)
{
//keyboard( );
num=keyboard();
tempdata[0]= Duan[num/10];
tempdata[1]= Duan[num%10];
display( 1 );
}
}
uchar keyboard(void)
{
uchar hang,lie,key;
keyport=0xff;
keyport=0x0f;
hang=keyport&0xf0;
if(hang!=0x0f)
{
delayMs(10);
if(hang!=0x0f)
{
hang=keyport&0x0f;
keyport=hang|0xf0;
lie=keyport&0xf0;
while((keyport&0xf0)!=0xf0);
key=(hang+lie);
}
}
switch(key)
{
case 0x7e:return 0;break;
case 0x7d:return 1;break;
case 0x7b:return 2;break;
case 0x77:return 3;break;
case 0xbe:return 4;break;
case 0xbd:return 5;break;
case 0xbb:return 6;break;
case 0xb7:return 7;break;
case 0xde:return 8;break;
case 0xdd:return 9;break;
case 0xdb:return 10;break;
case 0xd7:return 11;break;
case 0xee:return 12;break;
case 0xed:return 13;break;
case 0xeb:return 14;break;
case 0xe7:return 15;break;
default:return 0xff;break;
}
}
void display(uchar j )
{
int i;
i++;
if(i==200)
{
i=0;
}
for(j=0;j<2;j++)
{
P0=0;
LATCH1=1;
LATCH1=0;
P0=Wei[j];
LATCH2=1;
LATCH2=0;
P0=tempdata[j];
LATCH1=1;
LATCH1=0;
//delayMs(10);
}
}
void delayMs (uchar t)
{
while(--t)
{
while(--t);
while(--t);
}
}
程序是通过矩阵键盘在数码管上显示0-13的数字、
但是下载完根本没反应、
#define Dataport P0
#define keyport P1
#define uchar unsigned char
#define uint unsigned int
sbit LATCH1=P2^1;//定义锁存使能端口 段锁存
sbit LATCH2=P2^2;// 位锁存
uchar code Duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code Wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar tempdata[10]; //存储显示值的全局变量
void delayMs (uchar t);
void display ( uchar j );
uchar keyboard(void);
void main()
{
uchar num;
while(1)
{
//keyboard( );
num=keyboard();
tempdata[0]= Duan[num/10];
tempdata[1]= Duan[num%10];
display( 1 );
}
}
uchar keyboard(void)
{
uchar hang,lie,key;
keyport=0xff;
keyport=0x0f;
hang=keyport&0xf0;
if(hang!=0x0f)
{
delayMs(10);
if(hang!=0x0f)
{
hang=keyport&0x0f;
keyport=hang|0xf0;
lie=keyport&0xf0;
while((keyport&0xf0)!=0xf0);
key=(hang+lie);
}
}
switch(key)
{
case 0x7e:return 0;break;
case 0x7d:return 1;break;
case 0x7b:return 2;break;
case 0x77:return 3;break;
case 0xbe:return 4;break;
case 0xbd:return 5;break;
case 0xbb:return 6;break;
case 0xb7:return 7;break;
case 0xde:return 8;break;
case 0xdd:return 9;break;
case 0xdb:return 10;break;
case 0xd7:return 11;break;
case 0xee:return 12;break;
case 0xed:return 13;break;
case 0xeb:return 14;break;
case 0xe7:return 15;break;
default:return 0xff;break;
}
}
void display(uchar j )
{
int i;
i++;
if(i==200)
{
i=0;
}
for(j=0;j<2;j++)
{
P0=0;
LATCH1=1;
LATCH1=0;
P0=Wei[j];
LATCH2=1;
LATCH2=0;
P0=tempdata[j];
LATCH1=1;
LATCH1=0;
//delayMs(10);
}
}
void delayMs (uchar t)
{
while(--t)
{
while(--t);
while(--t);
}
}
程序是通过矩阵键盘在数码管上显示0-13的数字、
但是下载完根本没反应、