pwm呼吸灯

文章目录


一、呼吸灯

呼吸灯是指灯光在微电脑的控制之下完成由亮到暗的逐渐变化,使用开发板上的四个led灯实现1s间隔的呼吸灯。

二、代码实现

c 复制代码
```c
module pwm_led( 
    input				clk		,
    input				rst_n	,
    
    output reg [3:0]    led
);								 
parameter CNT_US = 6'd49;//50x20=1000ns=1us
parameter CNT_MS = 10'd999;//1usx1000=1ms
parameter CNT_S  = 10'd999;//1msx1000=1s

reg [5:0] cnt_us;
wire      add_cnt_us;
wire      end_cnt_us;

reg [9:0] cnt_ms;
wire      add_cnt_ms;
wire      end_cnt_ms;

reg [9:0] cnt_s;
wire      add_cnt_s;
wire      end_cnt_s;

reg flag;//闪烁标志
always @(posedge clk or negedge rst_n)begin 
   if(!rst_n)begin
        cnt_us <= 0;
    end 
    else if(add_cnt_us)begin 
            if(end_cnt_us)begin 
                cnt_us <= 0;
            end
            else begin 
                cnt_us <= cnt_us + 1;
            end 
    end
   else  begin
       cnt_us <= cnt_us;
    end
end 

assign add_cnt_us = 1'd1;
assign end_cnt_us = add_cnt_us && cnt_us == CNT_US;

always @(posedge clk or negedge rst_n)begin 
   if(!rst_n)begin
        cnt_ms <= 0;
    end 
    else if(add_cnt_ms)begin 
            if(end_cnt_ms)begin 
                cnt_ms <= 0;
            end
            else begin 
                cnt_ms <= cnt_ms + 1;
            end 
    end
   else  begin
       cnt_ms <= cnt_ms;
    end
end 

assign add_cnt_ms = end_cnt_us;
assign end_cnt_ms = add_cnt_ms && cnt_ms == CNT_MS;

always @(posedge clk or negedge rst_n)begin 
   if(!rst_n)begin
        cnt_s <= 0;
    end 
    else if(add_cnt_s)begin 
            if(end_cnt_s)begin 
                cnt_s <= 0;
            end
            else begin 
                cnt_s <= cnt_s + 1;
            end 
    end
   else  begin
       cnt_s <= cnt_s;
    end
end 

assign add_cnt_s = end_cnt_ms;
assign end_cnt_s = add_cnt_s && cnt_s == CNT_S;

always @(posedge clk or negedge rst_n)begin 
    if(!rst_n)begin
        flag <= 1'b0;
    end 
    else if(end_cnt_s)begin 
        flag <= ~flag;//1s取反
    end 
    else begin 
        flag <= flag;
    end 
end

always @(posedge clk or negedge rst_n)begin 
    if(!rst_n)begin
        led <= 4'b0;
    end 
    else begin
        if(flag)begin//亮pwm
            led <= {cnt_s > cnt_ms, cnt_s > cnt_ms,cnt_s > cnt_ms,cnt_s > cnt_ms};
        end 
        else begin//灭pwm
            led <= {cnt_s < cnt_ms, cnt_s < cnt_ms,cnt_s < cnt_ms,cnt_s < cnt_ms};
        end
    end 
end
endmodule

三、引脚分配

相关推荐
szxinmai主板定制专家1 小时前
【精密测量】基于ARM+FPGA的多路光栅信号采集方案
服务器·arm开发·人工智能·嵌入式硬件·fpga开发
千宇宙航8 小时前
闲庭信步使用SV搭建图像测试平台:第三十二课——系列结篇语
fpga开发
千宇宙航13 小时前
闲庭信步使用SV搭建图像测试平台:第三十一课——基于神经网络的手写数字识别
图像处理·人工智能·深度学习·神经网络·计算机视觉·fpga开发
小眼睛FPGA1 天前
【RK3568+PG2L50H开发板实验例程】FPGA部分/紫光同创 IP core 的使用及添加
科技·嵌入式硬件·ai·fpga开发·gpu算力
forgeda1 天前
如何将FPGA设计验证效率提升1000倍以上(2)
fpga开发·前沿技术·在线调试·硬件断点·时钟断点·事件断点
9527华安2 天前
FPGA实现40G网卡NIC,基于PCIE4C+40G/50G Ethernet subsystem架构,提供工程源码和技术支持
fpga开发·架构·网卡·ethernet·nic·40g·pcie4c
search72 天前
写Verilog 的环境:逻辑综合、逻辑仿真
fpga开发
search72 天前
Verilog 语法介绍 1-1结构
fpga开发
小眼睛FPGA2 天前
【RK3568+PG2L50H开发板实验例程】Linux部分/FPGA dma_memcpy_demo 读写案例
linux·运维·科技·ai·fpga开发·gpu算力
幸运学者2 天前
xilinx axi datamover IP使用demo
fpga开发