2012年10月28日 星期日

P6-70 2 digit BCD Counter 00-99 適用於DE2-70 (SW0 往上撥 才能計數 , 往下撥 Reset)

P6-70 2 digit BCD Counter 00-99 適用於DE2-70  (SW0 往上撥 才能計數 ,  往下撥 Reset)

源自於http://www.electronics-tutorials.ws/counter/count_2.html

Asynchronous Counter

In the previous tutorial we saw that an Asynchronous counter can have 2n-1 possible counting states e.g. MOD-16 for a 4-bit counter, (0-15) making it ideal for use in Frequency Division. But it is also possible to use the basic asynchronous counter to construct special counters with counting states less than their maximum output number by forcing the counter to reset itself to zero at a pre-determined value producing a type of asynchronous counter that has truncated sequences. Then an n-bit counter that counts up to its maximum modulus (2n) is called a full sequence counter and a n-bit counter whose modulus is less than the maximum possible is called a truncated counter.
But why would we want to create an asynchronous truncated counter that is not a MOD-4, MOD-8, or some other modulus that is equal to the power of two. The answer is that we can by using combinational logic to take advantage of the asynchronous inputs on the flip-flop. If we take the modulo-16 asynchronous counter and modified it with additional logic gates it can be made to give a decade (divide-by-10) counter output for use in standard decimal counting and arithmetic circuits.
Such counters are generally referred to as Decade Counters. A decade counter requires resetting to zero when the output count reaches the decimal value of 10, ie. when DCBA = 1010 and to do this we need to feed this condition back to the reset input. A counter with a count sequence from binary "0000" (BCD = "0") through to "1001" (BCD = "9") is generally referred to as a BCD binary-coded-decimal counter because its ten state sequence is that of a BCD code but binary decade counters are more common.

Asynchronous Decade Counter

Asynchronous Decade Counter
This type of asynchronous counter counts upwards on each leading edge of the input clock signal starting from "0000" until it reaches an output "1010" (decimal 10). Both outputs QB and QD are now equal to logic "1" and the output from the NAND gate changes state from logic "1" to a logic "0" level and whose output is also connected to the CLEAR (CLR) inputs of all the J-K Flip-flops.
This signal causes all of the Q outputs to be reset back to binary "0000" on the count of 10. Once QB andQD are both equal to logic "0" the output of the NAND gate returns back to a logic level "1" and the counter restarts again from "0000". We now have a decade or Modulo-10 counter.

Decade Counter Truth Table

Clock
Count
Output bit PatternDecimal
Value
QDQCQBQA
100000
200011
300102
400113
501004
601015
701106
801117
910008
1010019
11Counter Resets its Outputs back to Zero

Decade Counter Timing Diagram

Asynchronous Decade Counter Timing

Using the same idea of truncating counter output sequences, the above circuit could easily be adapted to other counting cycles be simply changing the connections to the AND gate. For example, a scale-of-twelve (modulo-12) can easily be made by simply taking the inputs to the AND gate from the outputs at "QC" and "QD", noting that the binary equivalent of 12 is "1100" and that output "QA" is the least significant bit (LSB).
Since the maximum modulus that can be implemented with n flip-flops is 2n, this means that when you are designing truncated asynchronous counters you should determine the lowest power of two that is greater than or equal to your desired modulus. For example, lets say you wish to count from 0 to 39, or mod-40. Then the highest number of flip-flops required would be six, n = 6 giving a maximum MOD of 64 as five flip-flops would only equal MOD-32.
divide-by-128 CounterNow suppose we wanted to build a "divide-by-128" counter for frequency division we would need to cascade seven flip-flops since 128 = 27. Using dual flip-flops such as the 74LS74 we would still need four IC's to complete the circuit.
One easy alternative method would be to use two TTL 7493's as 4-bit ripple counter/dividers. Since 128 = 16 x 8, one 7493 could be configured as a "divide-by-16" counter and the other as a "divide-by-8" counter. The two IC's would be cascaded together to form a "divide-by-128" frequency divider as shown.
Of course standard IC asynchronous counters are available such as the TTL 74LS90 programmable ripple counter/divider which can be configured as a divide-by-2, divide-by-5 or any combination of both. The 74LS390 is a very flexible dual decade driver IC with a large number of "divide-by" combinations available ranging form divide-by-2, 4, 5, 10, 20, 25, 50, and 100.


//top-level module for 2-digit BCD counter 00-->99
// 
module _2digit_BCD_count(SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 );

input  [17:0] SW; // toggle switches
input  [7:0] KEY;     // Push bottom
input  CLOCK_27; //Clock 27MHz , 50Mhz

output [17:0] LEDR; // red  LEDS   
  output [8:0] LEDG; // green LEDs
    
    output [6:0] HEX0,HEX1,HEX2,HEX3; //7-segment display
    
  
//set original program input , output 
  
//(clk,reset,one_out,ten_out);
//input clk,reset;
//output [3:0] one_out,ten_out;
    wire clk,reset;
    //reg [3:0] one_out,ten_out;
    
  
    wire [7:0] segout0;   //HEX 0
    wire [7:0] segout1;   //HEX 1
    
    assign  reset=SW[0] ;
    assign  clk =KEY[0];  
    
    
wire nreset,ones_en,tens_en,tc_ones;  //tc_ones --> 1's carry out
wire [3:0] one_out , ten_out;
  
  
// module cnt_10(ce,clk, clr,tc,qout);
cnt_10 _1s (.ce(ones_en),
             .clk(clk),
             .clr(nreset),
             .tc(tc_ones),    //1's carry out
.qout(one_out)
);
  
cnt_10 _10s (.ce(tens_en),
             .clk(clk),
             .clr(nreset),
             .tc(), //10's carry out  ignore   
             .qout(ten_out)
             );
             
assign tens_en=tc_ones;
assign ones_en =1'b1;
assign nreset=~reset;
    
    
            
_7seg UUT0(.hex((one_out)),
               .seg(segout0));
    
    _7seg UUT1(.hex((ten_out)),
               .seg(segout1));
    
    
       
    assign HEX0=segout0[6:0];
    assign HEX1=segout1[6:0];
    

endmodule

//====================================


//BCD counter counts 0 to 9 and then rolls over
module cnt_10(ce,clk,clr,tc,qout);
  input ce,clk,clr;
  output tc;  //carry out
  output [3:0] qout;
  
  reg [3:0] count;
  
  always @(posedge clk or posedge clr)
  begin
    if (clr)  //clear 
        count <=4'b0;
    else 

if (ce) 
      if (count==4'h9)
          count <=4'h0;
      else    
          count <= count +1;
  end        
  
  assign qout = count ;
  assign tc=(count==4'h9);   //if count=9 then carry out=1

endmodule

//======================================



//-----------------------------------------
//Common-cathod seven segment display
//using case.....endcase statement
//Filename : sevenseg_case.v
//----------------------------------------- 
module _7seg(hex , seg);

    input  [3:0] hex;
    output [7:0] seg;
    reg    [7:0] seg;
    
        

// segment encoding
//      0
//     ---  
//  5 |   | 1
//     ---   <- 6
//  4 |   | 2
//     ---
//      3

always @(hex)
begin
case (hex)
       // Dot point is always disable
       4'b0001 : seg = 8'b11111001;   //1 = F9H
       4'b0010 : seg = 8'b10100100;   //2 = A4H
       4'b0011 : seg = 8'b10110000;   //3 = B0H
       4'b0100 : seg = 8'b10011001;   //4 = 99H
       4'b0101 : seg = 8'b10010010;   //5 = 92H
       4'b0110 : seg = 8'b10000010;   //6 = 82H
       4'b0111 : seg = 8'b11111000;   //7 = F8H
       4'b1000 : seg = 8'b10000000;   //8 = 80H
       4'b1001 : seg = 8'b10010000;   //9 = 90H
       4'b1010 : seg = 8'b10001000;   //A = 88H
       4'b1011 : seg = 8'b10000011;   //b = 83H
       4'b1100 : seg = 8'b11000110;   //C = C6H
       4'b1101 : seg = 8'b10100001;   //d = A1H
       4'b1110 : seg = 8'b10000110;   //E = 86H
       4'b1111 : seg = 8'b10001110;   //F = 8EH
       default : seg = 8'b11000000;   //0 = C0H
     endcase
   end
   

endmodule



沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...