码迷,mamicode.com
首页 > 其他好文 > 详细

用Vivado写的verilog交通灯课程作业(一)

时间:2016-05-24 20:52:47      阅读:1408      评论:0      收藏:0      [点我收藏+]

标签:

一、主模块

 

交通灯和七段计数

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2016/05/24 14:55:05
// Design Name: 
// Module Name: traffic
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module traffic(
    input wire clk,
    input wire en,
    input wire rst,
    output reg [2:0] lamp,
    output wire [15:0] seven_seg
    );
    

    
    reg       [7:0]    num        ;
    reg                temp    ;
    reg       [7:0]    red_t    ; 
    reg       [7:0]    yellow_t;
    reg       [7:0]    green_t    ;    
    wire               clkout    ;
    reg       [1:0]    state;
    
    parameter [1:0] red = 2d0,
                    
                    green = 2d2   ,
                    yellow2 = 2d3 ;    
                    

    
    always @ ( posedge clk or negedge rst)
        begin 
            if( rst == 0 )
                begin  
                    state <= 2b0 ;    
                    temp  <= 1b0 ;    
                    red_t <= 8b0010_0101;               //设置灯计时器的预置数,采用BCD码
                    yellow_t <= 8b0000_0101;
                    green_t <= 8b0010_0000;
                end
            else begin 
                if ( en )     
                    begin
                    if ( !temp ) begin 
                            temp <= 1b1;
                            case ( state )              //交通灯状态变换
                                red : begin 
                                    num <= red_t ;
                                    lamp <= 3b100;
                                    state <= green;
                                end
                            
                                green : begin 
                                    num <= green_t ;
                                    lamp <= 3b001;
                                    state <= yellow2 ;
                                end
                                yellow2 : begin 
                                    num <= yellow_t ;
                                    lamp <= 3b010;
                                    state <= red ;
                                end      
                                default : lamp <= 3b100 ;
                            endcase
                        end
                    else begin                            //倒计数
                        if ( num >1b1 )
                            if ( num [3:0] == 0 ) begin
                                num [ 3:0 ] <= 4b1001;
                                num [7:4] <= num [7:4] -1 ;
                            end
                            else num [3:0] <= num [3:0] -1 ;
                            if ( num == 2 )
                                temp <= 0 ;
                        end      
                    end
                else begin 
                    lamp <= 3b100 ;
                    state <= red ;
                    temp  <= 0 ;
                end
            end    
      end
     
      
     /************************ 数码管译码**************************************/
    reg        [7:0]    Y_r_1;                
    reg        [7:0]    Y_r_2;
    
    assign seven_seg[7:0] ={1b1,(~Y_r_1[6:0])};
    assign seven_seg[15:8] = {1b1,(~Y_r_2[6:0])};
    
    always @(num[3:0] )
        begin
            Y_r_1 = 7b1111111;
            case (num[3:0] )
                    4b0000: Y_r_1 = 7b0111111; // 0
                    4b0001: Y_r_1 = 7b0000110; // 1
                    4b0010: Y_r_1 = 7b1011011; // 2
                    4b0011: Y_r_1 = 7b1001111; // 3
                    4b0100: Y_r_1 = 7b1100110; // 4
                    4b0101: Y_r_1 = 7b1101101; // 5
                    4b0110: Y_r_1 = 7b1111101; // 6
                    4b0111: Y_r_1 = 7b0000111; // 7
                    4b1000: Y_r_1 = 7b1111111; // 8
                    4b1001: Y_r_1 = 7b1101111; // 9
                    4b1010: Y_r_1 = 7b1110111; // A
                    4b1011: Y_r_1 = 7b1111100; // b
                    4b1100: Y_r_1 = 7b0111001; // c
                    4b1101: Y_r_1 = 7b1011110; // d
                    4b1110: Y_r_1 = 7b1111001; // E
                    4b1111: Y_r_1 = 7b1110001; // F
                    default: Y_r_1 = 7b0000000;
                endcase
        end
    
        always @( num[7:4] )
        begin
            Y_r_2 = 7b1111111;
            case ( num[7:4] )
                    4b0000: Y_r_2 = 7b0111111; // 0
                    4b0001: Y_r_2 = 7b0000110; // 1
                    4b0010: Y_r_2 = 7b1011011; // 2
                    4b0011: Y_r_2 = 7b1001111; // 3
                    4b0100: Y_r_2 = 7b1100110; // 4
                    4b0101: Y_r_2 = 7b1101101; // 5
                    4b0110: Y_r_2 = 7b1111101; // 6
                    4b0111: Y_r_2 = 7b0000111; // 7
                    4b1000: Y_r_2 = 7b1111111; // 8
                    4b1001: Y_r_2 = 7b1101111; // 9
                    4b1010: Y_r_2 = 7b1110111; // A
                    4b1011: Y_r_2 = 7b1111100; // b
                    4b1100: Y_r_2 = 7b0111001; // c
                    4b1101: Y_r_2 = 7b1011110; // d
                    4b1110: Y_r_2 = 7b1111001; // E
                    4b1111: Y_r_2 = 7b1110001; // F
                    default: Y_r_2 = 7b0000000;
                endcase
        end 

    
endmodule

二、分频

clk初始时钟25MHz,分频之后1s一个脉冲

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2016/05/24 15:04:09
// Design Name: 
// Module Name: clk_div
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module clk_div(
    input wire rst,
    input wire clk,
    output reg clkout
    );
     
    reg     [31:0]    count1;
    
     always @ ( posedge clk or negedge rst)
        
      begin
        if ( rst== 1b0 ) begin
           clkout <= 1b0;
           count1 <= 17b0; 
        end
      else begin
         if ( count1 >= 32d25000000) begin
             clkout <= ~clkout ;
              count1 <=#1  17b0; 
              end
            else begin
              count1 <= count1 + 1;
            
            end 
         end 
      end 
endmodule

 

抄了好多。

但是也改了好久好久好久好久。

Vivado真特么难用,这游戏不适合我

要回归Quartus II 。

 

用Vivado写的verilog交通灯课程作业(一)

标签:

原文地址:http://www.cnblogs.com/MnsterLu/p/5524511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!