版主晚上好!
我在用STC89C52,郭天祥那块实验板上做矩阵键盘练习时遇到奇怪问题,有四个键按下时,P1口灯会亮,但是我程序里面根本没有P1口的编程;用郭天祥的矩阵键盘程序烧入又没有这种现象,所以可以确认是我的程序问题,但是我的程序在keil 调试中,查看P1口,却没有看到任何变化,其他变量num和P3口(键盘输入口)是按预期变化的,真是好奇怪,摊上大事了;
下面是我的程序,大体是检测P3口输入状态,然后数码管显示键盘所代表的0~F,数码管在P0口;P1口连8个发光二极管,P2口是锁存器;P3是键盘;
说下特征,不是所有P1口LED都发光,当按下有四个键跟P3^7有关,当按下这四个键,P1口部分LED,特别是P1^4/5/6会亮;松开就关闭了;在keil调试无效
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar num,temp;
uchar code table[]={ //定义数码管显示0~f
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit wela=P2^7;
sbit dula=P2^6;
void init();
void display(uint u); //显示数码管
void delay(uint z); //延时函数
uchar keyscan(); //扫描矩阵键盘
void main()
{
init();
while(1)
{
display(keyscan());
}
}
void init() //初始化程序
{
P0=0x00; //数码管锁存器选位
wela=1;
wela=0;
P0=0x00; //数码管段锁存器不显示
dula=1;
dula=0;
num=0;
}
void display(uint u) //显示数码管
{
P0=table;
dula=1;
dula=0;
}
void delay(uint z) //延时函数
{
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan() //扫描矩阵键盘
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee: num=0;break;
case 0xde: num=1;break;
case 0xbe: num=2;break;
case 0x7e: num=3;break;
break;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed: num=4;break;
case 0xdd: num=5;break;
case 0xbd: num=6;break;
case 0x7d: num=7;break;
break;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb: num=8;break;
case 0xdb: num=9;break;
case 0xbb: num=10;break;
case 0x7b: num=11;break;
break;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7: num=12;break;
case 0xd7: num=13;break;
case 0xb7: num=14;break;
case 0x77: num=15;break;
break;
}
}
}
return num;
}
以下是**的程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^4;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0};
uchar num,temp,num1;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan();
void display(uchar aa);
void main()
{
num=17;
dula=1;
P0=0;
dula=0;
wela=1;
P0=0xc0;
wela=0;
while(1)
{
display(keyscan());
}
}
void display(uchar aa)
{
dula=1;
P0=table[aa-1];
dula=0;
}
uchar keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:num=9;
break;
case 0xdb:num=10;
break;
case 0xbb:num=11;
break;
case 0x7b:num=12;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:num=13;
break;
case 0xd7:num=14;
break;
case 0xb7:num=15;
break;
case 0x77:num=16;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return num;
}
|