dianzi67的个人空间 https://blog.eetop.cn/loveer [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

PWM controller based on Verilog

已有 2069 次阅读| 2010-8-8 10:14 |个人分类:EE-Designs

天气: 阴雨
心情: 平静

PWM controller based on Verilog:

//-----------------------------32 bits pwm controller------------------------//
//the width is negative
module pwm(clk,write_data,cs,write_n,addr,clr_n,read_data,pwm_out);
//----------------------define the inout put-------------------------//
input clk,cs,clr_n;
input addr;
input write_n;
output pwm_out;
output [31:0] read_data;
input [31:0] write_data;
//--------------------define regs and wires--------------------------//
reg [31:0] period;
reg [31:0] pulse_width;
reg [31:0] counter;
reg off;
reg [31:0] read_data;
wire period_en, pulse_width_en; //enable to write
//-------------------------------------------------------------------//
//define the content of period and pulse_width
always @(posedge clk or negedge clr_n)
 begin
  if (clr_n==0)
   begin
   period<=32'h 00000000;
   pulse_width<=32'h 00000000;
   end
  else
   begin
    if (period_en)//write in preiod data
     period<=write_data[31:0];
    else
     period<=period;
    if (pulse_width_en)//write in width data
     pulse_width<=write_data[31:0];
          else
           pulse_width<=pulse_width;
         end
    end
//------------------------------------------------------------------//
//visit the regs
always @(addr or period or pulse_width)
    begin
     if (addr == 0)
     read_data=period;
     else
     read_data=pulse_width;
    end
//-------------------------------------------------------------------//
//the logic to generate pwm
//the period of pwm
always @(posedge clk or negedge clr_n)
 begin
  if (clr_n==0)
  counter<=0;
  else
  begin
  if (counter>=period-1)
     counter<=0;
     else
     counter<=counter+1;
     end
    end
//-----------------------------------------------------//
//ajust the width of pwm
always @(posedge clk or negedge clr_n)
 begin
  if (clr_n==0)
  off<=0;
  else
   if (counter>=pulse_width)
      off <= 1;
   else
      if (counter==0)
      off<=0;
      else
      off<=off;
    end

assign period_en = cs & !write_n & !addr;
assign pulse_width_en = cs & !write_n & addr;
//PWM output
assign pwm_out=!off;
endmodule

 

 


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 2

    粉丝
  • 1

    好友
  • 4

    获赞
  • 28

    评论
  • 490

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-13 09:04 , Processed in 0.013015 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部