PWM----调节LED亮度

Posted yllinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 = 21d2, cnt2_pwm = 21d52_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 <= 21d1;
25                 cnt2_pwm <= 21d52_4288;
26             end 
27         else if (cnt_s == 26d1000_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 <= 21h0;
43         else if (cnt == 21d55_0000)  //一个周期
44             cnt <= 21b0;
45         else 
46             cnt <= cnt + 1b1;
47             
48     
49     //    pwm生成
50     always @ (posedge clk, negedge rst)
51         if (!rst)
52             out1 <= 1b0;
53         else if (cnt <= cnt1_pwm)
54             out1 <= 1b0;
55         else
56             out1 <= 1b1;
57             
58     always @ (posedge clk, negedge rst)
59         if (!rst)
60             out2 <= 1b0;
61         else if (cnt <= cnt2_pwm)
62             out2 <= 1b0;
63         else
64             out2 <= 1b1;
65             
66     assign out3 = 1b0;
67     assign out4 = 1b0;
68     
69 endmodule

 

以上是关于PWM----调节LED亮度的主要内容,如果未能解决你的问题,请参考以下文章

Windows 10 IoT Core用PWM控制器控制树莓派LED灯亮度

Windows 10 IoT Core用PWM控制器控制树莓派LED灯亮度

4-ESP8266+onenet+STM32定时器的PWM应用(onenet云平台远程控制LED灯的亮度)

ESP8266 Web Server (WebSocket) 实现多控件多引脚调节(PWM)

Arduino ESP32 Web Service PWM控制led亮度

ESP32 Web Server (WebSocket) 实现多控件多引脚调节(PWM)