当前位置:网站首页>FPGA:使用PWM波控制LED亮度
FPGA:使用PWM波控制LED亮度
2022-07-28 04:48:00 【刘颜儿】
前言
正文
用到的知识点:
- 用检测下降沿的方法进行按键消抖
- 通过生成三角波,进而实现可变占空比的PWM波
- 生成递增、递减的信号
设计文件
// 4次调节亮度
// 按下第4次后,亮度依次减小
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;//按键下降沿
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;
//====================================
//检测按键下降沿
[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);
//====================================
//====================================
// 用50MHZ时钟生成20KHZ的PWM
//生成三角波
[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
//====================================
// 按键计数
[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
测试文件
`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
上板测试

边栏推荐
- printf()打印char* str
- Inspire domestic students to learn robot programming education for children
- Is low code the future of development? On low code platform
- 【sylar】框架篇-Chapter10-Address 模块
- (克隆虚拟机步骤)
- Reading the paper "learning span level interactions for aspect sentimental triple extraction"
- Improve the core quality of steam education among students
- 【sylar】框架篇-Chapter6-协程调度模块
- Important SQL server functions - numeric functions
- Method of converting UI file to py file
猜你喜欢

启发国内学子学习少儿机器人编程教育

Important SQL server functions - string utilities

ADB environment configuration

could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node

Important SQL server functions - numeric functions

物联网工业串口转WiFi模块 无线路由WiFi模块的选型

pytorch_ Lightning in lightning_ The output of hparams.yaml in logs is null

Mysql database -- first knowledge database

王爽汇编语言详细学习笔记三:寄存器(内存访问)

Zhejiang University and other recent review papers on deep learning new drug design
随机推荐
RuntimeError: stack expects each tensor to be equal size, but got [8] at entry 0 and [2] at entry 2
【sylar】框架篇-Chapter11-Socket 模块
excel实战应用案例100讲(十一)-Excel插入图片小技巧
Web渗透之域名(子域名)收集方法
Constructor of member function
【sylar】框架篇-Chapter23-模块篇总结
linux下安装mysql
(clone virtual machine steps)
王爽汇编语言详细学习笔记三:寄存器(内存访问)
Redis类型
Take out system file upload
Pyqt based grouping tool
Explain initialization list
【Oracle】083错题集
could only be written to 0 of the 1 minReplication nodes. There are 0 datanode(s) running and 0 node
Design and development of C language ATM system project
np. The data returned from delete details is the data after deleting the specified dimension
Mac installs mysql5.7 through brew
np. unravel_ Index() finds the index value of an element (or group of elements) of the array after being pulled into one dimension. The corresponding index value in the original dimension (or specify
Redis type