当前位置:网站首页>FPGA: use PWM wave to control LED brightness
FPGA: use PWM wave to control LED brightness
2022-07-28 04:50:00 【Liu Yaner】
Preface
Text
Knowledge points used :
- Use the method of detecting the falling edge to eliminate the shaking of keys
- By generating triangular waves , And then realize variable duty cycle PWM wave
- Generate incremental 、 Decreasing signal
Design documents
// 4 Adjust the brightness once
// Press next 4 Next time , The brightness decreases in turn
module pwm_led_02(
input clk,
input rst_n,
input key,
output reg led
);
parameter CNT_MAX = 12'd1249;
parameter PWM_2 = 12'd249;
parameter PWM_4 = 12'd499;
parameter PWM_6 = 12'd749;
parameter PWM_8 = 12'd999;
reg [11:0]PWM_type;
wire key_nege;// The falling edge of the key
reg key_reg1;
reg key_reg2;
reg cnt_up;
reg cnt_down;
reg key_up;
reg key_down;
reg [2:0] cnt_key;
reg [11:0] cnt_1249;
//====================================
// Detect the falling edge of the key
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n )begin
key_reg1 <= 1'd0;
key_reg2 <= 1'd0;
end
else begin
key_reg1 <= key;
key_reg2 <= key_reg1;
end
end
assign key_nege = key_reg2 & (~key_reg1);
//====================================
//====================================
// use 50MHZ Clock generation 20KHZ Of PWM
// Generating triangular waves
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n )
cnt_1249 <= 12'd0;
else if(key_nege)
cnt_1249 <= 12'd0;
else if(cnt_up)
cnt_1249 <= cnt_1249 + 1;
else if(cnt_down)
cnt_1249 <= cnt_1249 - 1;
else
cnt_1249 <= cnt_1249;
end
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n )begin
cnt_up <= 1'd0;
cnt_down <= 1'd0;
end
else if(cnt_1249 == CNT_MAX-1)begin
cnt_up <= 1'd0;
cnt_down <= 1'd1;
end
else if(cnt_1249 == 0)begin
cnt_up <= 1'd1;
cnt_down <= 1'd0;
end
else begin
cnt_up <= cnt_up;
cnt_down <= cnt_down;
end
end
//====================================
// Key count
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n )
cnt_key <= 3'd0;
else if(key_nege)begin
if(key_up)
cnt_key <= cnt_key + 1;
else if(key_down)//cnt_key[0] &&
cnt_key <= cnt_key - 1;
end
end
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n )begin
key_up <= 1'd0;
key_down <= 1'd0;
end
else if(cnt_key == 4)begin
key_up <= 1'd0;
key_down <= 1'd1;
end
else if(cnt_key == 0)begin
key_up <= 1'd1;
key_down <= 1'd0;
end
else begin
key_up <= key_up;
key_down <= key_down;
end
end
[email protected](*)begin
case(cnt_key)
3'd1:begin
if(key_up)begin
PWM_type = PWM_2;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
else if(key_down)begin
PWM_type = PWM_2;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
end
3'd2:begin
if(key_up)begin
PWM_type = PWM_4;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
else if(key_down)begin
PWM_type = PWM_4;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
end
3'd3:begin
if(key_up)begin
PWM_type = PWM_6;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
else if(key_down)begin
PWM_type = PWM_6;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
end
3'd4:begin
if(key_up)begin
PWM_type = PWM_8;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
else if(key_down)begin
PWM_type = PWM_8;
if(cnt_1249 <= PWM_type)
led = 1'd0;
else if(cnt_1249 > PWM_type )
led = 1'd1;
end
end
default: begin
PWM_type = CNT_MAX;
led = 1'd1;
end
endcase
end
endmodule
The test file
`timescale 1ns/1ns
module tb_pwm (
);
reg clk,rst_n,key;
wire led;
initial begin
clk = 1'd0;
rst_n = 1'd0;
key = 1'd1;
# 100
rst_n = 1'd1;
# 30000
key = 1'd0;
#20000
key = 1'd1;
# 20000
key = 1'd0;
#5000
key = 1'd1;
# 10000
key = 1'd0;
# 30000
key = 1'd1;
# 20000
key = 1'd0;
#5000
key = 1'd1;
# 10000
key = 1'd0;
# 30000
key = 1'd1;
# 30000
key = 1'd0;
#20000
key = 1'd1;
# 20000
key = 1'd0;
#5000
key = 1'd1;
# 10000
key = 1'd0;
# 30000
key = 1'd1;
# 20000
key = 1'd0;
#5000
key = 1'd1;
# 10000
key = 1'd0;
# 30000
key = 1'd1;
end
always #10 clk = ~ clk;
pwm_led_02 pwm_led_inst(
.clk(clk),
.rst_n(rst_n),
.key(key),
.led(led)
);
endmodule
On board test

边栏推荐
- Printf() print char* str
- 【Oracle】083错题集
- Odoo action analysis (action.client, action.act_window, action.server)
- NAT基本原理与私有IP
- [Sylar] practical part - redis based parameter query service
- Method of converting UI file to py file
- Inspire domestic students to learn robot programming education for children
- Mac installs mysql5.7 through brew
- Cloudcompare & PCL point cloud least square fitting plane
- Angr (XI) - official document (Part2)
猜你喜欢

100 lectures on Excel practical application cases (XI) - tips for inserting pictures in Excel

01 node express system framework construction (express generator)
![(2.4) [service Trojan -slimftp] introduction and use](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
(2.4) [service Trojan -slimftp] introduction and use

Observable time series data downsampling practice in Prometheus

Leetcode 454. Adding four numbers II

Easycvr Video Square snapshot adding device channel offline reason display

CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?

Constructor of member function

Method of converting UI file to py file

Domain name (subdomain name) collection method of Web penetration
随机推荐
[daily one] visual studio2015 installation in ancient times
could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
When initializing with pyqt5, super() and_ init _ () problems faced by the coordinated use of functions, as well as the corresponding learning and solutions
[Sylar] framework -chapter11 socket module
[每日一氵]上古年代的 Visual Studio2015 安装
Web渗透之域名(子域名)收集方法
Niuke, convert string to integer
Reading the paper "learning span level interactions for aspect sentimental triple extraction"
np. The data returned from delete details is the data after deleting the specified dimension
Sort - cardinal sort
Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
外卖系统 文件上传
String 0123456789abcdef, what is the number of substrings (not empty and not the same string itself) [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
塑料可以执行GB/T 2408 -燃烧性能的测定吗
Odoo action analysis (action.client, action.act_window, action.server)
低代码是开发的未来吗?浅谈低代码平台
Blooming old trees -- quickly build a map bed application with imageprocessor
[Sylar] framework chapter -chapter10-address module
网络安全基本知识——密码(一)
App test process and test points