Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me write a program for detecting and decoding a keypad

Status
Not open for further replies.

MRFGUY

Full Member level 1
Joined
Sep 16, 2003
Messages
98
Helped
9
Reputation
18
Reaction score
3
Trophy points
1,288
Activity points
1,049
keypad scan

I just try to write a program which detect the 3x4 keypad and decode. I try to learn xilinx application xapp512__verilog.zip, but I still confuse how to write this program. Please show me links or examples (but please do not advise me to use google to search).

Thanks.
 

Re: keypad scan

Can you post here what you tried??
I will try to correct it!
 

Re: keypad scan

I just learn that I need to use pull up resistors. So I plan to use at row and I assign column as output. But still wondering how to scan the key press.

module keypadscan(reset, clk, row, column, out);
// program for 4x3 keypad
input reset, clk;
input [3:0] row;
output [2:0] column;
output [3:0] out; //decoded output

reg [3:0] out;

always@(row or column)

case ({row,column})
7'b0001_001 : out = 4'b0001;
7'b0001_010 : out = 4'b0010;
7'b0001_100 : out = 4'b0011;

7'b0010_001 : out = 4'b0100;
7'b0010_010 : out = 4'b0101;
7'b0010_100 : out = 4'b0110;

7'b0100_001 : out = 4'b0111;
7'b0100_010 : out = 4'b1000;
7'b0100_100 : out = 4'b1001;

7'b1000_001 : out = 4'b1110; // decode as E
7'b1000_010 : out = 4'b0000;
7'b1000_100 : out = 4'b1111; // decode as F

default : out = 4'b0000;

endcase

//scanning keypad

always@ (posedge clk or posedge reset)

if (reset) out<=0;

else // this part I can not think

endmodule
 

Re: keypad scan

Here is the corrected code!!
You need to add one more o/p pin to tell key is pressed for this
you can take out scan_en.

Code:
module keypadscan(reset, 
                  clk, 
                  row, 
                  column, 
                  out);
   // program for 4x3 keypad
   input  reset, clk;
   output [3:0] row; 
   input  [2:0] column; //pullup to vcc 
   output [3:0] out;    //decoded output

   reg  [3:0]  row;
   reg  [3:0]  out;

   wire        scan_en = &column;
   
   //scanning keypad
   always @(posedge clk or posedge reset)
     if (reset) 
       row <= 4'b1110;
     else if (scan_en)
       row <= {row[2:0], row[3]};
   
               
   always @(row or column) begin
      out = 4'b0000;
      case ({row,column})
        7'b1110_110 : out = 4'b0001;
        7'b1110_101 : out = 4'b0010;
        7'b1110_011 : out = 4'b0011;
        
        7'b1101_110 : out = 4'b0100;
        7'b1101_101 : out = 4'b0101;
        7'b1101_011 : out = 4'b0110;
        
        7'b1011_110 : out = 4'b0111;
        7'b1011_101 : out = 4'b1000;
        7'b1011_011 : out = 4'b1001;
        
        7'b0111_110 : out = 4'b1110; // decode as E
        7'b0111_101 : out = 4'b0000;
        7'b0111_011 : out = 4'b1111; // decode as F
      endcase // case({row,column})
   end // always @ (row or column)
endmodule // keypadscan
 

Re: keypad scan

Can I directly connect the keypad pin to cpld.

When I connect like this the 7-seg show only 0 all the time.

Thanks
 

Re: keypad scan

Go thro' the xapp512__verilog.zip data base you will find UCF file in this you will see
following lines...
NET "column<0>" PULLUP;
NET "column<1>" PULLUP;
NET "column<2>" PULLUP;
NET "column<3>" PULLUP;
NET "column<4>" PULLUP;
NET "column<5>" PULLUP;
NET "column<6>" PULLUP;
NET "column<7>" PULLUP;

You need to include similar lines in ur ucf file then you can connect ur
keyboard directly to CPLD. Try this and let us know!
 

Re: keypad scan

my ucf file doesn't have the pull up . It contain only :
#PACE: Start of Constraints generated by PACE

#PACE: Start of PACE I/O Pin Assignments
NET "clk" LOC = "P5" ;
NET "column<0>" LOC = "P1" ;
NET "column<1>" LOC = "P2" ;
NET "column<2>" LOC = "P3" ;
NET "out<0>" LOC = "P18" ;
NET "out<1>" LOC = "P19" ;
NET "out<2>" LOC = "P20" ;
NET "out<3>" LOC = "P22" ;
NET "reset" LOC = "P39" ;
NET "row<0>" LOC = "P4" ;
NET "row<1>" LOC = "P44" ;
NET "row<2>" LOC = "P8" ;
NET "row<3>" LOC = "P9" ;

#PACE: Start of PACE Area Constraints

#PACE: Start of PACE Prohibit Constraints

#PACE: End of Constraints generated by PACE

Should I key in myself those colum pull up.

thanks
 

Re: keypad scan

Yes you add the following lines to ur ucf file manually...
Code:
NET "column<0>" PULLUP;
NET "column<1>" PULLUP;
NET "column<2>" PULLUP;
 

Re: keypad scan

After keying in, I faced some warnings during "generate programming file".

WARNING:Cpld:1258 - Invalid constraint 'PULLUP' found in netlist. The
WARNING:Cpld:1258 - Invalid constraint 'PULLUP' found in netlist. The
WARNING:Cpld:1258 - Invalid constraint 'PULLUP' found in netlist. The

I also get warnings when open "assign package pin"



WARNING:pace - Constraint 'PULLUP' with value 'PULLUP' for object 'column<0>' found in UCF file is not valid in the target device.
WARNING:pace - Constraint 'PULLUP' with value 'PULLUP' for object 'column<1>' found in UCF file is not valid in the target device.
WARNING:pace - Constraint 'PULLUP' with value 'PULLUP' for object 'column<2>' found in UCF file is not valid in the target device.
ERROR:pace:3 - DRC errors or warnings found. Please run Tools->Run Design Rule Check to look at these messages

My cpld is XC9572.

thanks
 

Re: keypad scan

Then in that case you need to add external pullup resistors!
 

Re: keypad scan

Can I use 1k between o/p port and VCC as an external pullup.
 

Re: keypad scan

Use 4k7 resistors for pullup.
 

keypad scan

you must add pullup resistors. try 4k7
 

Re: keypad scan

How about the row (actually these are output ports). When I try to connect directly to keypad rows (it seem ok), but it works only one time (it can scan all key once) and later this ports look like high all the time ( may be demage, I have to test again by downloading other program)
 

Re: keypad scan

I try to change to new cpld (xc9572) with same program and all rows connect directly with keypad. Use 4.7 k for pullup. It seem ok for some time. When I press about 10 times later all output go to 0.

If I shut down the power and siwtch on it works again like 10 times with no error. After that all output go to 0 again.

So I need to power on and off during testing keypad. What sould be that error.

I used the coding posted by nand_gates.
 

Re: keypad scan

This is really strange!! We need to debug this..
Do one thing output status of row and column on LED for this
you need to modify the code. Do this and let us know whats the
status on LED's when the keyboard gets lock!
 

keypad scan

That weird behavior sounds like floating inputs.
Are you sure you added your pullup resistors to the input pins, and not to the output pins?

The XC9500 family unfortunately does not provide configurable pullups or pulldowns.
 

Re: keypad scan

Dear nand_gates:

Actually I just one more variable name shiftreg like app. note xapp512 in your code. It look similar to me and it is just a one more variable. But I faced some problems.

When I take out this code from my program it is working properly. My key pad never hang.

my extra code is:
reg [3:0] shiftreg=4'b1110;

and in the always block

always @(posedge clk )

begin
if (reset)
shiftreg<=4'b0000;

else if (scan_en)
shiftreg <= {shiftreg[2:0], shiftreg[3]};
end


assign row= shiftreg;

Do you have any idea which lead to the error.



nand_gates said:
This is really strange!! We need to debug this..
Do one thing output status of row and column on LED for this
you need to modify the code. Do this and let us know whats the
status on LED's when the keyboard gets lock!
 

keypad scan

Were you applying a reset pulse to nand_gates example? It needs one. Beware that it's difficult to apply an asynchronous reset if the clock is running at high frequency.

You avoided the reset requirement by initializing shiftreg. That's good. However, you now clear shiftreg to zero when you get a reset pulse. That's bad.


How fast is your clock? You may have trouble scanning a keyboard at a jillion megahertz due to capacitive coupling crosstalk.
 

Re: keypad scan

Dear nand_gates and echo47,

I just try to test my circuit according to both of your advice. I found that my circuit is working properly with pull down circuit (with pull down program code). I still don't know what is the reason behind that.

I just found that my programs are working olny at very low frequency (only around at 100 Hz). If I increase the frequency (even khz range) the keypad is lock after 3-4 times press and all 4 bits output are zero (for both pull up and pull down program)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top