[笔记].小练Verilog版本PWM驱动步进电机
硬件:MAX II EPM240T100C5
软件:Quartus II 10.0
step_motor.v
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | module step_motor //#(parameter RPM50 = 40, RPM300 = 6) #( parameter RPM50 = 1000, RPM300 = 3) ( // global clock and asyn reset input CLOCK_50, input RST_N, // step motor interface output wire ENA, output wire PUL, output wire DIR, //test output wire PIN_64 ); //assign PIN_64 = pwm_out; localparam SUBDIVISION = 64; function integer log2( input integer n); integer i; for (i=0; 2**i <=n; i=i+1) log2=i+1; endfunction // generate 6KHz tick localparam DIV_50M_INTO_6K = 8333 / SUBDIVISION; reg [log2(DIV_50M_INTO_6K):1] cnt0; // 0~8332 always @( posedge CLOCK_50) if (DIV_50M_INTO_6K-1 == cnt0) cnt0 <= 0; else cnt0 <= cnt0 + 1'b1 ; wire tick_6KHz = (DIV_50M_INTO_6K-1 == cnt0) ? 1 : 0; // generate pulse signal at low frequency localparam DIV_1SEC = 50_000_000; // 10Hz reg [log2(DIV_1SEC):1] cnt1; always @( posedge CLOCK_50, negedge RST_N) if (!RST_N) cnt1 <= 0; else if (cnt1 == DIV_1SEC - 1) cnt1 <= 0; else cnt1 <= cnt1 + 1; reg [log2(RPM50):1] period ; reg acc_dec; always @( posedge CLOCK_50, negedge RST_N) if (!RST_N) begin period <= RPM50; //acc_dec <= 0; end else if (cnt1 == DIV_1SEC - 1) begin //if(period == RPM300) acc_dec <= ~acc_dec; //if(acc_dec) begin if ( period == RPM300) period <= RPM50; else period <= period - 1; //end //period <= RPM50; //else period <= period - 1; end wire adjust_enable = (cnt1 == DIV_1SEC - 1) ? 1 : 0; /* PWM */ wire pwm_out; pwm_generator #(.N(log2(RPM50)), .START_SPEED(RPM50)) pwm_generator_inst ( // global clock and asyn reset .CLOCK_50(CLOCK_50), .RST_N(RST_N), // clock enable tick .clock_enable(tick_6KHz), // PWM interface .adjust_enable(adjust_enable), . period ( period ), .pwm_out(pwm_out) ); assign PUL = pwm_out; assign ENA = 1; assign DIR = 0; endmodule |
pwm_generator.v
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | module pwm_generator #( parameter N=10, START_SPEED = 100) ( // global clock and asyn reset input CLOCK_50, input RST_N, // clock enable tick input clock_enable, // PWM interface input adjust_enable, input [N:1] period , output reg pwm_out ); reg [N:1] period_r; reg [N:1] pulse_width_r; // pass the parameter always @( posedge CLOCK_50, negedge RST_N) if (!RST_N) begin period_r <= START_SPEED; pulse_width_r <= START_SPEED >> 1; end else if (adjust_enable) begin period_r <= period ; pulse_width_r <= period >> 1; end // counter reg [N:1] cnt; always @( posedge CLOCK_50, negedge RST_N) if (!RST_N) cnt <= 0; else if (clock_enable) begin if (cnt == period_r) cnt <= 0; else cnt <= cnt + 1'b1 ; end // generate pulse always @( posedge CLOCK_50, negedge RST_N) if (!RST_N) pwm_out <= 0; else if (clock_enable) begin if (cnt < pulse_width_r) pwm_out <= 1; else pwm_out <= 0; end endmodule |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步