PWM,脉冲宽度调制。顾名思义,是通过调制脉冲的宽度,即占空比,来实现的。可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象。若用LED灯来显示现象,则可以称作:LED呼吸灯;
简单的代码如下:
/********************************Copyright************************************** **----------------------------File information-------------------------- ** File name :led_pwm.v ** CreateDate :2015.03 ** Funtions :pwm的试验,用led灯来显示pwm的效果 ** Operate on :M5C06N3L114C7 ** Copyright :All rights reserved. ** Version :V1.0 **---------------------------Modify the file information---------------- ** Modified by : ** Modified data : ** Modify Content: *******************************************************************************/ 001 module led_pwm ( 002 clk, 003 rst_n, 004 005 pwm_out 006 ); 007 input clk; /* 24Mhz */ 008 input rst_n; 009 // wire rst_n; 010 output pwm_out; 011 012 // assign rst_n = 1; 013 //------------------------------------- 014 /* ius */ 015 localparam t_1us = 5'd23; 016 // localparam t_1us = 5'd6; /* 用于测试 */ 017 reg [4:0] cnt1; 018 always @(posedge clk or negedge rst_n) 019 begin 020 if(!rst_n) 021 begin 022 cnt1 <= 0 ; 023 end 024 else 025 begin 026 if(cnt1 == t_1us) 027 cnt1 <= 0; 028 else 029 cnt1 <= cnt1 + 1; 030 end 031 end 032 033 034 /* 1ms */ 035 localparam t_1ms = 10'd999; 036 // localparam t_1ms = 10'd19; /* 用于测试 */ 037 reg [9:0] cnt2; 038 always @(posedge clk or negedge rst_n) 039 begin 040 if(!rst_n) 041 begin 042 cnt2 <= 0 ; 043 end 044 else 045 begin 046 if(cnt1 == t_1us) 047 begin 048 if(cnt2 == t_1ms) 049 cnt2 <= 0; 050 else 051 cnt2 <= cnt2 + 1; 052 end 053 else 054 cnt2 <= cnt2; 055 end 056 end 057 058 /* 1s */ 059 localparam t_1s = 10'd999; 060 // localparam t_1s = 10'd19; /* 用于测试 */ 061 reg [9:0] cnt3; 062 always @(posedge clk or negedge rst_n) 063 begin 064 if(!rst_n) 065 begin 066 cnt3 <= 0; 067 end 068 else 069 begin 070 if((cnt1 == t_1us)&&(cnt2 == t_1ms)) 071 begin 072 if(cnt3 == t_1s) 073 cnt3 <= 0; 074 else 075 cnt3 <= cnt3 + 1; 076 end 077 else 078 cnt3 <= cnt3; 079 end 080 end 081 082 reg flag; 083 always @(posedge clk or negedge rst_n) 084 begin 085 if(!rst_n) 086 begin 087 flag <= 0; 088 end 089 else if((cnt1 == t_1us)&&(cnt2 == t_1ms)&&(cnt3 == t_1s)) 090 begin 091 flag <= ~flag; 092 end 093 else 094 flag <= flag; 095 end 096 097 assign pwm_out = flag?((cnt2 < cnt3)?0:1):((cnt2 < cnt3)?1:0); 098 099 endmodule 100 101
仿真验证代码:
/********************************Copyright************************************** **----------------------------File information-------------------------- ** File name :led_pwm_tb.v ** CreateDate :2015.03 ** Funtions : led_pwm 的测试文件 ** Operate on :M5C06N3L114C7 ** Copyright :All rights reserved. ** Version :V1.0 **---------------------------Modify the file information---------------- ** Modified by : ** Modified data : ** Modify Content: *******************************************************************************/ 01 module led_pwm_tb; 02 reg clk; 03 reg rst_n; 04 05 wire pwm_out; 06 07 led_pwm led_pwm_1( 08 .clk, 09 .rst_n, 10 11 .pwm_out 12 ); 13 14 localparam tck = 24; 15 localparam t = 1000/tck; 16 always 17 #(t/2) clk = ~clk; 18 19 initial 20 begin 21 clk = 0; 22 rst_n = 0; 23 24 #(50*t) rst_n = 1; 25 26 end 27 28 endmodule 29 30
仿真结果: