--- Quote Start ---
Hi,
I would like to know is there any possibility to use always inside for loop??
please anybody explain the errors associated with my code both logical and coding syntax issues
ThankYOU
--- Quote End ---
I think you have been struggling for some time with your pwm work. I am afraid Your current coding is just wrong all over.
Moreover your algorithm plan is not clear. I am here volunteering the below piece of code which is almost a precision pwm generator.
and it can't be simpler(well it depends). The only drawback is that I have done it in matlab code and leave it to you to translate to hdl.
it is just an accumultor and comparator. Don't worry at this stage about how it works.
you can also avoid division /100 on the duty cycle by some simple bit shift trick (rescale duty cycle to divide by 128)
clear all;
n = 1000; %this test length
clk = 100; %MHz system clock
f1 = 1; %MHz required output frequency
%%%%%%%%%%%% accumulator %%%%%%%%%%%%%%%
m = 1024; %(assumed) lut size
tw = floor(m*f1/clk); %tuning word for half cycle
ptr = 0; %pointer to lut
for i = 1:n
ptr = round(mod(ptr + tw, m));
lut_data(i) = (ptr-1)*64; %lut computed per pointer
end
%%%%%%%%%%%%% comparator %%%%%%%%%%%%%
clkout = zeros(1,n);
duty = 1; %1 ~ 100 percent
threshold = 32768*duty*2/100;
for i = 1:n
if lut_data(i) <= threshold, clkout(i) = 32767; end;
end
%%%%%%%%%%%% check results %%%%%%%%%%%
figure;plot(lut_data,'g-'); hold
plot(clkout,'r.-');
grid;
d = diff(clkout);
b = find(d);
c = diff(b);
duty_out = round(100*c(2)/(c(1)+c(2)))