我正在努力使FPGA成为数字锁的伺服驱动器。
My code is as follows:
`timescale 1ns / 1ps
/*
1 pin for servo--ORANGE CABLE
red cable-- 5V, brown cable-- GND.
Position "0" (1.5 ms pulse) is middle,
"90" (~2ms pulse) is all the way to the right,
"-90" (~1 ms pulse) is all the way to the left.
servo stuff:
http://www.micropik.com/PDF/SG90Servo.pdf
*/
//All i need to do is set SERVOPWM to 1 and 0 with delays i think
module ServoTestNShit(input M_CLOCK,
output [7:0] IO_LED, // IO Board LEDs
output reg SERVOPWM);
assign IO_LED = 7'b1010101; // stagger led lights just cause
reg [15:0] counter;
reg [15:0] counter1;
initial begin
counter1 = 0;
counter = 0;
end
//use counter to have a 1ms or 2ms or 1.5ms duty cycle for a while inorder to actually run
//because run it this way is asking the servo to move for 1.5ms so it cant atually move that fast
always @ (posedge M_CLOCK)
begin
counter <= counter+1;
counter1 <= counter1+1;
end
always @ (negedge M_CLOCK)
begin
//if (counter1 > 500)
//begin
SERVOPWM <= (counter <= 1);
//end
end
endmodule
目前,无论发送2毫秒还是1毫秒,我都可以将其一直向右转。我遇到的最大问题是尝试使其只能向右转然后停止。我尝试过的所有结果最终要么都不起作用,要么就是不停地工作,就像我一开始从未将0发送到图钉一样。
在经过足够的时间将其一直旋转到一个方向后,有人可以建议最好的方法将其发送为0吗?
谢谢!
您需要通过脉宽调制(PWM)调整伺服电压。换句话说,如果您想要10%的电压,则需要SERVOPWM
将持续时间设置为持续时间的10%。
我会做的事情是这样的:
module ServoTestNShit(input M_CLOCK,
input [7:0] voltage_percentage,
output [7:0] IO_LED, // IO Board LEDs
output reg SERVOPWM);
reg [7:0] counter;
initial begin
counter = 0;
end
// let the counter count for 100 time units
always @ (posedge M_CLOCK)
begin
counter <= counter+1;
if (counter <= 100)
counter <= 0;
end
// set the output 1 for voltage_percentage/100 of the time
always @ (negedge M_CLOCK)
begin
SERVOPWM <= (counter <= voltage_percentage);
end
endmodule
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句