oke. Em đã hiểu. Cám ơn anh rất nhiều
lập trình SPWM cho động cơ AC bằng VHDL
Collapse
X
-
-
Anh Tony ơi cho em hỏi anh 1 việc này nữa ạ.
Ở trong cái chương trình điều xung PWM của anh viết hướng dẫn đó
bây giờ em muốn chuyển sang dung lên tới 16 bit, trong đó 2 bit đầu là mức 1, 2 bit sau nữa 11, 2 bit sau nữa là 111... đến bít 15,16 là 1111111111111111 ,sau đó giảm ngược lại, Thì em phải làm thế nào hả anh. Em làm mãi mà không đựoc.library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity SPWM is
generic ( Cmax : integer :=8);
port (
clk : in std_logic;
reset : in std_logic;
enable : in std_logic;
PWM : out std_logic);
end SPWM;
architecture Behavioral of SPWM is
signal cclk : integer range 0 to Cmax;
signal threshold : integer range 0 to Cmax;
signal up : std_logic;
begin
process (clk, reset)
begin
if (reset = '1') then
PWM <= '0';
cclk <= 0;
threshold <= 0;
up <= '1';
elsif (clk'event and clk ='1') then
if (enable = '1') then
if (cclk <= threshold) then
PWM <= '1';
else
PWM <= '0';
end if;
if (cclk = Cmax-1) then
cclk <= 0;
else
cclk <= cclk + 1;
end if;
if (cclk = Cmax-1) then
if (threshold = Cmax-3) then
up <= '0';
end if;
if (threshold = 1) then
up <= '1';
end if;
if (up = '1') then
threshold <= threshold + 1;
else
threshold <= threshold - 1;
end if;
end if; --
end if; --enable
end if;
end process;
end Behavioral;
Mong anh giúp đỡ em.
Cám ơn anh!
Comment
-
-
anh Tony ơi cho em hỏi em đã sửa lại rồi mà sao 2 xung kế tiếp nhau lại không bằng nhau
đây là code của em
Mong anh giúp đỡ, em sắp nộp rồi mà vẫn chưa điều chế được anh akentity SPWM1 is
generic ( Cmax : integer :=8);
port (
clk : in std_logic;
reset : in std_logic;
enable : in std_logic;
PWM : out std_logic
);
end SPWM1;
architecture Behavioral of SPWM1 is
signal cclk : integer range 0 to Cmax;
signal threshold1 : integer range 0 to Cmax ;
signal up : std_logic;
signal tmp : std_logic;
begin
process (clk, reset,enable)
variable threshold : integer range 0 to Cmax;
begin
if ((reset = '1')or(enable = '0')) then
PWM <= '0';
cclk <= 0;
threshold1 <= 0;
up <= '1';
elsif (clk'event and clk ='1') then
if (cclk <= threshold1) then
tmp <='1';
PWM <= tmp;
else
tmp <='0';
PWM <= tmp;
end if;
if (cclk = Cmax) then
cclk <= 0;
else
cclk <= cclk + 1;
end if;
if (cclk = Cmax-1) then
if (threshold1 = Cmax-2) then
up <= '0';
end if;
if (threshold1 = 0) then
up <= '1';
end if;
if (up = '1') then
if (cclk =0) then
threshold := threshold ;
elsif (cclk = 1) then
threshold := threshold +1;
elsif (cclk = 2) then
threshold := threshold ;
elsif (cclk = 3) then
threshold := threshold +1;
elsif (cclk = 4) then
threshold := threshold ;
elsif (cclk = 5) then
threshold := threshold +1;
elsif (cclk = 6) then
threshold := threshold ;
elsif (cclk = 7) then
threshold := threshold +1;
end if;
else
if (cclk =0) then
threshold := threshold ;
elsif (cclk =1) then
threshold := threshold -1;
elsif (cclk =2) then
threshold := threshold ;
elsif (cclk =3) then
threshold := threshold -1;
elsif (cclk =4) then
threshold := threshold ;
elsif (cclk =5) then
threshold := threshold -1;
elsif (cclk =6) then
threshold := threshold ;
elsif (cclk =7) then
threshold := threshold -1;
end if;
end if;
threshold1 <= threshold ;
end if;
end if;
end process;
end Behavioral;
Cám ơn anh!Last edited by FPGA_9572XL; 12-06-2011, 20:23.
Comment
-
Tôi đổi lại code đã gửi trước đây, bạn thử lại xem.
Code:library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity SPWM is generic ( Cmax : integer :=16); port ( clk : in std_logic; reset : in std_logic; enable : in std_logic; PWM : out std_logic); end SPWM; architecture Behavioral of SPWM is signal cclk : integer range 0 to Cmax; signal threshold : integer range 0 to Cmax; signal up, threshold_change : std_logic; begin process (clk, reset) begin if (reset = '1') then PWM <= '0'; cclk <= 0; threshold <= 0; up <= '1'; threshold_change <= '0'; elsif (clk'event and clk ='1') then if (enable = '1') then if (cclk <= threshold) then PWM <= '1'; else PWM <= '0'; end if; if (cclk = Cmax-1) then cclk <= 0; else cclk <= cclk + 1; end if; if (cclk = Cmax-1) then if (threshold_change = '1') then if (threshold = Cmax-2) then up <= '0'; end if; if (threshold = 1) then up <= '1'; end if; if (up = '1') then threshold <= threshold + 1; else threshold <= threshold - 1; end if; end if; threshold_change <= not threshold_change; end if; -- end if; --enable end if; end process; end Behavioral;
Comment
-
Anh ơi, anh xem lai bài của anh gửi hộ em với, em đã mô phỏng thì dạng xung đầu ra của nó không thay đổi mà giống nhau anh ak`. Anh giúp em với, đây là dạng xung mà em mô phỏng được:

Em cám ơn anh!
Comment
-
Chắc simulator của bạn bị trở ngại. Tôi dùng Modelsim và thử nghiệm trước khi gửi rồi
Comment
Bài viết mới nhất
Collapse

Comment