romel_emperado
Advanced Member level 2
- Joined
- Jul 23, 2009
- Messages
- 606
- Helped
- 45
- Reputation
- 132
- Reaction score
- 65
- Trophy points
- 1,318
- Location
- philippines
- Activity points
- 6,061
module segmentTrial(clk,clk2,min1,seg,seg_sel);
input clk;
output [7:0]seg;
output [3:0]seg_sel;
output clk2;
output min1;
reg [7:0]seg;
reg [3:0]seg_sel;
reg clk2;
reg min1;
integer i;
//parameter out = 8'b00000010;//6
parameter out = 8'b100_1111_0;
initial seg_sel = 4'b0001;
always@(posedge clk)
begin
if(i==8000000) //divide by 80000 => 16MHz to 200Hz
begin
clk2=~clk2;
i=1;
end
else
begin
i=i+1'b1;
end
end
/*always@(posedge clk2)
begin
seg_sel={seg_sel[2:0],seg_sel[3]};
if(seg_sel==4'b1000) seg = out;
else if(seg_sel==4'b0100)seg = out;
else if(seg_sel==4'b0010)seg = out;
else if(seg_sel==4'b0001)seg = out;
end
*/
endmodule
//this module allows numbes to be displayed in the 7 segment display.
//parameters declared like zero, one... are the numbers displayed in the 7segment.
module decoder(in,out);
parameter zero =8'b000_0001_1; // 3
parameter one =8'b100_1111_1; //159
parameter two =8'b001_0010_1; // 37
parameter three=8'b000_0110_1; // 13
parameter four =8'b100_1100_1; //153
parameter five =8'b010_0100_1; // 73
parameter six =8'b010_0000_1; // 65
parameter seven=8'b000_1111_1; // 31
parameter eight=8'b000_0000_1; // 1
parameter nine =8'b000_0100_1; // 9
parameter blank=8'b111_1111_1; //255
input [3:0]in;
output [7:0]out;
reg [7:0]out;
always@(in)begin
case(in)
0:out=zero;
1:out=one;
2:out=two;
3:out=three;
4:out=four;
5:out=five;
6:out=six;
7:out=seven;
8:out=eight;
9:out=nine;
default:out=blank;
endcase
end
endmodule
module segmentTrial(clk,clk2,min1,seg,seg_sel);
input clk;
output [7:0]seg;
output [3:0]seg_sel;
output clk2;
output min1;
reg [7:0]seg;
reg [3:0]seg_sel;
reg clk2;
reg min1;
integer i,hr;
//wire in,outd;
parameter one = 8'b100_1111_1; //159
parameter zero = 8'b000_0001_1; // 3
parameter two =8'b001_0010_1; // 37
parameter three=8'b000_0110_1; // 13
parameter four =8'b100_1100_1; //153
parameter five =8'b010_0100_1; // 73
parameter six =8'b010_0000_1; // 65
parameter seven=8'b000_1111_1; // 31
parameter eight=8'b000_0000_1; // 1
parameter nine =8'b000_0100_1; // 9
parameter blank=8'b111_1111_1; //255 */
initial seg_sel = 4'b0001;
initial seg = blank;
//initial hr = 12;
//initial min = 1;
always@(posedge clk)
begin
if(i==16000000) //divide by 80000 => 16MHz to 200Hz
begin
clk2=~clk2;
i=1;
end
else
begin
i=i+1'b1;
end
end
always@(posedge clk2)
begin
hr = hr+1'b1;
if(hr == 60 || hr > 9 )
begin
seg = zero;
hr = 0;
end
if(hr == 1) seg = one;
if(hr == 2) seg = two;
if(hr == 3) seg = three;
if(hr == 4) seg = four;
if(hr == 5) seg = five;
if(hr == 6) seg = six;
if(hr == 7) seg = seven;
if(hr == 8) seg = eight;
if(hr == 9) seg = nine;
end
endmodule
Also functions, that are available for limited purposes, generate parallel hardware. Looking at hardware description languages from a software point of view is a popular way to completely misunderstand them. It's not impossible to learn things by trial and error, but much better to have a profound text book or tutorial.
module segmentTrial(clk,tick,seg,seg_sel);
input clk;
output [7:0]seg;
output [3:0]seg_sel;
output tick;
reg tick;
reg [7:0]seg;
reg [3:0]seg_sel;
integer i,r,minute1,second,minute2,hour1,hour2;
parameter one = 8'b100_1111_1; //159
parameter zero = 8'b000_0001_1; // 3
parameter two =8'b001_0010_1; // 37
parameter three=8'b000_0110_1; // 13
parameter four =8'b100_1100_1; //153
parameter five =8'b010_0100_1; // 73
parameter six =8'b010_0000_1; // 65
parameter seven=8'b000_1111_1; // 31
parameter eight=8'b000_0000_1; // 1
parameter nine =8'b000_0100_1; // 9
parameter blank=8'b111_1111_1; //255 */
initial seg_sel = 4'b0001; //intialize seleector digit 1
initial seg = blank; //initialize segment in to blank display
initial minute2 = 5;
initial minute1 = 9;
initial hour2 = 1;
initial hour1 = 2;
/*---------- timer ---------------------*/
always@(posedge clk)
begin
if(i==16000000) //1Hz
begin
i=1;
second = second+1'b1;
if(second == 60)
begin
second = 0;
minute1 = minute1+1'b1;
if(minute1 == 10)
begin
minute1 = 0;
minute2 = minute2+1'b1;
if(minute2 == 6)
begin
minute2 = 0;
hour1 = hour1+1'b1;
if(hour1 == 3 && hour2 == 1)
begin
hour1 = 1;
hour2 = 0;
end
if(hour1 == 10)
begin
hour1 = 0;
hour2 = hour2+1'b1;
end
end
end
end
end
else
begin
i=i+1'b1; //increament i
end
end
always@(posedge clk)
begin
if(r==800)
begin
tick=~tick;
r=1;
end
else
begin
r=r+1'b1;
end
end
always@(posedge tick)
begin
seg_sel={seg_sel[2:0],seg_sel[3]};
if(seg_sel==4'b1000)
begin
case(hour2) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four;
5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine;
endcase
end
else if(seg_sel==4'b0100)
begin
case(hour1) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four;
5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine;
endcase
end
else if(seg_sel==4'b0010)
begin
case(minute2) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four;
5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine;
endcase
end
else if(seg_sel==4'b0001)
begin
case(minute1) 0:seg=zero; 1:seg=one; 2:seg=two; 3:seg=three; 4:seg=four;
5:seg=five; 6:seg=six; 7:seg=seven; 8:seg=eight; 9:seg=nine;
endcase
end
end
endmodule
Hi Romel,
Here's an excellent resource for your MAX II Starter Kit. Complete with plenty of example Verilog Modules to implement various cores. Now you have some working modules by which to practice.
MAX II and MAX CPLD Design Examples
BigDog
by the way Im done with my digital clock but it is not the write way of doing this but Im happy for now I've done it..
second = second+1'b1;
if(second == 60)
begin
second = 0;
if(second == 59)
second <= 0;
else
second <= second+1'b1;
Not bad as a first attempt. I appreciate the clear synchronous design.
I would like to point to one aspect of Verilog programming. You are using "=" blocking assignments throughout the sequential "always@(posedge clk)"
blocks, like below:
This is similar to the way you would handle counting in a C program, but it's not the way, the FPGA works. So the Verilog compiler has to translate your code to something different like:Code:second = second+1'b1; if(second == 60) begin second = 0;
Most textbooks suggest to use non-blocking "<=" assignments in sequential code. If you are proceeding to more complex designs, you'll see why.Code:if(second == 59) second <= 0; else second <= second+1'b1;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?