/*自己做的PWM程序
*2014-01-09
*/
module pwm(clk,rst,led);
input clk,rst;
output [7:0] led;
parameter T=31'd2000000;
parameter C=31'd200_0;
reg [31:0] cnt;
reg [31:0] pul;
reg on;
always @ (posedge clk or negedge rst)
if(!rst)
begin
cnt<=0;
pul<=0;
end
else if(T==cnt)
begin
cnt<=0;
pul<=pul+100;
if(pul==C)
pul<=0;
end
else
begin
cnt<=cnt+1;
end
reg [31:0] div;
always @ (posedge clk or negedge rst)
if(!rst)
begin
div<=0;
end
else
begin
div<=div+1;
if(div==C)
div<=0;
if(div<pul)
on<=1;
else
on<=0;
end
assign led[1]=on;
assign led[0]=1;
assign led[2]=1;
assign led[3]=1;
assign led[4]=1;
assign led[5]=1;
assign led[6]=1;
assign led[7]=1;
endmodule