Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Interfacing 4*4 keypad with DE2 board

Hi Everyone,

I am doing a project in which i has to interface a keypad . the project is such that when i pressed any alphabet on the keypad the respective alphabet will be shown on the LCD Please help me in this respect

Regards

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    module matrixKeyboard_drive(
      input            i_clk,
      input            i_rst_n,
      input       row,                 // 矩阵键盘 行
      output reg  col,                 // 矩阵键盘 列
      output reg  keyboard_val         // 键盘值     
    );
    //++++++++++++++++++++++++++++++++++++++
    // 分频部分 开始
    //++++++++++++++++++++++++++++++++++++++
    reg  cnt;                         // 计数子
    always @ (posedge i_clk, negedge i_rst_n)
      if (!i_rst_n)
        cnt <= 0;
      else
        cnt <= cnt + 1'b1;
    wire key_clk = cnt;                // (2^20/50M = 21)ms 
    //--------------------------------------
    // &#20998;&#39057;&#37096;&#20998; &#32467;&#26463;
    //--------------------------------------
    //++++++++++++++++++++++++++++++++++++++
    // &#29366;&#24577;&#26426;&#37096;&#20998; &#24320;&#22987;
    //++++++++++++++++++++++++++++++++++++++
    // &#29366;&#24577;&#25968;&#36739;&#23569;&#65292;&#29420;&#28909;&#30721;&#32534;&#30721;
    parameter NO_KEY_PRESSED = 6'b000_001;  // &#27809;&#26377;&#25353;&#38190;&#25353;&#19979;  
    parameter SCAN_COL0      = 6'b000_010;  // &#25195;&#25551;&#31532;0&#21015; 
    parameter SCAN_COL1      = 6'b000_100;  // &#25195;&#25551;&#31532;1&#21015; 
    parameter SCAN_COL2      = 6'b001_000;  // &#25195;&#25551;&#31532;2&#21015; 
    parameter SCAN_COL3      = 6'b010_000;  // &#25195;&#25551;&#31532;3&#21015; 
    parameter KEY_PRESSED    = 6'b100_000;  // &#26377;&#25353;&#38190;&#25353;&#19979;
    reg  current_state, next_state;    // &#29616;&#24577;&#12289;&#27425;&#24577;
    always @ (posedge key_clk, negedge i_rst_n)
      if (!i_rst_n)
        current_state <= NO_KEY_PRESSED;
      else
        current_state <= next_state;
    // &#26681;&#25454;&#26465;&#20214;&#36716;&#31227;&#29366;&#24577;
    always @ *
      case (current_state)
        NO_KEY_PRESSED :                    // &#27809;&#26377;&#25353;&#38190;&#25353;&#19979;
            if (row != 4'hF)
              next_state = SCAN_COL0;
            else
              next_state = NO_KEY_PRESSED;
        SCAN_COL0 :                         // &#25195;&#25551;&#31532;0&#21015; 
            if (row != 4'hF)
              next_state = KEY_PRESSED;
            else
              next_state = SCAN_COL1;
        SCAN_COL1 :                         // &#25195;&#25551;&#31532;1&#21015; 
            if (row != 4'hF)
              next_state = KEY_PRESSED;
            else
              next_state = SCAN_COL2;    
        SCAN_COL2 :                         // &#25195;&#25551;&#31532;2&#21015;
            if (row != 4'hF)
              next_state = KEY_PRESSED;
            else
              next_state = SCAN_COL3;
        SCAN_COL3 :                         // &#25195;&#25551;&#31532;3&#21015;
            if (row != 4'hF)
              next_state = KEY_PRESSED;
            else
              next_state = NO_KEY_PRESSED;
        KEY_PRESSED :                       // &#26377;&#25353;&#38190;&#25353;&#19979;
            if (row != 4'hF)
              next_state = KEY_PRESSED;
            else
              next_state = NO_KEY_PRESSED;                      
      endcase
    reg       key_pressed_flag;             // &#38190;&#30424;&#25353;&#19979;&#26631;&#24535;
    reg  col_val, row_val;             // &#21015;&#20540;&#12289;&#34892;&#20540;
    // &#26681;&#25454;&#27425;&#24577;&#65292;&#32473;&#30456;&#24212;&#23492;&#23384;&#22120;&#36171;&#20540;
    always @ (posedge key_clk, negedge i_rst_n)
      if (!i_rst_n)
      begin
        col              <= 4'h0;
        key_pressed_flag <=    0;
      end
      else
        case (next_state)
          NO_KEY_PRESSED :                  // &#27809;&#26377;&#25353;&#38190;&#25353;&#19979;
          begin
            col              <= 4'h0;
            key_pressed_flag <=    0;       // &#28165;&#38190;&#30424;&#25353;&#19979;&#26631;&#24535;
          end
          SCAN_COL0 :                       // &#25195;&#25551;&#31532;0&#21015;
            col <= 4'b1110;
          SCAN_COL1 :                       // &#25195;&#25551;&#31532;1&#21015;
            col <= 4'b1101;
          SCAN_COL2 :                       // &#25195;&#25551;&#31532;2&#21015;
            col <= 4'b1011;
          SCAN_COL3 :                       // &#25195;&#25551;&#31532;3&#21015;
            col <= 4'b0111;
          KEY_PRESSED :                     // &#26377;&#25353;&#38190;&#25353;&#19979;
          begin
            col_val          <= col;        // &#38145;&#23384;&#21015;&#20540;
            row_val          <= row;        // &#38145;&#23384;&#34892;&#20540;
            key_pressed_flag <= 1;          // &#32622;&#38190;&#30424;&#25353;&#19979;&#26631;&#24535;  
          end
        endcase
    //--------------------------------------
    // &#29366;&#24577;&#26426;&#37096;&#20998; &#32467;&#26463;
    //--------------------------------------
    //++++++++++++++++++++++++++++++++++++++
    // &#25195;&#25551;&#34892;&#21015;&#20540;&#37096;&#20998; &#24320;&#22987;
    //++++++++++++++++++++++++++++++++++++++
    always @ (posedge key_clk, negedge i_rst_n)
      if (!i_rst_n)
        keyboard_val <= 4'h0;
      else
        if (key_pressed_flag)
          case ({col_val, row_val})
            8'b1110_1110 : keyboard_val <= 4'h0;
            8'b1110_1101 : keyboard_val <= 4'h4;
            8'b1110_1011 : keyboard_val <= 4'h8;
            8'b1110_0111 : keyboard_val <= 4'hC;
            
            8'b1101_1110 : keyboard_val <= 4'h1;
            8'b1101_1101 : keyboard_val <= 4'h5;
            8'b1101_1011 : keyboard_val <= 4'h9;
            8'b1101_0111 : keyboard_val <= 4'hD;
            
            8'b1011_1110 : keyboard_val <= 4'h2;
            8'b1011_1101 : keyboard_val <= 4'h6;
            8'b1011_1011 : keyboard_val <= 4'hA;
            8'b1011_0111 : keyboard_val <= 4'hE;
            
            8'b0111_1110 : keyboard_val <= 4'h3; 
            8'b0111_1101 : keyboard_val <= 4'h7;
            8'b0111_1011 : keyboard_val <= 4'hB;
            8'b0111_0111 : keyboard_val <= 4'hF;        
          endcase
    //--------------------------------------
    //  &#25195;&#25551;&#34892;&#21015;&#20540;&#37096;&#20998; &#32467;&#26463;
    //--------------------------------------
          
    endmodule