This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Matrix keyboard interfacing

we are interfacing 3x4 matrix keyboard and interfacing it with port 1 of P89C51RD2 microcontroller.
We are connecting column to P1.0 to P1.3 and row to P1.4 to P1.6.
could anybody have code in C

Parents
  • Thanks for replying
    i tried the key board, my logic is correct but when i m fusing the only last rows keys are working here i m sending my code actually here i m connetcing 12 switch 3x4 matrix to mc.Rows are to P1.6,P1.5,P1.4 and columns are to lower nibble of P1 and port0 to respective LED for checking .please find mistake and do correction in that.waiting for ur +ve response,thanks
    ////////////


    #include <stdio.h>     /*standared inclide file for main and other c functions */
    #include <AT89X51.H>
    
    unsigned char var_keynumber;           //for key indentification
    
    #pragma OPTIMIZE(6)                        //for reitteration
    #pragma SAVE
    void delay(void)     //delay function for debaunse 20ms
     {
         TMOD=01; //        ;timer 0 ,mode 1
         TL0=0xE0;
         TH0=0xB1;
         TR0=1;
      again:
         if (TF0!=1) goto again;                //if (TF0!=1)
         TR0=0;       //;stoP timer
         TF0=0;     // ;clear tf0 flag
    }
    
    #pragma RESTORE
    
    //start the Program//////////////////////////////////////////////////////////
    void main(void)
      {
             EA=0;              // disable interrupt*/
        	 P1 =0x0F;          //set port 1 as input
    		 SP=0x07;           //initialize stack pointer
        while(1)                  //loop forever
          {
    
     	 #pragma asm
             push ACC         //save accumulator and flag
    	     push PSW
         #pragma endasm
    		 P0=0x00;            //initially all output leds are off
    		 P2=0x00;            //initially all output leds are off
    
    		 P1_4=0;     //mask lower bit ground all the rows
             P1_5=0;
             P1_6=0;
    
    //checking previous key is released or not
    
      nadREL:
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
    		 delay();
    		 else
    		 goto nadREL;
    
    //checking for new key is pressed or not
      nadNEW:
            if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
    		 goto nadNEW;
    		 else
    		 delay();
    
    
      //scan for pressed key
    
    P1_4=0;     //row 0
    P1_5=1;
    P1_6=1;
     P1_0=0;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=0;goto LAST;
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=1;goto LAST;
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=2;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=3;goto LAST;
    
    
    P1_4=1;     //row 1
    P1_5=0;
    P1_6=1;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=4;goto LAST;
    
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=5;goto LAST;
    
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=6;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=7;goto LAST;
    
    
    P1_4=1;      //row 2
    P1_5=1;
    P1_6=0;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=8;goto LAST;
    
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=9;goto LAST;
    
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=10;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=11;goto LAST;
     goto nadNEW     //if error then check again
    
    LAST:
    switch(var_keynumber)
          {
    
    case 0:P0_0=1; break;
    case 1:P0_1=1; break;
    case 2:P0_2=1; break;
    case 3:P0_3=1; break;
    case 4:P0_4=1; break;
    case 5:P0_5=1; break;
    case 6:P0_6=1; break;
    case 7:P0_7=1; break;
    case 8:P2_6=1; break;
    case 9:P2_7=1; break;
    case 10:P2_5=1;break;
    case 11:P2_4=1;break;
          #pragma asm
            Pop ACC         //save accumulator and flag from stack
    	  Pop PSW
     	 #pragma endasm
         }          //switch end
    
    
    
    
    
    	   }            //while end
    
     }                      //main end
    
    ///////////////////////////////////////////////////////////////////////////
    
    
    
    

Reply
  • Thanks for replying
    i tried the key board, my logic is correct but when i m fusing the only last rows keys are working here i m sending my code actually here i m connetcing 12 switch 3x4 matrix to mc.Rows are to P1.6,P1.5,P1.4 and columns are to lower nibble of P1 and port0 to respective LED for checking .please find mistake and do correction in that.waiting for ur +ve response,thanks
    ////////////


    #include <stdio.h>     /*standared inclide file for main and other c functions */
    #include <AT89X51.H>
    
    unsigned char var_keynumber;           //for key indentification
    
    #pragma OPTIMIZE(6)                        //for reitteration
    #pragma SAVE
    void delay(void)     //delay function for debaunse 20ms
     {
         TMOD=01; //        ;timer 0 ,mode 1
         TL0=0xE0;
         TH0=0xB1;
         TR0=1;
      again:
         if (TF0!=1) goto again;                //if (TF0!=1)
         TR0=0;       //;stoP timer
         TF0=0;     // ;clear tf0 flag
    }
    
    #pragma RESTORE
    
    //start the Program//////////////////////////////////////////////////////////
    void main(void)
      {
             EA=0;              // disable interrupt*/
        	 P1 =0x0F;          //set port 1 as input
    		 SP=0x07;           //initialize stack pointer
        while(1)                  //loop forever
          {
    
     	 #pragma asm
             push ACC         //save accumulator and flag
    	     push PSW
         #pragma endasm
    		 P0=0x00;            //initially all output leds are off
    		 P2=0x00;            //initially all output leds are off
    
    		 P1_4=0;     //mask lower bit ground all the rows
             P1_5=0;
             P1_6=0;
    
    //checking previous key is released or not
    
      nadREL:
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
    		 delay();
    		 else
    		 goto nadREL;
    
    //checking for new key is pressed or not
      nadNEW:
            if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
    		 goto nadNEW;
    		 else
    		 delay();
    
    
      //scan for pressed key
    
    P1_4=0;     //row 0
    P1_5=1;
    P1_6=1;
     P1_0=0;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=0;goto LAST;
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=1;goto LAST;
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=2;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=3;goto LAST;
    
    
    P1_4=1;     //row 1
    P1_5=0;
    P1_6=1;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=4;goto LAST;
    
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=5;goto LAST;
    
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=6;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=7;goto LAST;
    
    
    P1_4=1;      //row 2
    P1_5=1;
    P1_6=0;
    if((P1_3==1)&&(P1_2==1)&&(P1_1==1)&&(P1_0==0))
     var_keynumber=8;goto LAST;
    
    else if((P1_3==1)&&(P1_2==1)&&(P1_1==0)&&(P1_0==1))
     var_keynumber=9;goto LAST;
    
    else if((P1_3==1)&&(P1_2==0)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=10;goto LAST;
    
    else if((P1_3==0)&&(P1_2==1)&&(P1_1==1)&&(P1_0==1))
     var_keynumber=11;goto LAST;
     goto nadNEW     //if error then check again
    
    LAST:
    switch(var_keynumber)
          {
    
    case 0:P0_0=1; break;
    case 1:P0_1=1; break;
    case 2:P0_2=1; break;
    case 3:P0_3=1; break;
    case 4:P0_4=1; break;
    case 5:P0_5=1; break;
    case 6:P0_6=1; break;
    case 7:P0_7=1; break;
    case 8:P2_6=1; break;
    case 9:P2_7=1; break;
    case 10:P2_5=1;break;
    case 11:P2_4=1;break;
          #pragma asm
            Pop ACC         //save accumulator and flag from stack
    	  Pop PSW
     	 #pragma endasm
         }          //switch end
    
    
    
    
    
    	   }            //while end
    
     }                      //main end
    
    ///////////////////////////////////////////////////////////////////////////
    
    
    
    

Children
No data