PWM的LEDG灯控制

通过PWM控制LEDG灯的频率

1 module pwm(
2 clk,
3 reset_n,
4 high_dur,
5 total_dur,
6 pwm,
7 toggle
8 );
9
10  input clk;
11  input reset_n;
12  input [31:0] high_dur;
13 input [31:0] total_dur;
14 output pwm;
15 output reg toggle;
16
17 reg [31:0] tick;
18
19 always @ (posedge clk or negedge reset_n)
20 begin
21 if (!reset_n)
22 begin
23 tick <= 0;
24 toggle <= 0;
25 end
26 else if (tick >= total_dur)
27 begin
28 tick <= 0;
29 toggle <= ~toggle;
30 end
31 else
32 tick <= tick + 1;
33 end
34
35 assign pwm = (tick < high_dur)?1'b1:1'b0;
36
37 endmodule
38

 

可以通过 SW按钮来控制LED

1 module DE0_PWM_LED(
2 //////////////////// Clock Input ////////////////////
3 CLOCK_50, // 50 MHz
4 //////////////////////// LED ////////////////////////
5 LEDG, // LED Green[9:0]
6 //////////////////// DPDT Switch ////////////////////
7 SW, // Toggle Switch[9:0]
8 reset_n
9 );
10
11 //////////////////////// Clock Input ////////////////////////
12 input CLOCK_50; // 50 MHz
13 //////////////////////////// LED ////////////////////////////
14 output [9:0] LEDG; // LED Green[9:0]
15 //////////////////////// DPDT Switch ////////////////////////
16 input [9:0] SW; // Toggle Switch[9:0]
17 input reset_n;
18 //=======================================================
19 // REG/WIRE declarations
20 //=======================================================
21 wire reset_n;
22 //assign reset_n = 1'b1;
23 wire [9:0] MAX_LIGHT;
24 //`define MAX_LIGHT 8'hFF
25 wire [11:0] MAX_LIGHT_TICK;
26 //`define MAX_LIGHT_TICK 11'h7FF
27
28 reg [8:0] light;
29 reg [9:0] light_tick;
30 reg lighter;
31 wire toggle;
32
33
34 //=======================================================
35 // Structural coding
36 //=======================================================
37
38 //
39 always @ (posedge toggle or negedge reset_n)
40 begin
41 if (!reset_n)
42 begin
43 light_tick <= 0;
44 light <= 0;
45 lighter <= 1'b1;
46 end
47 else if (light_tick >= MAX_LIGHT_TICK)
48 begin
49 light_tick <= 0;
50 if (lighter)
51 begin
52 if (light >= MAX_LIGHT)
53 lighter <= 1'b0;
54 else
55 light <= light + 1'b1;
56 end
57 else
58 begin
59 if (light == 0)
60 lighter <= 1'b1;
61 else
62 light <= light - 1'b1;
63 end
64 end
65 else
66 light_tick <= light_tick + 1'b1;
67 end
68
69 wire led_pwm;
70 wire [8:0] linear_light;
71 // linearlize process: input pwm->led illuminance (user can optimal linearity here)
72 assign linear_light = (light < MAX_LIGHT/2)?light/4:(MAX_LIGHT/10+(light-MAX_LIGHT/2)*9/4);
73
74 pwm pwm_inst(
75 .clk(CLOCK_50),
76 .reset_n(reset_n),
77 .high_dur(linear_light),
78 .total_dur(MAX_LIGHT),
79 .pwm(led_pwm),
80 .toggle(toggle)
81 );
82
83 assign LEDG[0] = led_pwm;
84 assign LEDG[1] = led_pwm;
85 assign LEDG[2] = led_pwm;
86 assign LEDG[3] = led_pwm;
87 assign LEDG[4] = led_pwm;
88 assign LEDG[5] = led_pwm;
89 assign LEDG[6] = led_pwm;
90 assign LEDG[7] = led_pwm;
91 assign LEDG[8] = led_pwm;
92 assign LEDG[9] = led_pwm;
93
94
95 assign MAX_LIGHT = SW;
96 assign MAX_LIGHT_TICK = MAX_LIGHT<<1;
97
98 endmodule
99
100
101

 

 

posted on 2010-11-26 11:08  齐威王  阅读(1581)  评论(0编辑  收藏  举报

导航