Forum Discussion
Altera_Forum
Honored Contributor
12 years agoQuick update:
I managed to make a pwm generator and I can now control speed of a brushless motor by flipping switches from the de2 board which sens pwm signals to the ESC. Now I need to make a pwm decoder since my 2.4ghz receiver outputs pwm signals. The period is 20ms but I will need to program code which detects each duty cycle. Also, I found a gyro that I already own that outputs pwm signals instead of i2c. This will make my hard easier since after I get my pwm decoder to work, I can start writing the algorithm that controls each ESC (which then powers the motors). Here is my code for the decoder which I haven't been able to test yet. The pwmsignal has a period of 20ms (50hz) and it will be 1'b1 1-2ms depending if the input is at 100% or at 0%. When input is at 100%, the signal will be 1'b1 for 2ms, when at 50, for 1.5ms and when at 0%, for 1ms. I guess my greatest difficulty is trying to "sync" each 20ms cycle. Ratio should be a value between 0 and 100 and thats what I'm trying to calculate from the pwmsignal. Essentially, when pwmsignal is 1 for 2ms, I want ratio to be at 100 and when pwmsignal is 1 for 1ms, I want it to be at 0. I'd love to hear if anyone has a better way to find the ratio and if my current code would work. --- Quote Start --- module gyro(clk, control, pwm,pwmsignal); input clk; input pwmsignal; input [8:0] control; output pwm; reg pwm; reg [31:0] counter = 32'd0; reg [31:0] signalon = 32'd0; //# of times a sec pwm signal is at 1 reg [31:0] signaloff = 32'd0; //# of time a sec pwm signal is at 0 reg [31:0] ratio = 32'd0; always @ (posedge clk) begin counter <= counter+1; if (pwmsignal == 1'b1) begin signalon <= signalon+1; end else begin signaloff <= signaloff+1; end if ((counter >= 32'd950000)&&(signalon >=500)) //clock speed is 50,000,000hz, signalon >= 500 in case there is error begin //we want to refresh signal 50 time a sec counter <= 32'b0; ratio <= ((signalon-10'd50000)/5000); //max signalon is 100,000 min is 50,000. This way answer is between 0 and 100 end else begin end end endmodule --- Quote End ---