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

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;       
}
请指教!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。