環形計數器---Ring Counter
適用於DE2-70
==========================================
設計方法
1.每一正反器波形皆相同,只是相位不同。
2.每個正反器輸出責任週期皆為25%。
3.每個正反器輸出信號週期皆為輸入CK的4倍。
==========================================
形式1: 1000->0100->0010->0001->1000->.....
形式2:
1000->1100->1110->1111->0111->0011->0001->0000
->1000->>.................
// Ch08 cnt5.v
// 環形計數器
module cnt5 (SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 ,HEX4 );
input [17:0] SW; // toggle switches
input [3: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 ,HEX4; //7-segment display
//set original program input , output
//cnt5 (Clk,Clr,Q1,Q2);
//input Clk,Clr; // 一位元輸入
//output [3:0] Q1,Q2; // 四位元輸出
//reg [3:0] Q1,Q2; // 宣告為暫存器資料
wire Clk ,Clr ;
reg [3:0] Q1=4'b1000;
reg [3:0] Q2=4'b1000; // 宣告為暫存器資料
wire [7:0] segout0; //HEX 0
wire [7:0] segout1; //HEX 1
assign Clk=KEY[0];
assign Clr=KEY[1];
// 產生計數值
always@ (negedge Clk or negedge Clr)
if (!Clr)
begin
Q1 = 4'b1000; // Q1 初值
Q2 = 4'b1000; // Q2 初值
end
else
begin
Q1 = { Q1[0],Q1[3:1]}; // 循環右移位
Q2 = {~Q2[0],Q2[3:1]}; // 循環右移位(反相)
end
_7seg UUT0(.hex(Q1),
.seg(segout0));
_7seg UUT1(.hex(Q2),
.seg(segout1));
assign HEX0=segout0[6:0];
assign HEX1=segout1[6:0];
assign LEDG[7:4]=Q1;
assign LEDR[3:0]=Q2;
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
沒有留言:
張貼留言