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.

How to input digit using push button with FPGA board?

Status
Not open for further replies.

rfub

Newbie level 6
Joined
Dec 9, 2009
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,391
Hi, I'm designning an electronic locker using Verilog with Xilinx 3E, Basys2 Board.
I have several problem while programming
1 I want to enter the combinations using pushbuttons, every time I push one button, the number will increase one and will be displayed on the corresponding 7-segment Led from 0 to 9.

I used FSM but I can't get the number properly, actually the simulation is good, but when I download the program into the board, the problems come out, I'm wondering if it's the reason of pushbutton?

Here's my code, can you please take a look at it and tell me what's wrong with my code? Thank you very much.

Code:
`timescale 1ns / 1ps

module Locker(clk, switch, button, cathodes, anodes, leds);
    input clk;
    input switch;//switch==0, show combination, else show locker state
    input [3:0] button;
   
    output [7:0] cathodes;
    output [3:0] anodes;
    output [7:0] leds;
   
    reg [7:0] cathodes;
    reg [3:0] anodes;
    reg [7:0] leds=0;
    reg [3:0] digit;
    reg [3:0] temp_comb;
   
    reg [14:0]count;
   
    reg [1:0] ntmp0=0, ntmp1=0, ntmp2=0, ntmp3=0, nstate=0;
    reg [1:0] ctmp0, ctmp1, ctmp2, ctmp3, cstate;
    reg [1:0] flag ;//0, unlock, 1, lock, 2, pause
    integer error=0, ledtime=0, paustime=0;
    parameter LOCK  = 3'b001,UNLC = 3'b010,PAUS = 3'b100 ;
   
    always @(posedge clk)  //LED flash and pause wait
    begin
        ctmp0 = ntmp0;
        ctmp1 = ntmp1;
        ctmp2 = ntmp2;
        ctmp3 = ntmp3;
        cstate = nstate;
        error <= ntmp0 + ntmp1 + ntmp2 + ntmp3;   
        if (ctmp0 ==0 && ctmp1 == 0 && ctmp2 == 0 && ctmp3 == 0)
            begin
            flag = 0; //combination is right
            end
        else
            begin
            flag = 1; //lock
            end
        if (error > 30)
        begin
            flag = 2; //pause
            if (paustime > 50000000)
            begin
                paustime = 0;
                flag = 1;
            end
            paustime = paustime+1;
        end
        end   

    always @(posedge clk)
    begin
        if (flag == 2'd0 && switch == 1)
        begin
            if (ledtime > 10000000)
            begin
                ledtime = 0;
                leds = ~leds;
            end
            ledtime = ledtime+1;
        end
        else   
               leds = 0;
    end
   
    always @(posedge clk)  //composition of combination
    begin
        case (button)
            4'b0001:
            begin
            temp_comb = ntmp0;
            ntmp0 <= #1 combination(temp_comb);
            end
            4'b0010:
            begin
            temp_comb = ntmp1;
            ntmp1 <= #1 combination(temp_comb);
            end
            4'b0100:
            begin
            temp_comb = ntmp2;
            ntmp2 <= #1 combination(temp_comb);
            end
            4'b1000:
            begin
            temp_comb = ntmp3;
            ntmp3 <= #1 combination(temp_comb);
            end
            4'b1001:
                begin
                    ntmp0 <= 0;
                    ntmp1 <= 0;
                    ntmp2 <= 0;
                    ntmp3 <= 0;
                end
        endcase
    end
   
    always @(posedge clk) //change of state
    begin
        case (flag)
            2'd0: nstate <= UNLC;
            2'd1: nstate <= LOCK;
            2'd2: nstate <= PAUS;           
        endcase
    end


    //display the result on the 7-segment LEDs dynamically
    always @(posedge clk)
    begin
    count <= count+1;
        if (count<=5000)
            begin
            anodes  <= 4'b0111; //an0, k14, most right segment
            end
        else if (count<=10000)           
          begin
            anodes <= 4'b1011; //an1, m13
            end   
        else    if (count<=15000)
          begin
            anodes <= 4'b1101; //an2. j12
            end
        else    if (count<=20000)
          begin
            anodes <= 4'b1110; //an2. j12
            end           
        else
            count<=0;
    end


    always  @(posedge clk)  //displacement of the 7_segment LEDS
    begin
    if (switch == 0) //show the combinations
    begin
    cathodes = show_combination(digit);
        case(anodes)
        4'b0111: digit = ctmp0;
        4'b1011:    digit = ctmp1;               
        4'b1101: digit = ctmp2;
        4'b1110: digit = ctmp3;           
        endcase
    end
    else //show the clock states
    begin
            case(anodes)
        4'b0111:case (cstate) //most right anode
                         LOCK:    cathodes=8'hc6;//show nothing
                         UNLC:    cathodes=8'hc6;//C
                         PAUS:    cathodes=8'h92;//S
                    endcase
        4'b1011:case (cstate)
                         LOCK:    cathodes=8'hc0;//C
                         UNLC:    cathodes=8'hc7;//L
                         PAUS:    cathodes=8'hc1;//U
                    endcase                       
        4'b1101:case (cstate)
                         LOCK:    cathodes=8'hc7;//O
                         UNLC:    cathodes=8'hab;//n
                         PAUS:    cathodes=8'h88;//A
                    endcase
        4'b1110:case (cstate)
                         LOCK:    cathodes=8'hff;//L
                         UNLC:    cathodes=8'hc1;//U
                         PAUS:    cathodes=8'h8c;//P
                    endcase
        endcase   
    end   
    end
   

    function [3:0] combination;
        input [3:0] temp_comb;
        begin
            case (temp_comb)
             4'd0:    combination=4'd1;
             4'd1:    combination=4'd2;
             4'd2:    combination=4'd3;
             4'd3:    combination=4'd4;
             4'd4:    combination=4'd5;
             4'd5:    combination=4'd6;
             4'd6:    combination=4'd7;
             4'd7:    combination=4'd8;
             4'd8:    combination=4'd9;
             4'd9:    combination=4'd0;
            default: combination=8'hcf;
            endcase   
        end   
    endfunction   
   
    function [7:0] show_combination;
        input [3:0] digit;
        begin
            case (digit)
             4'd0:    show_combination=8'hc0;
             4'd1:    show_combination=8'hcf;
             4'd2:    show_combination=8'ha4;
             4'd3:    show_combination=8'hb0;
             4'd4:    show_combination=8'h99;
             4'd5:    show_combination=8'h92;
             4'd6:    show_combination=8'h82;
             4'd7:    show_combination=8'hf8;
             4'd8:    show_combination=8'h80;
             4'd9:    show_combination=8'h90;
            default: show_combination=8'hcf;
            endcase   
        end
    endfunction   
   
endmodule
 

What problems are you getting? Can you provide the output of chipscope? How about the out of the simulation? You're not giving us a lot to work with here. As your code is, its a pain to read. If you are gonna post it in public, add comments so there can be a general understanding.
 

try to work with switch instead, if it works then the problem is in the pusbutton.
 

Thanks for your response,I've already figured out the problem.
 

Hi,

I just went through your code it seems to be fine for me... can u tell me what is the reason behind it for not getting proper output.

Thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top