CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

4x4 Keypad and 6 Seven Segment

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Nisar



Joined: 16 Nov 2007
Posts: 20

View user's profile Send private message

4x4 Keypad and 6 Seven Segment
PostPosted: Fri Mar 20, 2009 12:37 pm     Reply with quote

I want to display on seven segment when any key from keypad is pressed. i.e when I press key1 display show 1, key2 show 2 and so on. Plz anyone send me CCS C code example.

Regards
Nisar Ahmed
Nisar



Joined: 16 Nov 2007
Posts: 20

View user's profile Send private message

4x4 Keypad and 6 Seven Segment CCS C Code for every one
PostPosted: Tue Mar 31, 2009 12:10 pm     Reply with quote

Code:
//experiment purpose: familiar how to scan key matrix

//program without process key shake,not think multi-keys pressed at the same time too.

//the lowest Seven Segment display corresponding key( such as press K10 display 10,press K25 display 25)

//if no key pressed display FF

//key and key scan result corresponding
//   key       key scan  result        key      key scan  result
//   K1            0XE7                K09          0XB7
//   K2            0XEB                K10          0XBB
//   K3            0XED                K11          0XBD
//   K4            0XEE                K12          0XBE
//   K5            0XD7                K13          0X77
//   K6            0XDB                K14          0X7B
//   K7            0XDD                K15          0X7D
//   K8            0XDE                K16          0X7E


#include <16F877.h>                           // PIC16F877 header file
#use delay(clock=4000000)                       // for 4Mhz crystal
#fuses XT, NOWDT, NOPROTECT, NOLVP              // for debug mode
#byte port_a=5 /* define the location of register port_a */
#byte port_b=6 /* define the location of register port_b */
#byte port_c=7 /* define the location of register port_c */
#byte port_d=8 /* define the location of register port_d */



 int   result;
 void  delay();              //delay function declare
 void  init();               //I/O PORT initialize function declare
 void  scan();               //key scan function declare
 void  display(int x);       //display function declare
//---------------------------------------------------
                             //main program
void main()
{
  while(1)                   //circle work
    {
      init();                //call initialize subprogram
      scan();                //call key scan subprogram
      display(result);       //call result display subprogram
    }
 }

//---------------------------------------------------
//initialize function
void init()
 {
//  Set_tris_a(0X07);               // set A PORT general I/O PORT
  Set_tris_a(0X0F);                //A PORT low 4 bits INPUT,high 4 bits OUTPUT
  Set_tris_c(0XF0);                //C PORT high 4 bits INPUT,low 4 bits OUTPUT
  Set_tris_d(0X00);                //set D PORT OUTPUT
  Port_a=(0XFF);
  Port_d=(0XFF);                //clear all display
 }

//---------------------------------------------------
//key scan program
void scan()
{
 Port_C=(0XF7);                 //C3 OUTPUT low,the other 3 bits OUTPUT high
 delay_us(1);                 //delay
 result=PORT_C;               //read C PORT
 result=result&0xf0;         //clear low 4 bits
 if(result!=0xf0)            //judge if high 4 bits all 1(all 1 is no key press)
   {
     result=result|0x07;     //no,add low 4 bits 0x07 as key scan result
   }
 else                        //yes,change low 4 bits OUTPUT, judge if a key press again
   {
   Port_C=(0XFb);               //C2 OUTPUT low,the other 3 bits OUTPUT high
   delay_us(1);              //delay
   result=PORT_C;             //read C PORT
   result=result&0xf0;       //clear low 4 bits
   if(result!=0xf0)          //judge if high 4 bits all 1(all 1 is no key press)
     {
     result=result|0x0b;     //no,add low 4 bits 0x0b as key scan result
     }
   else                      //yes,change low 4 bits OUTPUT, judge if a key press again
       {
      Port_C=(0XFd);           //C1 OUTPUT low,the other 3 bits OUTPUT high
      delay_us(1);          //delay
       result=PORT_C;         //read C PORT
       result=result&0xf0;   //clear low 4 bits
       if(result!=0xf0)      //judge if high 4 bits all 1(all 1 is no key press)
        {
        result=result|0x0d;  //no,add low 4 bits 0x0d as key scan result
        }
       else                  //yes,change low 4 bits OUTPUT, judge if a key press again
          {
         Port_C=(0XFe);        //C0 OUTPUT low,the other 3 bits OUTPUT high
         delay_us(1);       //delay
          result=PORT_C;      //read C PORT
          result=result&0xF0;//clear low 4 bits
          if(result!=0xF0)   //judge if high 4 bits all 1(all 1 is no key press)
            {
             result=result|0x0e;//no,add low 4 bits 0x0e as key scan result
            }
          else               //yes,all key scan end,no key press,set no key press flag
            {
             result=0xFF;    //key scan result 0xff as no key press flag
            }
          }
      }
   }
 }

//----------------------------------------------------------
//display program
void   display(int x)
  {
   switch(result)
      {
       case 0xe7:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0xf9;PORT_A=0X2F;delay(); break;   //K10
       case 0xeb:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0xa4;PORT_A=0X2F;delay(); break;   //K11
       case 0xed:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0xb0;PORT_A=0X2F;delay(); break;   //K12
       case 0xee:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0x99;PORT_A=0X2F;delay(); break;   //K13
       case 0xd7:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0x92;PORT_A=0X2F;delay(); break;   //K14
       case 0xdb:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0x82;PORT_A=0X2F;delay(); break;   //K15
       case 0xdd:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0Xf8;PORT_A=0X2F;delay(); break;   //K16
       case 0xde:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0X80;PORT_A=0X2F;delay(); break;   //K17
       case 0xb7:
                 PORT_D=0xc0;PORT_A=0X1F;delay(); PORT_D=0X90;PORT_A=0X2F;delay(); break;   //K18
       case 0xbb:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0Xc0;PORT_A=0X2F;delay(); break;   //K19
       case 0xbd:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0xf9;PORT_A=0X2F;delay(); break;   //K20
       case 0xbe:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0xa4;PORT_A=0X2F;delay(); break;   //K21
       case 0x77:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0xb0;PORT_A=0X2F;delay(); break;   //K22
       case 0x7b:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0x99;PORT_A=0X2F;delay(); break;   //K23
       case 0x7d:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0x92;PORT_A=0X2F;delay(); break;   //K24
       case 0x7e:
                 PORT_D=0xf9;PORT_A=0X1F;delay(); PORT_D=0x82;PORT_A=0X2F;delay(); break;   //K25
       case 0xff:
                 PORT_D=0x8e;PORT_A=0X1F;delay();  PORT_D=0x8e; PORT_A=0X2F; delay();       //no key press
      }
   }

//------------------------------------------------------------------
//delay program
void  delay()              //delay program
    {
     int i;                 //define integer variable
     for(i=0x50;i--;);     //delay
    }


Regards
Nisar ahmed
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Tue Mar 31, 2009 6:43 pm     Reply with quote

Nisar,
Have you posted this in the code library? I'm sure some folks would appreciate it.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Apr 01, 2009 11:02 am     Reply with quote

Congratulations on being the only person ever to post code here with any attention given to clear formatting! All right, maybe the second person, or the third.

I hope you have pullup resistors on the input pins of Port C, because if you don't, there's no expectation that the input pins will test high when no button is pressed; they'll just be a floating level. If you used Port B instead, you could use the internal pullups and not have this issue.

If a user presses two buttons at once, you may have a situation of two outputs fighting with each other. You could avoid this by setting all the bits in the output register low, but use the TRIS register to make only one pin at a time actually be an output.
otacon



Joined: 12 Nov 2010
Posts: 3

View user's profile Send private message

PostPosted: Fri Nov 12, 2010 10:18 pm     Reply with quote

There is something wrong with this code, port A always not in low or high(emulation by proteus). How about solve?
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 6:18 am     Reply with quote

re:
problem ... Port A in Proteus simulation not working correctly.

solution..... DO NOT trust Proteus ! It is a simulator NOT the real world !

That being said, try a very,very simple program with the PORT A problem and if it fails, get rid of Proteus. If it works, reduce this program to just the Port A code and simulate it. If it fails compare the two programs to see what is different between them.

In over 35 years of programming, I've nevere found a simulator that I'd trust your life to !
otacon



Joined: 12 Nov 2010
Posts: 3

View user's profile Send private message

PostPosted: Sun Nov 14, 2010 9:57 pm     Reply with quote

I have a source can run in Proteus as well,it very short,it's 5x5 key board.Read from PortC and PortD,display resuilt in led 7s driver by Port B and PortE. It's can be anti shaking and have keypressed/keyrelease feature
This is Sub:
//////////////////////////////////////////
int rotate,n,k,col,key_value,key_release;
void read_key()
{
output_C(0xff);
rotate = 0xfe;
for(n=1;n<6;++n) //n la gia tri cot
{
output_D(rotate);
delay_us(10);
col=portC; //doc vao tu PortC
if(col!=0xff)
goto have_key1;
else
rotate=rotate<<1; //dich trai 1 bit
}
key_release=0xff;
key_value=0;
goto let_out;
have_key1:
if(key_release==0xff)
{
for(k=1;k<6;k++) //k la gia tri hang
{
if(!bit_test(col,k-1))
{
key_value=((k-1)*5+(n-1)+1); //gia tri phim bam tu 1-25
key_release=0x00; //keypressed
}
}
}
let_out:
delay_us(5);
}
//////////////////////////////////////////
I have a Proteus File,but i don't know how to upload here.If you need,PM for me!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group