矩阵键盘输入在数码管上显示用只程序复位时为什么显示零

2019-07-15 21:22发布

不用子程序时没有任何的问题,开始时(复位)数码管不显示!,但是修改之后就有问题了,(编码表从0到F),还望各位不嫌弃程序有点长,指点迷津!!非常感谢!
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit yi=P2^6;
sbit er=P2^5;
sbit san=P2^4;
sbit si=P2^7;
uchar code table[]={0x28,
0xee,
0x32,
0xa2,
0xe4,
0xa1,
0x21,
0xea,
0x20,
0xa0,
0x60,
0x25,
0x39,
0x26,
0x31,
0x71,};
uchar num;temp;
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void main()
{
    yi=0;
        er=0;
    san=0;
    si=0;
        while(1)
        {
                P1=0xfe;
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        delay(5);
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {       
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xee:num=0;
                                                break;
                                        case 0xde:num=1;
                                                break;
                                        case 0xbe:num=2;
                                                break;
                                        case 0x7e:num=3;
                                                break;
                                }
                        while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }
                        P0=table[num];
                                }
                        }
                        P1=0xfd;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xed:num=4;
                                                        break;
                                                case 0xdd:num=5;
                                                        break;
                                                case 0xbd:num=6;
                                                        break;
                                                case 0x7d:num=7;
                                                        break;
                                        }
                                while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                P0=table[num];
                                        }
                        }
                        P1=0xfb;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xeb:num=8;
                                                        break;
                                                case 0xdb:num=9;
                                                        break;
                                                case 0xbb:num=10;
                                                        break;
                                                case 0x7b:num=11;
                                                        break;
                                        }
                                        while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                P0=table[num];
                                }
                        }
                        P1=0xf7;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xe7:num=12;
                                                        break;
                                                case 0xd7:num=13;
                                                        break;
                                                case 0xb7:num=14;
                                                        break;
                                                case 0x77:num=15;
                                                        break;
                                        }
                                while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                P0=table[num];
                                }
                        }
               
                }
}

但是当把键盘识别的程序写成子程序,再在主程序调用的时候数码管复位的时候却是显示零,注意:不是零点,是不是说这个时候num已经是0了?
程序修改如下:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit yi=P2^6;
sbit er=P2^5;
sbit san=P2^4;
sbit si=P2^7;
uchar code table[]={0x28,
0xee,
0x32,
0xa2,
0xe4,
0xa1,
0x21,
0xea,
0x20,
0xa0,
0x60,
0x25,
0x39,
0x26,
0x31,
0x71,};
uchar num;temp;
uchar keyscan();
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void main()
{
    yi=0;
        er=0;
    san=0;
    si=0;
        while(1)
        {
                keyscan();
                P0=table[num];       
        }
}
uchar keyscan()
{
        P1=0xfe;
        temp=P1;
        temp=temp&0xf0;
        while(temp!=0xf0)
        {
                delay(5);
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {       
                        temp=P1;
                        switch(temp)
                        {
                                case 0xee:num=0;
                                        break;
                                case 0xde:num=1;
                                        break;
                                case 0xbe:num=2;
                                        break;
                                case 0x7e:num=3;
                                        break;
                        }
                while(temp!=0xf0)
                        {
                                temp=P1;
                                temp=temp&0xf0;
                        }
                        }
                }
                P1=0xfd;
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        delay(5);
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {       
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xed:num=4;
                                                break;
                                        case 0xdd:num=5;
                                                break;
                                        case 0xbd:num=6;
                                                break;
                                        case 0x7d:num=7;
                                                break;
                                }
                        while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }
                                }
                }
                P1=0xfb;
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        delay(5);
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {       
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xeb:num=8;
                                                break;
                                        case 0xdb:num=9;
                                                break;
                                        case 0xbb:num=10;
                                                break;
                                        case 0x7b:num=11;
                                                break;
                                }
                                while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }
                        }
                }
                P1=0xf7;
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        delay(5);
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {       
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xe7:num=12;
                                                break;
                                        case 0xd7:num=13;
                                                break;
                                        case 0xb7:num=14;
                                                break;
                                        case 0x77:num=15;
                                                break;
                                }
                        while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }
                        }
                }
return num;       
}
请指教!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
3条回答
城东
1楼-- · 2019-07-15 23:51
键盘是一个4X4的键盘
城东
2楼-- · 2019-07-16 01:22
下午我继续看了郭老师的视频,后来我给num赋值,赋了一个16,在编码表里加上十六个元素为0xff,果然可以避免了复位时显示零的问题!我想是因为如果num没有赋值那么程序默认为0,所以显示零
修改之后如下:
#include<reg52.h>
#define uchar unsigned char
uchar num,temp;
void delay(uchar z);
uchar keyscan();
sbit yi=P2^6;
sbit er=P2^5;
sbit san=P2^4;
sbit si=P2^7;
uchar code table[]={0x28,
0xee,
0x32,
0xa2,
0xe4,
0xa1,
0x21,
0xea,
0x20,
0xa0,
0x60,
0x25,
0x39,
0x26,
0x31,
0x71,0xff};
void main()
{
        num=16;
        er=0;
        san=0;
        si=0;
        yi=0;
        while(1)
                {
                        num=keyscan();
                        P0=table[num];
                }
}
uchar keyscan()
{
while(1)
        {
                P1=0xfe;
                temp=P1;
                temp=temp&0xf0;
                while(temp!=0xf0)
                {
                        delay(5);
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {       
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xee:num=0;
                                                break;
                                        case 0xde:num=1;
                                                break;
                                        case 0xbe:num=2;
                                                break;
                                        case 0x7e:num=3;
                                                break;
                                }
                        while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }
                                }
                        }
                        P1=0xfd;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xed:num=4;
                                                        break;
                                                case 0xdd:num=5;
                                                        break;
                                                case 0xbd:num=6;
                                                        break;
                                                case 0x7d:num=7;
                                                        break;
                                        }
                                while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                        }
                        }
                        P1=0xfb;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xeb:num=8;
                                                        break;
                                                case 0xdb:num=9;
                                                        break;
                                                case 0xbb:num=10;
                                                        break;
                                                case 0x7b:num=11;
                                                        break;
                                        }
                                        while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                }
                        }
                        P1=0xf7;
                        temp=P1;
                        temp=temp&0xf0;
                        while(temp!=0xf0)
                        {
                                delay(5);
                                temp=P1;
                                temp=temp&0xf0;
                                while(temp!=0xf0)
                                {       
                                        temp=P1;
                                        switch(temp)
                                        {
                                                case 0xe7:num=12;
                                                        break;
                                                case 0xd7:num=13;
                                                        break;
                                                case 0xb7:num=14;
                                                        break;
                                                case 0x77:num=15;
                                                        break;
                                        }
                                while(temp!=0xf0)
                                        {
                                                temp=P1;
                                                temp=temp&0xf0;
                                        }
                                }
                        }
return num;
                }       
}
void delay(uchar z)
{
        uchar x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}

潜伏的鱼
3楼-- · 2019-07-16 06:32
你把子程序的返回值提前一下,和原来的地方对齐试试。PS:具体是不是这样,我没有验证,就要靠你了。要是不行,你可以试试把keil和proteus联调,单步运行,这样会发现为社么数码管会亮~

一周热门 更多>