Altera_Forum
Honored Contributor
12 years agoPWM signal via sine-triangle comparison
Hello,
right now i am stuck in aproblem in my project. I hope its not explained to complicated and someone can help me (-: I try to generate a pwm signal out of a sine with 50 Hz - 20ms. I have triangle function with a period time of 8 µs by counting from -99 to 99 and vice versa. I have a sine table with 500 values, so if i want to fit the whole sine into the 20ms period and i have the triangle with a period of 8µs (400* 20ns) i have to update the sine value every 40µs. Now i simulated everything and it compiles just fine but i think my design is full of logical errors which i right now cant see. In the modelsim analysis i see a close pwm signal but with the wrong amplitude, so that the sine function is much larger than the triangle. I hope someone can help me and thanks in advance for reading through my code!! Cheers Tim
---------------------------------------------------------------------------------
-------------------------Timer Direction Function--------------------------------
---------------------------------------------------------------------------------
TimerDirection:process (CLOCK,RESET,TimerDir,Timer)
begin
if RESET = '0' then
TimerDir <= '1';
elsif CLOCK'event and CLOCK ='1' then
if (Timer =99) then
TimerDir <= '0';
end if;
if (Timer =-99) then
TimerDir <= '1';
end if;
end if;
end process TimerDirection;
CountingTimer: process (CLOCK,RESET,TimerDir,Timer)
begin
if RESET = '0' then
--Timer<= (others => '0');
Timer <= 0;
countSine <= 0;
elsif CLOCK'event and CLOCK ='1' then
if (TimerDir ='1') then
Timer <=Timer + 1;
end if;
if (TimerDir ='0') then
Timer <=Timer - 1;
end if;
countSine <= countSine +1;
if (countSine = 2000) then
countSine <= 0;
end if;
end if;
end process CountingTimer; type table is array (0 to 499) of std_logic_vector(8 downto 0);
function init_table return table is
variable var: table;
variable x: real;
begin
for i in 0 to 499 loop
x:= SIN(real(i)*MATH_PI/real(500));
var(i):=std_logic_vector(to_signed(INTEGER(x*real(100)),9));
end loop;
return var;
end;
constant sinTable : mem := init_mem;
this function generates the pwm signal: modulate: process (CLOCK,RESET,TimerDir,Timer)
begin
if RESET = '0' then
PWM_OUTPUT <= '1';
elsif CLOCK'event and CLOCK ='1' then
if SinValue = std_logic_vector(to_signed(Timer,9)) and (Timerdir<='1')then
PWM_OUTPUT <= '0';
end if;
if SinValue = std_logic_vector(to_signed(Timer,9)) and (Timerdir<='0')then
PWM_OUTPUT <= '1';
end if;
end if;
end process modulate;
end pwm_process;