PWM----调节LED亮度

-

-

--调节两个LED灯亮度

 1 module led_pwm (
 2     clk, 
 3     rst, 
 4 //cnt1_pwm, 
 5     out1,
 6     out2,
 7     out3,
 8     out4
 9 );
10 
11     input clk, rst;
12     //input [19:0] count_pwm;
13     output reg out1, out2;
14     output out3, out4;
15     
16     reg [19:0] cnt;
17     reg [19:0] cnt1_pwm = 21'd2, cnt2_pwm = 21'd52_4288;
18     reg [25:0] cnt_s;
19     
20     always @ (posedge clk, negedge rst)
21         if (!rst)
22             begin 
23                 cnt_s <= 0;
24                 cnt1_pwm <= 21'd1;
25                 cnt2_pwm <= 21'd52_4288;
26             end 
27         else if (cnt_s == 26'd1000_0000) //计0.2秒
28             begin 
29                 cnt_s <= 0;
30                     cnt1_pwm <= {cnt1_pwm[18:0], cnt1_pwm[19]}; //调节脉宽
31                     cnt2_pwm <= {cnt2_pwm[0], cnt2_pwm[19:1]};
32                         
33                     //cnt1_pwm <= (cnt1_pwm >> 2)|(cnt1_pwm << 19);//right
34                     //cnt2_pwm <= (cnt2_pwm << 2)|(cnt2_pwm >> 19);//left
35             end 
36         else 
37             cnt_s <= cnt_s + 1;
38             
39     //
40     always @ (posedge clk, negedge rst)
41         if (!rst)
42             cnt <= 21'h0;
43         else if (cnt == 21'd55_0000)  //一个周期
44             cnt <= 21'b0;
45         else 
46             cnt <= cnt + 1'b1;
47             
48     
49     //    pwm生成
50     always @ (posedge clk, negedge rst)
51         if (!rst)
52             out1 <= 1'b0;
53         else if (cnt <= cnt1_pwm)
54             out1 <= 1'b0;
55         else
56             out1 <= 1'b1;
57             
58     always @ (posedge clk, negedge rst)
59         if (!rst)
60             out2 <= 1'b0;
61         else if (cnt <= cnt2_pwm)
62             out2 <= 1'b0;
63         else
64             out2 <= 1'b1;
65             
66     assign out3 = 1'b0;
67     assign out4 = 1'b0;
68     
69 endmodule

 

posted @ 2017-09-29 22:56  纟彖氵戋  阅读(397)  评论(0编辑  收藏  举报