1. library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity pwm is
Generic(
CONSTANT Ffpga: integer := 40000000;--Frecuencia del Fpga
CONSTANT Fpwm: integer := 480;--Frecuencia del PWM
--CONSTANT Dcmin : ineger := 30;--Duty cycle menor
--CONSTANT Dcmax : integer := 70;--Duty cycle mayor
CONSTANT Dcmin : integer := 25000;--
CONSTANT Dcmax : integer := 66666);
Port ( clk : in STD_LOGIC;
duty_c : in STD_LOGIC_VECTOR (7 downto 0);
pwm : out STD_LOGIC);--Salida del pwm
end pwm;
architecture Behavioral of pwm is
CONSTANT escala : integer := Ffpga / Fpwm;
--CONSTANT min : real := escala * (Dcmin);--La cota menor 30%
--CONSTANT max : integer := escala *(Dcmax / 100);--La cota mayor 80%
--CONSTANT t : integer := max - min;--El tiempo de ciclo de trabajo
--CONSTANT p : integer := t/255;--Valor de cada bit
--CONSTANT pos : integer := duty_c * (p) + min;--Posicion de duty cycle
signal pwm_Cont: STD_LOGIC_VECTOR (17 downto 0) :=(others =>'0');--El conteo que
se lleva para comparar con duty cycle
type estados is (alto,bajo);
signal estado: estados := alto;
signal pos : integer := 0;
begin
MEF:process(clk,estado,pwm_Cont,pos)
begin
pos <= 25000;
if (clk'EVENT and clk = '1')then
pwm_Cont <= pwm_Cont + 1;
case estado is
when alto => if pwm_Cont >= pos then -- si ya es mayor la cuenta
estado <= bajo;--Salta a estado bajo
end if;
when bajo => if pwm_Cont <= pos then --el duty cycle requerido
estado <= alto;--Salta a estado alto
end if;
end case;
end if;
if pwm_Cont = escala then -- es el periodo de 480hz
pwm_Cont <= "000000000000000000";--Reinicia cuenta
end if;
end process;
---------------------------------------------------------------
process(estado)
begin
case estado is
when alto => pwm <= '1';--Si esta en el estado alto pwm es 1
when bajo => pwm <= '0';--Si esta en el estado bajo pwm es 0