创客 发表于 2013-8-1 18:32:09

两位数码管之计数器

module digital1 (clk,res,sm_cs1,sm_cs2,out);

input clk,res;

output sm_cs1,sm_cs2;
output out;

wire out;

reg cnt;

always @ (posedge clk or negedge res)
   if(!res) cnt <= 25'b0;
         else cnt <= cnt + 1'b1;       

reg shi,ge;
                       
always @ (posedge clk or negedge res)
    if(!res)
      begin
             shi <= 4'd1;
                  ge <= 4'd0;
               end
                elseif(cnt == 25'h1ffffff)
                         begin
                                          //shi <= shi + 1'b1;
                                           ge <= ge + 1'b1;
                                                if(ge == 4'h9)
                                               begin
                                               ge <= 4'd0;
                                               shi <= shi + 1'b1;
                                                  if(shi == 4'h9)
                                                  begin
                                                       shi <= 4'd0;
                                                       end
                                               end
                                        end
parameter result0 = 7'h3f,
          result1 = 7'h06,
                  result2 = 7'h5b,
                  result3 = 7'h4f,
          result4 = 7'h66,
                  result5 = 7'h6d,
                  result6 = 7'h7d,
                       result7 = 7'h07,
          result8 = 7'h7f,
                  result9 = 7'h6f;
//          result10 = 7'hf7,
//          result11 = 7'hfc,
//                  result12 = 7'h39,
//                  result13 = 7'h5e,
//                       result14 = 7'h79,
//                  result15 = 7'h71;
reg num;
reg outr;

always @ (num)
                               case(num)
                                4'h0:outr<=result0;
                                4'h1:outr<=result1;
                                4'h2:outr<=result2;
                                4'h3:outr<=result3;
                                4'h4:outr<=result4;
                                4'h5:outr<=result5;
                                4'h6:outr<=result6;
                                4'h7:outr<=result7;
                                4'h8:outr<=result8;
                                4'h9:outr<=result9;
//                                4'hA:outr<=result10;
//                                4'hB:outr<=result11;
//                                4'hC:outr<=result12;
//                                4'hD:outr<=result13;
//                                4'hE:outr<=result14;
//                                4'hF:outr<=result15;
                                default: outr <= result0;
       endcase
assign out = outr;
                       
reg sm_cs2_r,sm_cs1_r;
//----------------------------两个数码管之间的扫描
always @(clk or shi or ge)
      begin
                if(cnt)
                begin
                        sm_cs2_r = 0;
                        sm_cs1_r = 1;
                        num = shi;
                end
                else
                begin
                        sm_cs2_r = 1;
                        sm_cs1_r = 0;
                                                                num = ge;
                  end
      end
                       
assign sm_cs2 = sm_cs2_r;
assign sm_cs1 = sm_cs1_r;

       
       
endmodule
         
               
页: [1]
查看完整版本: 两位数码管之计数器