Apéndice A: Programa en VHDL del proyecto -- Company: -- Engineer: UDLAP Jose Juan Vazquez Montaño --- Create Date: 20:53:42 02/24/2008 -- Design Name: PWM1 a 50Hz -- Module Name: Gen_Pwm - Behavioral -- Project Name: JJ_TESIS -- Target Devices: SPARTAN 3 -- Tool versions: Xilinx ISE 9.1 -- Description: -- prograama que genera PWM a50Hz a partir de la señal de los sensores en la patas para escalar un nivel. --- Dependencies: --- Revision: -- Revision 0.01 - File Created -- Additional Comments: ----------------------------------------------------------------------------------88 library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; ---- Uncomment the following library declaration if instantiating ---- any Xilinx primitives in this code. --library UNISIM; --use UNISIM.VComponents.all; entity Gen_Pwm1 is Port ( s1,s2,s3,s4,s5,s6,s7,s8,s9,s10 : out STD_LOGIC;-- salida de PWM1 para servo1 sdii : in STD_LOGIC;--entrada de sdii sensor delantero izquierdo sdd : in STD_LOGIC;--entrada de sdd sensor delantero derecho sdii2 : in STD_LOGIC;--entrada de sdii sensor delantero izquierdo sdd2 : in STD_LOGIC;--entrada de sdd sensor delantero derecho reset : in STD_LOGIC;-- reset C_D1 : out STD_LOGIC; C_D2 : out STD_LOGIC; C_I1 : out STD_LOGIC; C_I2 : out STD_LOGIC; SW_AD : in STD_LOGIC; SW_AT : in STD_LOGIC; SW_DE : in STD_LOGIC; 89 SW_IZ : in STD_LOGIC; clock : in STD_LOGIC);--entrada de reloj a 50MHz end Gen_Pwm1; architecture Behavioral of Gen_Pwm1 is signal cont: integer:=0; --1000000 signal cont2: integer:= 0; --50 signal P_b: integer:=30000; --50 signal P_b2: integer:=30000; --50 signal P_b3: integer:=120000; --50 signal P_b4: integer:=120000; --50 signal P_b5: integer:=120000; --50 signal P_b6: integer:=30000; --50 signal P_b7: integer:=30000; --50 signal P_b8: integer:=120000; --50 signal P_b9: integer:=120000; --50 signal P_b10: integer:=30000; --50 --signal cont3: integer:= 0; --50 begin 90 process (clock,reset,SW_AD,SW_AT,SW_DE,SW_IZ,sdd,sdii,sdd2,sdii2) begin if(reset='1' ) then -- resetea todos los valores C_D1<= '0'; C_D2<= '0'; C_I1 <= '0'; C_I2 <= '0'; elsif(sdd='0' or sdii='0' or sdd2='0' or sdii2='0' ) then -- resetea todos los valores C_D1<= '0'; C_D2<= '0'; C_I1 <= '0'; C_I2 <= '0'; elsif(clock'event and clock='1' ) then --cada que susede el evento transicion a 1 del reloj if(SW_AD='1')then C_D1<= '1'; C_D2<= '0'; C_I1 <= '1'; 91 C_I2 <= '0'; elsif(SW_AT='1')then C_D1<= '0'; C_D2<= '1'; C_I1 <= '0'; C_I2 <= '1'; elsif(SW_DE='1')then C_D1<= '1'; C_D2<= '0'; C_I1 <= '0'; C_I2 <= '1'; elsif(SW_IZ='1')then C_D1<= '0'; C_D2<= '1'; C_I1 <= '1'; C_I2 <= '0'; else C_D1<= '0'; C_D2<= '0'; C_I1 <= '0'; C_I2 <= '0'; end if;--de(SW1='1') end if;--(clock'event and clock='1' ) 92 end process; process (clock,reset,sdd) variable flag: integer:= 1; --50 variable flag2: integer:= 0; --50 variable Pas1: integer:= 0; --50 variable Pas2: integer:= 0; --50 variable Pas3: integer:= 0; --50 variable Pas4: integer:= 0; --50 variable Pas5: integer:= 0; --50 variable Pas6: integer:= 0; --50 variable Pas7: integer:= 0; --50 variable Pas8: integer:= 0; --50 variable Pas9: integer:= 0; --50 variable Pas10: integer:= 0; --50 variable Pas11: integer:= 0; --50 variable Pas12: integer:= 0; --50 variable Pas13: integer:= 0; --50 variable Pas14: integer:= 0; --50 variable Pas15: integer:= 0; --50 variable b2: integer:= 0; --50 93 begin if(reset='1' ) then -- resetea todos los valores s1 <= '0'; s2 <= '0'; s3 <= '0'; s4 <= '0'; cont <= 0; cont2 <= 0; flag := 1; flag2 := 0; P_b <= 30000; P_b2 <= 30000; P_b3 <= 120000; P_b4 <= 120000; P_b5 <= 120000; P_b6 <= 30000; P_b7 <= 120000; P_b8 <= 120000; P_b9 <= 30000; P_b10 <= 30000; Pas1:= 0; --50 Pas2:= 0; --50 Pas3:= 0; --50 94 Pas4:= 0; --50 Pas5:= 0; --50 Pas6:= 0; --50 Pas7:= 0; --50 Pas8:= 0; --50 Pas9:= 0; --50 Pas10:= 0; --50 Pas11:= 0; --50 Pas12:= 0; --50 Pas13:= 0; --50 Pas14:= 0; --50 Pas15:= 0; --50 b2:= 0; --50 elsif(clock'event and clock='1' ) then --cada que susede el evento transicion a 1 del reloj cont <= cont+1; --contadores empiezan cuentas cont2 <= cont2+1; if(cont >= 1000000)then cont<=0; end if;--(cont >= 1000000) 95 if(cont2 >= 10000000)then cont2<=0; b2:=1; end if;--(cont >= 1000000) if (cont >= P_b) THEN -- si contador es mayor o igual a P_b se va a '0' s1 <= '0'; else s1 <= '1'; end if; if(cont >= P_b2) THEN s2 <= '0'; --si no se mantiene en '1' -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else s2 <= '1'; end if; if(cont >= P_b3) THEN s3 <= '0'; -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else s3 <= '1'; end if; if(cont >= P_b4) THEN s4 <= '0'; -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else 96 s4 <= '1'; end if; if(cont >= P_b5) THEN s5 <= '0'; -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else s5 <= '1'; end if; if (cont >= P_b6) THEN -- si contador es mayor o igual a P_b se va a '0' s6 <= '0'; else s6 <= '1'; end if; if(cont >= P_b7) THEN s7 <= '0'; --si no se mantiene en '1' -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else s7 <= '1'; end if; if(cont >= P_b8) THEN s8 <= '0'; -- si contador es mayor o igual a P_b se va a '0' --si no se mantiene en '1' else s8 <= '1'; end if; if(cont >= P_b9) THEN -- si contador es mayor o igual a P_b se va a '0' 97 s9 <= '0'; --si no se mantiene en '1' else s9 <= '1'; end if; if(cont >= P_b10) THEN -- si contador es mayor o igual a P_b se va a '0' s10 <= '0'; --si no se mantiene en '1' else s10 <= '1'; end if; if(sdii='0' and sdd='0' and flag=1 and SW_AD and SW_ AT and SW_DE and SW_IZ) then-pas1:=1; end if;--(sdii='0' and sdd='0') if (pas1=1 and b2=1)then P_b5<=75000; b2:=0; flag:=0; P_b<=P_b+250; if(P_b >=48000)then Pas1:=0; 98 Pas2:=1; end if;--(P_b >=85000)--90° end if;--(pas1=1) if (pas2=1 and b2=1)then b2:=0; P_b2<=P_b2+250; if(P_b2 >=44000)then--15° Pas2:=0; Pas3:=1; end if;--(P_b2 >=43333) end if;--(pas2=1) if (pas3=1 and b2=1)then b2:=0; P_b<=P_b-250; if(P_b<=30000)then--15° Pas3:=0; Pas4:=1; end if;--(P_b <=43333) end if;--(pas3=1) 99 if (pas4=1 and b2=1)then b2:=0; P_b3<=P_b3-250; if(P_b3 <=105000)then--90° Pas4:=0; Pas5:=1; end if;--(P_b3 >=85000) end if;--(pas4=1) if (pas5=1 and b2=1)then b2:=0; P_b4<=P_b4-250; if(P_b4 <=105500)then--165° Pas5:=0; Pas6:=1; end if;--(P_b4 >=126667) end if;--(pas5=1) if (pas6=1 and b2=1)then b2:=0; P_b3<=P_b3+250; if(P_b3 >=120000)then--165° 100 Pas6:=0; Pas7:=1; end if;--(P_b3 >=126667) end if;--(pas6=1) if (pas7=1 and b2=1)then b2:=0; P_b5<=120000; flag2:=1; pas7:=0; pas8:=1; end if;--(pas6=1) if(sdii2='0' and sdd2='0' and pas8=1 ) pas9:=1; pas8:=0; end if;--(sdii='0' and sdd='0') if (pas9=1 and b2=1)then P_b10<=75000; b2:=0; flag2:=0; P_b6<=P_b6+500; 101 if(P_b6 >=75000)then Pas9:=0; Pas10:=1; end if;--(P_b >=85000)--90° end if;--(pas1=1) if (pas10=1 and b2=1)then b2:=0; P_b7<=P_b7-500; if(P_b7 <=95000)then--15° Pas10:=0; Pas11:=1; end if;--(P_b2 >=43333) end if;--(pas2=1) if (pas11=1 and b2=1)then b2:=0; P_b6<=P_b6-500; if(P_b6<=30000)then--15° Pas11:=0; Pas12:=1; end if;--(P_b <=43333) 102 end if;--(pas3=1) if (pas12=1 and b2=1)then b2:=0; P_b8<=P_b8-500; if(P_b8 <=75000)then--90° Pas12:=0; Pas13:=1; end if;--(P_b3 >=85000) end if;--(pas4=1) if (pas13=1 and b2=1)then b2:=0; P_b9<=P_b9+500; if(P_b9 >=60000)then--165° Pas13:=0; Pas14:=1; end if;--(P_b4 >=126667) end if;--(pas5=1) if (pas14=1 and b2=1)then 103 b2:=0; P_b8<=P_b8+500; if(P_b8 >=120000)then--165° Pas14:=0; Pas15:=1; end if;--(P_b3 >=126667) end if;--(pas6=1) if (pas15=1 and b2=1)then b2:=0; P_b10<=30000; flag2:=0; pas15:=0; end if;--(pas6=1) end if; end process; end Behavioral; 104