**这是本文档旧的修订版!**

PWM亮度控制设计代码

// --------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------
// Module: Lightness
// 
// Author: Step
// 
// Description: Lightness control
// 
// Web: www.ecbcamp.com
//
// --------------------------------------------------------------------
// Code Revision History :
// --------------------------------------------------------------------
// Version: |Mod. Date:   |Changes Made:
// V1.0     |2015/11/11   |Initial ver
// --------------------------------------------------------------------
module Lightness
(
input			clk_in,
input			rst_n_in,
input			up_pulse,
input			down_pulse,
output	reg		Lightness_out
);
 
localparam	CYCLE = 10;
 
reg	[3:0] duty;
//Control duty cycle
always @(posedge clk_in or negedge rst_n_in) begin 
	if(!rst_n_in) begin 
		duty<=4'd5;
	end else begin
		if(up_pulse && (duty<CYCLE)) duty <= duty + 4'd1;
		else if(down_pulse && (duty>4'd0)) duty <= duty - 4'd1;
		else duty <= duty;
	end
end 
 
reg	[3:0]	cnt;
//counter for cycle
always @(posedge clk_in or negedge rst_n_in) begin 
	if(!rst_n_in) begin 
		cnt<=4'd0;
	end else begin
		if(cnt>=CYCLE) cnt<=4'd0;
		else cnt <= cnt + 4'd1;
	end 
end 
 
//pulse generate with duty
always @(posedge clk_in or negedge rst_n_in) begin 
	if(!rst_n_in) begin 
		Lightness_out<=1'b1;
	end else begin
		if(cnt<=duty) Lightness_out<=1'b1;
		else Lightness_out<=1'b0;
	end 
end 
 
endmodule