EmbDev.net

Forum: FPGA, VHDL & Verilog Help Beginner Make Stop watch


von John (Guest)


Rate this post
useful
not useful
Trying to make a stop watch that will stop when you hit the stop button 
that is what the while loop is for. I am an amateur and would really 
appreciate any help.

Thank You!

ERROR:HDLCompiler:806 - "/home/melvin/Desktop/Modern 
DIG/DashClock/DashClock.v" Line 85: Syntax error near "else".

1
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date:    14:20:50 04/10/2015 
7
// Design Name: 
8
// Module Name:    DashClock 
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module DashClock(
22
  input  START, STOP, CSS, RESET, clk,
23
  output a,b,c,d,e,f,g
24
    );
25
   reg [3:0] Enable;
26
   reg [15:0]TM;
27
   reg [18:0] Count ;
28
   wire Count_Max;
29
   
30
  always @(posedge clk)  //clk pulse at every 20ns
31
   begin
32
  
33
    if(Count == 500000)
34
       Count <= 0;
35
    else if (Count <= 500000)
36
       Count <= Count + 1;
37
   end
38
  
39
      assign Count_Max = ((Count == 500000)?1'b1:1'b0);
40
      
41
      
42
    
43
    reg Stop_Flg = 0;
44
  
45
    always @(posedge clk or posedge RESET)
46
    begin
47
    if(RESET)
48
      begin 
49
      TM [3:0] <= 0;
50
      TM [3:0] <= 0;
51
      TM [3:0] <= 0;
52
      TM [3:0] <= 0;
53
      end
54
        
55
    if (START == 1)  
56
      begin
57
      TM <= 0;
58
      
59
      
60
while (STOP !== Stop_Flg)
61
   begin
62
63
     if(Count_Max)
64
  begin  
65
66
   if(TM [3:0] == 4'b1001)
67
      begin
68
69
        TM [3:0] <= 4'b0000;
70
    if(TM [7:4] == 9)
71
                     begin
72
          TM [7:4] <= 4'b0000;
73
74
      if(TM [11:8] == 9)
75
          begin
76
            TM [11:8] <= 4'b0000;
77
78
        if(TM [15:12] == 9)
79
            begin
80
                Stop_Flg <= 1;
81
        else  // line 85 error                                                                                                                       
82
           TM [15:12] <= TM [15:12] +1;
83
                                end
84
85
         else 
86
                       TM [11:8] <= TM [11:8] +1;
87
               end
88
89
             else
90
        TM [7:4] <= TM [7:4] +1;
91
      end
92
                    
93
    else
94
      TM [3:0] <= TM [3:0] + 1;
95
    end// TM [3:0]
96
          
97
             end // If statement Count Max
98
         end // while
99
       end // IF Statement Start =1?
100
     end // always @ Clock
101
  
102
   reg [17:0] Mux_Count;
103
  reg [6:0]  Sev_Seg;
104
  reg        DP;
105
  
106
  always @ (posedge clk)
107
    begin
108
      Mux_Count <= Mux_Count +1;
109
    end
110
    
111
  always @ (*)
112
    begin
113
      case(Mux_Count[17:16])
114
       
115
       2'b00 :
116
        begin
117
        Sev_Seg <= TM[3:0];
118
        Enable <= 4'b0001;
119
        DP <= 0;
120
        end
121
        
122
       2'b01 :
123
        begin
124
        Sev_Seg <= TM[7:4];
125
        Enable <= 4'b0010;
126
        DP <= 1;
127
        end
128
        
129
       2'b10 :
130
        begin
131
        Sev_Seg <= TM[11:8];
132
        Enable = 4'b0100;
133
        DP <= 0;
134
        end
135
        
136
       2'b11 :
137
        begin
138
        Sev_Seg <= TM[15:12];
139
        Enable <= 4'b1000;
140
        DP <= 0;
141
        end
142
      endcase
143
     end
144
     
145
    reg [6:0] Convert;
146
    always @ (*)
147
      begin
148
       case(Sev_Seg)
149
        4'b0000 : Convert <= 7'b1111110;
150
        4'b0001 : Convert <= 7'b0110000;
151
        4'b0010 : Convert <= 7'b1101101;
152
        4'b0011 : Convert <= 7'b1111001;
153
        4'b0100 : Convert <= 7'b0110011;
154
        4'b0101 : Convert <= 7'b1011011;
155
        4'b0110 : Convert <= 7'b1011111;
156
        4'b0111 : Convert <= 7'b1110000;
157
        4'b1000 : Convert <= 7'b1111111;
158
        4'b1001 : Convert <= 7'b1111011;
159
        endcase
160
      end
161
    assign {a,b,c,d,e,f,g} = Convert;
162
    
163
    
164
endmodule

von Lattice User (Guest)


Rate this post
useful
not useful
John wrote:
>
> ERROR:HDLCompiler:806 - "/home/melvin/Desktop/Modern
> DIG/DashClock/DashClock.v" Line 85: Syntax error near "else".
>

This is caused by a nesting error. 2 lines before is an unclosed 
"begin".
Hard to spot, because of very bad indentation.

But there is a big rookie error, many beginners with programming (C, 
etc) background make: The while does not do what you expect!

Beginners should use while/for/... loops only in TEST benches, but never 
in anything which is targeted to hardware (i.e. FPGA).
Create a finite state machine instead.

(Your LED muxer is an example of a simple FSM)

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
John wrote:
> Trying to make a stop watch
Try this original code there:
http://simplefpga.blogspot.de/2012/07/to-code-stopwatch-in-verilog.html

BTW: there's a design flaw in it making the clock inaccurate for 
1/5000000. "That's not that much!" you may say, but in fact its a very 
basic problem in a beginners design. The prescaler counts 5000001 steps 
instead only 5000000, one step too much... :-o

: Edited by Moderator
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
One more thing is this here:

if(RESET)
begin
  TM [3:0] <= 0;
  TM [3:0] <= 0;
  TM [3:0] <= 0;
  TM [3:0] <= 0;
end

Nice, isn't it? :-o

: Edited by Moderator
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.