3380|1

6892

帖子

0

TA的资源

五彩晶圆(高级)

楼主
 

Verilog程序问题 [复制链接]

我是verilog初学者,正在Xilinx 3E下用verolog编写一个电子锁程序,下载到digilent Basys2FPGA板上实现。
我想通过4个按钮来输入密码,每按一次按钮,对应的数码管上显示的数字就会加一(从0至9循环)。密码设置好后拨下开关就会显示密码锁状态,按钮总共按的次数超过30次还没有成功解锁就会进入暂停状态,暂停10秒后回到锁定状态。
我用ModelSim仿真完全没有问题,但是下到板子里就出现3个主要问题,
一个是按下按钮怎么也不能按顺序递增数字,都是不规律的变化,我怀疑是FPGA板的原因二是程序无法顺利进入暂停状态,因为好像每按下一次按钮,综合出来的结果并不是认为我只按了一次。我纠结了一天也没想出解决办法
三是进入暂停状态不能自动回到锁定状态,我的状态机是在每个时钟上升沿更新状态,我也用了计数器数到10秒跳转状态,但是还是不行。
我绞尽脑汁一整天,试验了无数种方法,都不能解决,明天就要交了。程序有点长,求高手有时间帮我看看吧,非常感谢。评注写的很简洁,是我之前编的时候随便写的,大家有问题随便问,我会回帖解释。

我后来换成用开关来输入数字,这个问题解决了,但是又不知道该如何设置进入暂停状态,大家有没有好的想法也可以给我说一下,谢谢了。。




`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
此帖出自FPGA/CPLD论坛

最新回复

程序写的有点长,用三段式写的。其实用一段式表示也可以,实现应该很简单。  详情 回复 发表于 2010-4-23 12:55
点赞 关注
个人签名一个为理想不懈前进的人,一个永不言败人!
http://shop57496282.taobao.com/
欢迎光临网上店铺!
 

回复
举报

190

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
程序写的有点长,用三段式写的。其实用一段式表示也可以,实现应该很简单。
此帖出自FPGA/CPLD论坛
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
ADI 有奖直播报名中~
直播时间:3月27日(周四) 上午10:00-11:30
直播主题:易于驱动SAR型ADC的原理、优点及应用介绍
好礼等你拿~

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网 2

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表