ALTERA DE2 之 verilog HDL 学习笔记03 FPGA的PWM输出

PWM输出控制一个LED灯,调整输出信号的占空比即可改变LED灯的亮度。

由于程序块module是并行的,所以产生时钟模块非常的方便,不再需要像单片机那样靠定时器中断。

代码非常简单,一看就能明白。

该程序通过调整SW0~SW4的组合状态来改变输出PWM---LEDG

module PWM_module
(
    input clk,
    input rst_n,
    input [4:0] option_key,
    output led
);
    parameter SEGMENT = 8'd195;    //3.9us
    
    reg [7:0] c1;
    
    always @(posedge clk or negedge rst_n)
        if (!rst_n)
            c1 <= 7'd0;
        else if (c1 == SEGMENT)
            c1 <= 7'd0;
        else
            c1 <= c1 + 1'b1;
            
    reg [7:0] system_seg;
	 
    always @(posedge clk or negedge rst_n)
        if (!rst_n)
            system_seg <= 8'd0;
        else if (system_seg == 8'd255)
            system_seg <= 8'd0;
        else if (c1 == SEGMENT)
            system_seg <= system_seg + 1'b1;
            
    reg [7:0] option_seg;

    always @(posedge clk or negedge rst_n)
        if (!rst_n)
            option_seg <= 8'd0;
        else if (option_key[4])    //segment + 10
            if (option_seg < 8'd245) 
                option_seg <= option_seg + 8'd10;
            else 
                option_seg <= 8'd255;
        else if (option_key[3])    //segment - 10
            if (option_seg > 8'd10)
                option_seg <= option_seg - 8'd10;
            else
                option_seg <= 8'd0;
        else if (option_key[2])    //segment + 1
            if (option_seg < 8'd255)
                option_seg <= option_seg + 8'd1;
            else
                option_seg <= 8'd255;
        else if (option_key[1])    //segment - 1
            if (option_seg > 8'd0)
                option_seg <= option_seg - 8'd1;
            else
                option_seg <= 8'd0;
        else if (option_key[0])    //segment = half
            option_seg <= 8'd127;
            
    assign led = (system_seg < option_seg) ? 1'b1: 1'b0;

endmodule
我的引脚分配图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值