DE2 GPIO expansion P11-X 直流馬達轉速與轉向控制
參考http://www.ee.thit.edu.tw/~microlab/VHDL/other.htm#直流馬達轉速與轉向控制實習
module DC_MOTOR (GPIO_0 ,GPIO_1 , SW, LEDR, LEDG , CLOCK_27 ,KEY );
input [17:0] SW; // toggle switches
input [7:0] KEY; // Push bottom
input CLOCK_27; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [7:0] LEDG; // green LEDs
output [35:0]GPIO_0; //GPIO0 PORT
inout [35:0]GPIO_1; //GPIO1 PORT
reg [35:0] GPIO_1=36'bz ;
reg [7:0] LEDG; // green LEDs
reg [31:0] cnt;
wire KEY0,KEY1;
reg rKEY0,nCNT;
assign KEY0= GPIO_1[0];
assign KEY1= GPIO_1[1];
always @(posedge CLOCK_27 or negedge KEY0)
begin
if (!KEY0)
cnt<=0;
else
cnt <= cnt + 1;
nCNT <= cnt[26];
end
always @(nCNT)
begin
if (!nCNT)
begin
GPIO_1[9:8] <= 2'b10;
LEDG[3:0]<=1'b0;
GPIO_1[2] <=1'b0;
end
else
begin
GPIO_1[9:8] <= 2'b01;
LEDG[3:0]<=1'b1;
GPIO_1[2] <=1'b1;
end
end
assign LEDR[15:0] = cnt[20:5];
endmodule
/*
----------------------------------------------------------------------------------------------------------
--實驗名稱:直流馬達轉速與轉向控制實習
--檔案名稱:dcmotor.vhd
--功 能:以指撥開關來調整直流馬達轉速,以彈跳開關控制直流馬達轉向
----------------------------------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity dcmotor is
port(
clk : in std_logic; --系統頻率
DIP : in std_logic_vector(7 downto 0); --指撥開關輸入
BTN : in std_logic_vector(3 downto 0); --彈跳開關輸入
VREF : out std_logic; --PWM訊號
DIR : out std_logic_vector(1 downto 0) --TA7291模式選擇訊號
);
end dcmotor;
architecture a of dcmotor is
signal B:std_logic_vector(7 downto 0);
begin
----- 下數計數器 -----
process(clk)
begin
if clk'event and clk='1' then
B <= B-1;
end if;
end process;
----- 比較器 -----
VREF <= '1' when DIP > B else '0';
----- TA7291模式選擇 -----
process(BTN)
begin
case BTN is
when "1110" => DIR <= "00";
when "1101" => DIR <= "01";
when "1011" => DIR <= "10";
when "0111" => DIR <= "11";
when others => DIR <= "00";
end case;
end process;
end a;
*/
/*
---------------------------------------------------------------------
--實驗名稱:PWM實習
--檔案名稱:pwm.vhd
--功 能:以指撥開關來調整PWM訊號輸出
---------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity pwm is
port(
clk : in std_logic; --系統頻率
A : in std_logic_vector(7 downto 0); --PWM控制訊號
pwm : out std_logic --PWM訊號輸出
);
end pwm;
architecture a of pwm is
signal B:std_logic_vector(7 downto 0);
begin
----- 下數計數器 -----
process(clk)
begin
if clk'event and clk='1' then
B <= B-1;
end if;
end process;
--比較器
pwm<='1' when A > B else '0';
end a;
*/
沒有留言:
張貼留言