当前位置:   article > 正文

基于FPGA的秒表数码管显示_fpga简单四位数码管秒表

fpga简单四位数码管秒表

链接:https://pan.baidu.com/s/1JzG7b9xz8OXOeETqunnyBw
提取码:1234

功能

功能:四位数码管显示,从零开始计数,前两位显示秒(059),后两位显示0.01秒(099),
计满后从零开始,有开始键、暂停键、复位键。当第一次按下开始键,秒表从初始开始计数,LED显示器上显示当前计时值;当紧按下(按住)暂停键时,秒表暂停计时,LED上显示当前计时值,放开则继续计时;当按下复位键时,秒表停止计数,并且计数结果清零。

部分代码

module time_clock(
    clk,
    reset_n,
    start_key,
    pause_key,
    duan,
    wei
    );
    
    input           clk;                            //clk:50MHZ时钟输入;
    input           reset_n;                        //复位信号输入,低电平有效;
    input           start_key;                      //开始信号输入,低电平有效;
    input           pause_key;                      //暂停功能按键,进行秒表计时和倒计时时可以通过该按键进行暂停,‘1’暂停,‘0’继续
    
    output  [7:0]   duan;                           //duan:数码管段码;
    output  [7:0]   wei;                            //wei:数码管位码;
    
    reg     [7:0]   duan;                           //duan:数码管段码;
    reg     [7:0]   wei;                            //wei:数码管位码;

    reg     [24:0]  count;                          //100HZ时钟计数器
    reg     [13:0]  count2;                         //扫描时钟计数器
    reg             clk_2000hz;                     //100HZ时钟信号

    reg             clk_scan;                       //数码管扫描时钟
    reg     [1:0]   select;                         //用于扫描时选择显示位码
    
    // ****************************************************
    //开始按键消抖模块
    // ****************************************************
    reg         start_key_out;
    parameter   start_s0=3'b000,
                start_s1=3'b001,
                start_s2=3'b010,
                start_s3=3'b011,
                start_s4=3'b100,
                start_s5=3'b101;
    reg [2:0]   start_state;
    always @(posedge clk or negedge reset_n)
    begin
        if(reset_n == 1'b0)
        begin
            start_state <= start_s0;
            start_key_out <= 1'b1;
        end
        else
        begin
            case (start_state)
            start_s0:
            begin 
                start_key_out <= 1'b1;
                if(start_key == 1'b0)
                start_state <= start_s1;
                else 
                start_state <= start_s0;
            end
            start_s1:
            begin 
                if(start_key == 1'b0)
                start_state <= start_s2;
                else 
                start_state <= start_s0;
            end 
            start_s2:
            begin 
                if(start_key == 1'b0)
                start_state <= start_s3;
                else 
                start_state <= start_s0;    
            end 
            start_s3:
            begin 
                if(start_key == 1'b0)
                start_state <= start_s4;
                else 
                start_state <= start_s0;    
            end
            start_s4:
            begin 
                if(start_key == 1'b0)
                start_state <= start_s5;
                else 
                start_state <= start_s0;    
            end
            start_s5:
            begin 
                if(start_key == 1'b0)
                begin
                    start_key_out <= 1'b0;
                    start_state <= start_s5;
                end 
                else 
                begin
                    start_key_out <= 1'b0;
                    start_state <= start_s5;
                end
            end
            default:
            begin
                start_state <= start_s0;
                start_key_out <= 1'b1;
            end
            endcase
        end
    end
    //****************************************************************************************************
    //  模块名称:分频模块
    //  功能描述:
    //****************************************************************************************************
    always @(posedge clk or negedge reset_n)//2000HZ时钟进程
    begin
        if(reset_n == 1'b0)
        begin
            count <= 25'd0;
            clk_2000hz <= 1'b0;
        end
        else
            if(count == 25'd12499)
            begin
                clk_2000hz <= ~clk_2000hz;
                count <= 25'd0;
            end
            else
                count <= count + 1'b1;
    end
    
    reg     [17:0]  counter_count;
    reg             clk_100hz;
    always @(posedge clk or negedge reset_n)//100HZ时钟进程 clk_2000hz
    begin
        if(reset_n == 1'b0)
        begin
            counter_count <= 18'd0;
            clk_100hz <= 1'b0;
        end
        else
            if(~pause_key == 1'b0)//当暂停的时候不进行产生计数时钟信号
                if(counter_count == 18'd1)//9
                begin
                    clk_100hz <= ~clk_100hz;
                    counter_count <= 18'd0;
                end
                else
                    counter_count <= counter_count + 1'b1;
            else
            begin
                clk_100hz <= 1'b0;
                counter_count <= 18'd0;
            end
    end    
    //****************************************************************************************************
    //  模块名称:秒表计时
    //  功能描述:
    //**************************************************************************************************** 
    reg     [3:0]   counter_haomiao_ge;             //秒表计时,毫秒个位数BCD码
    reg     [3:0]   counter_haomiao_shi;            //秒表计时,毫秒十位数BCD码
    reg     [3:0]   counter_miao_ge;                //秒表计时,秒个位数BCD码
    reg     [2:0]   counter_miao_shi;               //秒表计时,秒十位BCD二进制码
    always @(posedge clk_100hz or negedge reset_n)
    begin
        if(reset_n == 1'b0)
        begin
            counter_haomiao_ge <= 4'd0;
            counter_haomiao_shi <= 4'd0;
            counter_miao_ge <= 4'd0;
            counter_miao_shi <= 3'd0;
        end
        else
            if(~start_key_out == 1'b1)//开始信号有效的时候进行计时
                if(counter_haomiao_ge == 4'd9)
                begin
                    counter_haomiao_ge <= 4'd0;
                    if(counter_haomiao_shi == 4'd9)
                    begin
                        counter_haomiao_shi <= 4'd0;
                        if(counter_miao_ge ==4'd9)
                        begin
                            counter_miao_ge <= 4'd0;
                            if(counter_miao_shi == 3'd5)
                            begin
                                counter_miao_shi <= 3'd0;
                            end
                            else
                               counter_miao_shi <= counter_miao_shi + 1'b1;
                        end
                        else
                            counter_miao_ge <= counter_miao_ge + 1'b1;
                    end
                    else
                        counter_haomiao_shi <= counter_haomiao_shi + 1'b1;
                end
                else
                    counter_haomiao_ge <= counter_haomiao_ge + 1'b1;
            else
            begin
                counter_haomiao_ge <= 4'd0;
                counter_haomiao_shi <= 4'd0;
                counter_miao_ge <= 4'd0;
                counter_miao_shi <= 3'd0;
            end
    end 
    //****************************************************************************************************
    //  模块名称:毫秒、秒计数寄存模块
    //  功能描述:
    //****************************************************************************************************        
    reg     [3:0]   reg_haomiao_ge;                 //毫秒个位数BCD码
    reg     [3:0]   reg_haomiao_shi;                //毫秒十位数BCD码
    reg     [3:0]   reg_miao_ge;                    //秒个位数BCD码
    reg     [2:0]   reg_miao_shi;                   //秒十位BCD二进制码
    always @(posedge clk or negedge reset_n)
    begin
        if(reset_n == 1'b0)
        begin
            reg_haomiao_ge <= 4'd9;
            reg_haomiao_shi <= 4'd9;
            reg_miao_ge <= 4'd9;
            reg_miao_shi <= 3'd5;
        end
        else
        begin
            reg_haomiao_ge <= counter_haomiao_ge;
            reg_haomiao_shi <= counter_haomiao_shi;
            reg_miao_ge <= counter_miao_ge;
            reg_miao_shi <= counter_miao_shi;
        end
    end
    //****************************************************************************************************
    //  模块名称:数码管位选择时钟产生模块
    //  功能描述:
    //****************************************************************************************************
    always @(posedge clk or negedge reset_n)//数码管扫描时钟产生进程
    begin
        if(reset_n == 1'b0)
        begin
            count2 <= 14'd0;
            clk_scan <= 1'b0;
        end
        else
            if(count2 == 14'd10000)
            begin
                count2 <= 14'd0;
                clk_scan <= ~clk_scan;//2500Hz的数码管扫描频率时钟信号
            end
            else
                count2 <= count2 + 1'b1;
    end
    //****************************************************************************************************
    //  模块名称:数码管位选择产生信号模块
    //  功能描述:
    //****************************************************************************************************
    always @(posedge clk_scan or negedge reset_n)
    begin 
        if(reset_n == 1'b0)
            select <= 2'b00;
        else
            select <= select + 1'b1;//数码管位选择信号
    end
    //****************************************************************************************************
    //  模块名称:译码电路
    //  功能描述:
    //****************************************************************************************************
    always @(posedge clk or negedge reset_n)
    begin
        if(reset_n == 1'b0)
        begin
            wei <= 8'b11111111;
            duan <= 8'hff;
        end
        else
        begin
            if(select == 2'd0)
            begin
                wei <= 8'b11101111;//毫秒个位数显示
                case(reg_haomiao_ge)
                    4'b0000:duan <= 8'b1100_0000;//0
                    4'b0001:duan <= 8'b1111_1001;//1
                    4'b0010:duan <= 8'b1010_0100;//2
                    4'b0011:duan <= 8'b1011_0000;//3
                    4'b0100:duan <= 8'b1001_1001;//4
                    4'b0101:duan <= 8'b1001_0010;//5
                    4'b0110:duan <= 8'b1000_0010;//6
                    4'b0111:duan <= 8'b1111_1000;//7
                    4'b1000:duan <= 8'b1000_0000;//8
                    4'b1001:duan <= 8'b1001_0000;//9
                    default:duan <= 8'hff;
                endcase
            end
            else if(select == 2'd1)
            begin
                wei <= 8'b11011111;//毫秒十位数显示
                case(reg_haomiao_shi)
                    4'b0000:duan <= 8'b1100_0000;//0
                    4'b0001:duan <= 8'b1111_1001;//1
                    4'b0010:duan <= 8'b1010_0100;//2
                    4'b0011:duan <= 8'b1011_0000;//3
                    4'b0100:duan <= 8'b1001_1001;//4
                    4'b0101:duan <= 8'b1001_0010;//5
                    4'b0110:duan <= 8'b1000_0010;//6
                    4'b0111:duan <= 8'b1111_1000;//7
                    4'b1000:duan <= 8'b1000_0000;//8
                    4'b1001:duan <= 8'b1001_0000;//9
                    default:duan <= 8'hff;
                endcase
            end
            else if(select == 2'd2)
            begin
                wei <= 8'b10111111;//秒个位数显示
                case(reg_miao_ge)
                    4'b0000:duan <= 8'b0100_0000;//0
                    4'b0001:duan <= 8'b0111_1001;//1
                    4'b0010:duan <= 8'b0010_0100;//2
                    4'b0011:duan <= 8'b0011_0000;//3
                    4'b0100:duan <= 8'b0001_1001;//4
                    4'b0101:duan <= 8'b0001_0010;//5
                    4'b0110:duan <= 8'b0000_0010;//6
                    4'b0111:duan <= 8'b0111_1000;//7
                    4'b1000:duan <= 8'b0000_0000;//8
                    4'b1001:duan <= 8'b0001_0000;//9
                    default:duan <= 8'hff;
                endcase
            end

            else if(select == 2'd3)
            begin
                wei <= 8'b01111111;//秒十位数显示
                case(reg_miao_shi)
                    3'b000:duan <= 8'b1100_0000;
                    3'b001:duan <= 8'b1111_1001;
                    3'b010:duan <= 8'b1010_0100;
                    3'b011:duan <= 8'b1011_0000;
                    3'b100:duan <= 8'b1001_1001;
                    3'b101:duan <= 8'b1001_0010;
                    3'b110:duan <= 8'b1000_0010;
                    default:duan <= 8'hff;
                endcase
            end
            else 
            begin
                wei <= 8'b11111111;
                duan <= 8'hff;
            end
        end
    end
endmodule


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/659902
推荐阅读
相关标签
  

闽ICP备14008679号