-- PWMchannel.vhd -- release 2.0 -- PWMChannel realizes a 16 bit PWM .generator -- Release 2 fixes a race condition on the PWM output on the StopValue=0 that caused a small -- high logic pulse on the PWM out. It fixes also that wrong 0 output on StopValue = 0xFFFF. -- Please refer to the proper documentation for the explanations of the interface designed. -- For more info see: http://www.acmesystems.it/?id=120 -- Author: Roberto Asquini -- Copyright (C) 2006 Acme Systems srl (http://www.acmesystems.it) -- -- This is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This example is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- To have a copy of the GNU General Public License write to the Free Software -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity PWMchannel is port( RunningCounter : in std_logic_vector(15 downto 0); -- main entry for the running counter. StartPWM : in std_logic; -- this line goes high when the running counter is zero. -- when the running counter reaches 0x0000 the PWM line should be raised. StopValue : in std_logic_vector(15 downto 0); -- 16 bit value. When the running counter -- reaches this value the PWM will lower its line. Clock : in std_logic; -- osc clock CLEARN : in std_logic; -- 0=reset all. EnablePWM : in std_logic; -- 0=disablePWM; 1=enablePWM; PWMout : out std_logic -- output of the PWM ); end PWMchannel; architecture Behavioral of PWMchannel is signal RunningEqualStop: std_logic; begin Comparator: process ( CLEARN, Clock ) begin if ( CLEARN = '0' ) then -- if CLEARN goes to zero it clears the EQUAL line RunningEqualStop <= '0'; elsif (Clock'event and Clock = '0') then if RunningCounter = StopValue then RunningEqualStop <= '1'; else RunningEqualStop <= '0'; end if; end if; end process; -- PWMprocess generate the PWM output and now it is completely synchronous with the Clock, -- this avoid problems on StopValues of 0 and 0xffff. PWMprocess: process ( CLEARN, StartPWM, EnablePWM, RunningEqualStop, Clock ) begin if (CLEARN = '0' ) then PWMout <= '0'; elsif (Clock'event and Clock = '1' ) then if (EnablePWM = '1' and StartPWM = '1' and RunningEqualStop = '0') then PWMout <= '1'; elsif (RunningEqualStop = '1' or EnablePWM = '0' ) then PWMout <= '0'; end if; end if; end process; end Behavioral;