|
2 | 2 | //-- Using PLL for 48Mhz clock. |
3 | 3 | //------------------------------------------------------------------ |
4 | 4 |
|
5 | | -module main ( |
6 | | - input ext_clk, // 12MHz clock |
7 | | - output LED // LED to blink |
| 5 | +module main #( |
| 6 | + parameter integer DIV = 8_000_000 |
| 7 | +) ( |
| 8 | + input ext_clk, // 12MHz clock |
| 9 | + output reg LED // LED to blink |
8 | 10 | ); |
9 | 11 |
|
10 | | - // PLL 12Mhz -> 28Mhz |
| 12 | + // PLL 12Mhz -> 48Mhz |
11 | 13 | // |
12 | 14 | // Generated with: |
13 | 15 | // apio raw -- icepll -i 12 -o 48 -q -m -f pll.v |
14 | 16 | // apio format pll.v |
15 | 17 | // |
16 | 18 | // (See pll.v for additional args that were added manually) |
17 | 19 |
|
| 20 | + // 48 Mhz clock from the pll. |
18 | 21 | wire sys_clk; |
19 | 22 |
|
| 23 | + // Generates the main clock . |
20 | 24 | pll pll ( |
21 | | - .clock_in(ext_clk), |
22 | | - .clock_out(sys_clk), |
| 25 | + .clock_in(ext_clk), // In 12 Mhz |
| 26 | + .clock_out(sys_clk), // Out 48 Mhz |
23 | 27 | .locked() |
24 | 28 | ); |
25 | 29 |
|
26 | | - reg [23:0] counter = 0; |
| 30 | + reg initialized = 1'b0; |
| 31 | + |
| 32 | + reg [31:0] counter; |
| 33 | + |
| 34 | + always @(posedge sys_clk) begin |
| 35 | + if (!initialized) begin |
| 36 | + // One time initialization. |
| 37 | + initialized <= 1'b1; |
| 38 | + LED <= 1'b1; |
| 39 | + counter <= 0; |
| 40 | + end else if (counter >= (DIV - 1)) begin |
| 41 | + // Delay reached, flip LED and and reset counter. |
| 42 | + LED <= !LED; |
| 43 | + counter <= 0; |
| 44 | + end else begin |
| 45 | + // Incrementing the delay counter. |
| 46 | + counter <= counter + 1; |
| 47 | + end |
| 48 | + end |
27 | 49 |
|
28 | | - always @(posedge sys_clk) counter <= counter + 1; |
29 | 50 |
|
30 | | - // Should blink at about 2.82Hz |
31 | | - // 48M / 2^24 = 2.82 |
32 | | - assign LED = counter[23]; |
33 | 51 |
|
34 | 52 | endmodule |
35 | 53 |
|
|
0 commit comments