初级会员 
  
	- 积分
 - 68
 
        - 金钱
 - 68 
 
       - 注册时间
 - 2018-8-21
 
      - 在线时间
 - 11 小时
 
 
 
 | 
 
1金钱 
请问为什么我下面这个程序,只有矩阵键盘的第一行正确输出了,第二三四行都单片机没有输出? 
在二三四行的程序中加入P1口的代码,用来点亮发光二极管,结果发光二极管也没有亮。 
 
但是有关矩阵键盘的程序,我单独运行以前编写的,还是可以正确运行的, 
下面程序中矩阵键盘的程序就是我从之前程序里复制过来修改的。 
 
//4.按下矩阵键盘第一行时以1200bps发送,1,2,3,4, 
//第二行时以2400bps发送5,6,7,8, 
//第三行以4800bps发送,9,10,11,12, 
//第四行以9600pbs 发送,13,14,15,16. 
 
#include<reg52.h> 
#define uint unsigned int 
#define uchar unsigned char 
 
void delayms(uint xms) 
{ 
        uint i,j; 
        for(i=xms;i>0;i--)//i=xms即延时约xms毫秒 
                for(j=110;j>0;j--); 
} 
 
void init() 
{ 
        TMOD=0X20; 
        SM1=1; 
        SM0=0; 
        REN=1; 
        EA=1; //!!! 
} 
 
void matrixkeyscan()                  //矩阵键盘代码 
{ 
        uchar temp; 
        P3=0xfe;                          //第一行送入低电平 
        temp=P3; 
        temp=temp&0xf0;              
        if(temp!=0xf0)                      
        { 
                delayms(10);             //去抖动 
                temp=P3; 
                temp=temp&0xf0;                 
                if(temp!=0xf0) 
                { 
                        TH1=0xe8; 
                        TL1=0xe8; 
                        TR1=1;          
                        temp=P3; 
                        switch(temp) 
                        { 
                                case 0xee:                  //1110 1110  一行一列  
                                        SBUF='1'; 
                                        break; 
                                case 0xde:                  //1101 1110 一行二列 
                                        SBUF='2'; 
                                        break; 
                                case 0xbe: 
                                        SBUF='3'; 
                                        break; 
                                case 0x7e: 
                                        SBUF='4'; 
                                        break;                         
                         } 
                         while(!TI); 
                         TI=0; 
                        while(temp!=0xf0)                  //等待按键释放 
                        { 
                                temp=P3; 
                                temp=temp&0xf0; 
                        } 
                        TR1=0;                    
                } 
        } 
 
        P3=0xfd;                               //第二行 
        temp=P3; 
        temp=temp&0xf0; 
        if(temp!=0xf0) 
        { 
                delayms(10);           //去抖动 
                temp=P3; 
                temp=temp&0xf0; 
                if(temp!=0xf0) 
                { 
                        TH1=0xf4; 
                        TL1=0xf4; 
                        TR1=1; 
                        switch(temp) 
                        { 
                                case 0xed: 
                                        SBUF='5'; 
                                        break; 
                                case 0xdd: 
                                        SBUF='6'; 
                                        break; 
                                case 0xbd: 
                                        SBUF='7'; 
                                        break; 
                                case 0x7d: 
                                        SBUF='8'; 
                                        break;                 
                        }                 
                        while(!TI); 
                        TI=0;         
                 
                        while(temp!=0xf0) 
                        { 
                                temp=P3; 
                                temp=temp&0xf0; 
                        } 
                        TR1=0;//!!!!!!!!!!!! 
                } 
        } 
 
        P3=0xfb; 
        temp=P3; 
        temp=temp&0xf0; 
        if(temp!=0xf0) 
        { 
                delayms(10);                 //去抖动 
                temp=P3; 
                temp=temp&0xf0; 
                if(temp!=0xf0) 
                { 
                        TH1=0xfa; 
                        TL1=0xfa; 
                        TR1=1; 
                        switch(temp) 
                        { 
                                case 0xeb: 
                                        SBUF='9'; 
                                        break; 
                                case 0xdb: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=1; 
                                        SBUF='0'; 
                                        break; 
                                case 0xbb: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=1; 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=1; 
                                        break; 
                                case 0x7b: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=1; 
                                        SBUF='2'; 
                                        break;                 
                        }                         
                        while(!TI); 
                        TI=0; 
                        while(temp!=0xf0)                 //等待按键释放 
                        { 
                                temp=P3; 
                                temp=temp&0xf0; 
                        } 
                        TR1=0; 
                } 
        } 
 
        P3=0xf7; 
        temp=P3; 
        temp=temp&0xf0; 
        if(temp!=0xf0) 
        { 
                delayms(10);                    //去抖动 
                temp=P3; 
                temp=temp&0xf0; 
                if(temp!=0xf0) 
                { 
                        TH1=0xfd; 
                        TL1=0xfd; 
                        TR1=1; 
                        switch(temp) 
                        { 
                                case 0xe7: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=0; 
                                        SBUF='3'; 
                                        break; 
                                case 0xd7: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=0; 
                                        SBUF='4'; 
                                        break; 
                                case 0xb7: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=0; 
                                        SBUF='5'; 
                                        break; 
                                case 0x77: 
                                        SBUF='1'; 
                                        while(!TI); 
                                        TI=0; 
                                        SBUF='6'; 
                                        break;                 
                        }                         
                        while(!TI); 
                        TI=0; 
                        while(temp!=0xf0) 
                        { 
                                temp=P3; 
                                temp=temp&0xf0; 
                        } 
                } 
                TR1=0; 
        } 
} 
void main() 
{ 
        init(); 
        while(1) 
        { 
                matrixkeyscan(); 
        } 
} 
 
 |   
 
 
 
- 
 
 
 
 
最佳答案
查看完整内容[请看2#楼] 
uchar temp;
        P3=0xfe;                          //第一行送入低电平
        temp=P3;
        temp=temp&0xf0;             
        if(temp!=0xf0)                     
        {
                delayms(10);             //去抖动
                temp=P3;
                temp=temp&0xf0;                
                if(temp!=0xf0)           ; 
 
 
 
 
 
 
 |