Hi all,
I am a student and I am working on a homework. Shortly, I will summary
shortly the hw's requirement. It said some thing like that: At first,
when counter goes from 0->7 then led 1 on; when the counter up to 8 then
led 2 on, up to 9 then led 3 on. Next, when the counter comes to 10, it
depends on a switch, if switch is on, led 4 on, if switch is off led 5
on. And if led 4 on, it still to be on until counter comes to 14 then
counter reset to 0 and comes back to first stage which means led 1 on.
And similarly, if led 5 on, it still to be on until counter comes to 22
then counter reset to 0 and comes back to first stage which means led 1.
The requirement is described by a lots of word but shortly it will be
like that after I design FSM for the problem. When I finish my code,
test bench runs OK but when I run on Altera DE1 board, it did not run
like my expectation. When the counter comes to 8,9,10 it runs nicely but
next when led 4 or led 5 on, it immediately comes back to stage 1 (led 1
on). Led 4 should be on 4 seconds or led 5 should be on 12 seconds
before come back to stage 1. In test bench, it waiting but in the board
nothing is waiting.
This is my code, hope you guys help me.
1 | module controller(sw, clk, rst, led, y, Y); //This is top level module
|
2 | input sw, clk, rst;
|
3 | output [4:0] led;
|
4 | output reg [2:0]y=0,Y=0; //y: present state; Y: next state
|
5 | parameter [2:0] fer=0, ready=1, prepare=2, transfer=3, flush=4; //define finite state machine
|
6 | reg [4:0] count=0; //counter
|
7 |
|
8 | frequency_divider C1(clk, clk_out); //divider frequency for DE1 board, I use pin L1 (50Mhz)
|
9 |
|
10 |
|
11 |
|
12 | always@(count, sw,y)
|
13 | case(y)
|
14 | fer: if (count==8)
|
15 | Y=ready;
|
16 | ready: if (count==9)
|
17 | Y=prepare;
|
18 | prepare: if ((count==10) & (sw==0))
|
19 | Y=flush;
|
20 | else if ((count==10) & (sw==1))
|
21 | Y=transfer;
|
22 | transfer: if (count==14)
|
23 | Y=fer;
|
24 | flush: if ( count==22)
|
25 | Y=fer;
|
26 | default: Y=2'bxx;
|
27 | endcase
|
28 |
|
29 | endcase
|
30 |
|
31 |
|
32 | always @(negedge rst, posedge clk_out)
|
33 | if (!rst)
|
34 | y<=fer;
|
35 | else
|
36 | y<=Y;
|
37 |
|
38 | always @(negedge rst, posedge clk_out)
|
39 | if (!rst)
|
40 | count = 0;
|
41 | else begin
|
42 | count = count + 1;
|
43 | if (y==transfer && count==15) //This to reset the counter
|
44 | count=0;
|
45 | else if (y==flush && count==23)
|
46 | count=0;
|
47 | end
|
48 |
|
49 | assign led[0]=(y==fer); // led 0 on when the system in fermertation stage (stage 1)
|
50 | assign led[1]=(y==ready);
|
51 | assign led[2]=(y==prepare);
|
52 | assign led[3]=(y==transfer);
|
53 | assign led[4]=(y==flush);
|
54 |
|
55 | endmodule
|
56 |
|
57 | module frequency_divider(clk_in, clk_out);
|
58 |
|
59 | input clk_in;
|
60 | output clk_out;
|
61 | reg [31:0] num=0;
|
62 | wire clk_out=num[24];
|
63 | always @(posedge clk_in) begin
|
64 | num <= num+ 1;
|
65 | end
|
66 | endmodule
|
67 |
|
68 |
|
69 | module test_controller; //test bench
|
70 | wire [4:0]led;
|
71 | reg sw, clk, rst;
|
72 | controller C1(sw, clk, rst, led);
|
73 |
|
74 | initial begin
|
75 | sw=0;
|
76 | clk=0;
|
77 | rst=0;
|
78 | #2rst=1;
|
79 | end
|
80 |
|
81 | always begin
|
82 | #1 clk=~clk;
|
83 | end
|
84 | always begin
|
85 | #100 sw=~sw;
|
86 | end
|
87 |
|
88 | endmodule
|