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

Interfacing Seven Segment Display.

Hi all..
I am trying to interface keypad and Seven Segment display with 89S52.
what i need to do is whatever keys are pressed from keypad, it should show on seven segment display. for Eg. if 2 is pressed from keypad then display should show 2. and so on.
my code is...

#include<reg51.h>
#include<stdio.h>

sbit row0=P2^7; //assigning PORT-0 to read rows
sbit row1=P2^6; //assigning PORT-0 to read rows
sbit row2=P2^5; //assigning PORT-0 to read rows
sbit row3=P2^4; //assigning PORT-0 to read rows
sfr COL=0xA0;   //assigning PORT-2 to read colomns

sbit buz=P3^2;

sbit seg0=P3^0;
sbit seg1=P3^1;
sbit seg2=P3^2;
sbit seg3=P3^3;
sbit seg4=P3^4;
sbit seg5=P3^5;

void msdelay(unsigned int value);
int keypad();

char digi[10]={0x40,0xF9,0x24,0x30,0x19,0x12,0x02,0xF8,0x00,0x10};

unsigned char numbers[6]={"44444"};

void main()
{
unsigned int i;
while(1)
  {
   do
   {
    numbers[i]=keypad();
    i++;
   }
   while(i!=6);
   i=0;
    {
     P1=0x40;
    }
   }

}                       // MAIN ENDS HERE.



void msdelay(unsigned int value)
{
 unsigned int i,j;
 for(i=0;i<value;i++)
 for(j=0;j<100;j++);
}

int keypad()
{
        unsigned char dat[4][4]={'7','8','9','A',        // assigning key matrix
                                 '4','5','6','B',
                                 '1','2','3','-',
                                 '*','0','#','+',};

 unsigned char colloc,rowloc;
 COL=0x0F;
 row0=0;
 row1=0;
 row2=0;
 row3=0;
 while(1)
 {
  do
   {
    do
     {
      msdelay(90);
      colloc=COL;
      colloc&=0x0F;
     }
   while(colloc==0x0F);
   msdelay(90);
   colloc=COL;
   colloc&=0x0F;
   }
  while(colloc==0x0F);
  while(1)
  {
        row0=0;
        row1=1;
        row2=1;
        row3=1;
        msdelay(5);
        colloc=COL;
        colloc&=0x0F;
        if(colloc!=0x0F)
        {
         rowloc=0;
         break;
        }
         row0=1;
         row1=0;
         row2=1;
         row3=1;
         msdelay(5);
         colloc=COL;
         colloc&=0x0F;
         if(colloc!=0x0F)
         {
          rowloc=1;
          break;
         }
         row0=1;
         row1=1;
         row2=0;
         row3=1;
         msdelay(5);
         colloc=COL;
         colloc&=0x0F;
         if(colloc!=0x0F)
          {
           rowloc=2;
           break;
          }
        row0=1;
        row1=1;
        row2=1;
        row3=0;
        msdelay(5);
        colloc=COL;
        colloc&=0x0F;
        if(colloc!=0x0F)
        {
         rowloc=3;
         break;
        }
  }
if(colloc==0x0E)
  return(dat[rowloc][0]);
else if(colloc==0x0D)
  return(dat[rowloc][1]);
else if(colloc==0x0B)
  return(dat[rowloc][2]);
else
  return(dat[rowloc][3]);
}
}


i have not tried anything because i am not getting any idea of how to start with...
because the output from keypad i get is numbers. and to display on seven segment we need hex.
how can i do this.
in my code, in MAIN function i have just tried to save the numbers from what i get from keypad to array numbers[7].
seven segment display are common anode and are connected to port 1 of 89S52.

Parents
  • "because the output from keypad i get is numbers. and to display on seven segment we need hex."

    You think hexadecimal values aren't numbers???

    Next thing - your 7-segment code can display digits 0..9. But your keypad code isn't limited to just picking up digits. What did you intend to happen if someone presses any of +, -, *, #, A, B?

    Another thing - don't you believe in giving variables initial values?

    void main() {
        unsigned int i;      <==== No initial value given
        while (1) {
           do {
               ************ What value have i received when you reach here the first time???
               numbers[i]=keypad();
               ************ What happens if a '+' was pressed? That isn't a digit.
               i++;
           } while(i!=6);
           i=0;
           { <========= Is this block just an arbitrary decoration?
               P1=0x40;      <==== Avoid magic assigns. Who will know what this assign does?
           }
        }
    }
    

    You need to decide what you want to solve. And not just solve "happy path" but take into account everything that can happen. Any button can be pressed. Or the user might tire in the middle of the input and stop pressing more buttons. Or the user may press multiple buttons in same row or in same column - possibly later releasing some of the buttons until they end up pressing just a single button. What is your rule for how to handle that? What would you want your phone to do when you enter a pin code and presses wrong?

    Your keypad() function has no way to report a failure to receive a valid key press. Is that a good choice?

    sbit row0=P2^7; //assigning PORT-0 to read rows
    sbit row1=P2^6; //assigning PORT-0 to read rows
    sbit row2=P2^5; //assigning PORT-0 to read rows
    sbit row3=P2^4; //assigning PORT-0 to read rows
    


    Are you really assigning a use for the full port? Aren't you mapping individual pins on the port? Because your comments doesn't really match the code.

    sfr COL=0xA0;   //assigning PORT-2 to read colomns
    

    You needed a comment to mention port 2 - wasn't there any symbol available in the 8051 header file that would have allowed you to not use the magic value 0xA0 in your code?

    Spend more time with your code. Use _your_ time solving the issues. It's when _you_ spend time trying to solve the issues that shows up that you will learn.

Reply
  • "because the output from keypad i get is numbers. and to display on seven segment we need hex."

    You think hexadecimal values aren't numbers???

    Next thing - your 7-segment code can display digits 0..9. But your keypad code isn't limited to just picking up digits. What did you intend to happen if someone presses any of +, -, *, #, A, B?

    Another thing - don't you believe in giving variables initial values?

    void main() {
        unsigned int i;      <==== No initial value given
        while (1) {
           do {
               ************ What value have i received when you reach here the first time???
               numbers[i]=keypad();
               ************ What happens if a '+' was pressed? That isn't a digit.
               i++;
           } while(i!=6);
           i=0;
           { <========= Is this block just an arbitrary decoration?
               P1=0x40;      <==== Avoid magic assigns. Who will know what this assign does?
           }
        }
    }
    

    You need to decide what you want to solve. And not just solve "happy path" but take into account everything that can happen. Any button can be pressed. Or the user might tire in the middle of the input and stop pressing more buttons. Or the user may press multiple buttons in same row or in same column - possibly later releasing some of the buttons until they end up pressing just a single button. What is your rule for how to handle that? What would you want your phone to do when you enter a pin code and presses wrong?

    Your keypad() function has no way to report a failure to receive a valid key press. Is that a good choice?

    sbit row0=P2^7; //assigning PORT-0 to read rows
    sbit row1=P2^6; //assigning PORT-0 to read rows
    sbit row2=P2^5; //assigning PORT-0 to read rows
    sbit row3=P2^4; //assigning PORT-0 to read rows
    


    Are you really assigning a use for the full port? Aren't you mapping individual pins on the port? Because your comments doesn't really match the code.

    sfr COL=0xA0;   //assigning PORT-2 to read colomns
    

    You needed a comment to mention port 2 - wasn't there any symbol available in the 8051 header file that would have allowed you to not use the magic value 0xA0 in your code?

    Spend more time with your code. Use _your_ time solving the issues. It's when _you_ spend time trying to solve the issues that shows up that you will learn.

Children
No data