• pwm呼吸灯



    一、呼吸灯

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

    二、代码实现

    
    ```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
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    三、引脚分配

    请添加图片描述

  • 相关阅读:
    Python装饰器探究
    iperf 测试网络性能
    leetcode/狒狒吃香蕉
    Eureka详解
    docker启动命令,docker重启命令,docker关闭命令
    leetcode刷题
    跨国企业扎根中国市场,应该选择什么样的云服务?
    Windows timeSetEvent定时器
    js逆向基础篇-某音乐网站-xx音乐
    《视觉SLAM十四讲》-- 后端 2
  • 原文地址:https://blog.csdn.net/the_same_bug/article/details/131751954