VHDL RGB LED PWM 控制程式 |
尚未結案
|
hungyi
一般會員 發表:1 回覆:0 積分:0 註冊:2009-12-16 發送簡訊給我 |
HELLO ALL
我想寫一佪RGB LED 控制程式如下 ,其中pwm 部分一直有錯(紅色),是否有人可指導,其中Data_Value 我會從一counter 讀出color sensor 謮值再和Set_data_R 比較,再做PWM調變 謝了 ibrary IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; use work.user_pkg.all; ---- Uncomment the following library declaration if instantiating ---- any Xilinx primitives in this code. --library UNISIM; --use UNISIM.VComponents.all; entity PWM_GEN is Port ( clock : in STD_LOGIC; Reset : in STD_LOGIC; Data_Value : in STD_LOGIC_VECTOR (7 downto 0); PWM : out STD_LOGIC); end PWM_GEN; architecture Behavioral of PWM_GEN is SIGNAL reg_out : std_logic_vector (7 downto 0); SIGNAL cnt_out_int : std_logic_vector (7 downto 0); SIGNAL pwm_int : std_logic; SIGNAL rco_int : std_logic; SIGNAL set_data_R : std_logic_vector (7 downto 0); SIGNAL A : std_logic_vector (7 downto 0); SIGNAL B : std_logic_vector (7 downto 0); begin process (clock,reg_out,reset) BEGIN IF(rising_edge (clock)) then A <= Data_value; END IF; END process; process (A,B) BEGIN B <= "10000011";--setting PWM duty R LED=%/Hz-- set_data_R <="11000000"; --set R LED pluse data-- if (set_data_R < A) then B <= B-1 ; --If set data > counter data then increase PWM duty -- ELSE if (set_data_R > A) then B <= B 1 ; --If set data < counter data then decrease PWM duty -- ELSE B <= "10000011"; END if; END if; if (B < "00000011" )then B <="00000100"; END if; if (B > "11111110") then B <="11111100"; END if; reg_out<= B; END process; process(clock,cnt_out_int,rco_int,reg_out) BEGIN IF(rco_int = '1') then cnt_out_int <= reg_out; ELSIF rising_edge(clock) then IF (rco_int ='0' and pwm_int='1' and cnt_out_int < "11111111") then cnt_out_int <= INC(cnt_out_int); ELSE IF (rco_int='0' and pwm_int='0' and cnt_out_int >"00000000") then cnt_out_int <=DEC(cnt_out_int); END if; END if; END if; END Process; Process(cnt_out_int,rco_int,clock,reset) BEGIN If (reset = '0') then rco_int <= '1'; elsif rising_edge(clock) then IF ((cnt_out_int="11111111") or (cnt_out_int ="00000000")) then rco_int <='1'; else rco_int <='0'; END if; END if; END process; Process(clock,rco_int,reset) BEGIN IF (reset = '0') then pwm_int <= '0'; elsif rising_edge(rco_int) then pwm_int <=NOT(PWM_int); ELSE pwm_int <=PWM_int; END If; END process; PWM <= PWM_int; end Behavioral; 編輯記錄
hungyi 重新編輯於 2010-02-10 01:51:29, 註解 無‧
|
KSC
一般會員 發表:4 回覆:18 積分:4 註冊:2006-09-17 發送簡訊給我 |
B這個signal multi access.
if (set_data_R < A) then B <= B-1 ; --If set data > counter data then increase PWM duty -- ELSE if (set_data_R > A) then B <= B 1 ; --If set data < counter data then decrease PWM duty -- ELSE B <= "10000011"; END if; 和 if (B < "00000011" )then B <="00000100"; END if; if (B > "11111110") then B <="11111100"; END if; 同時出value比B 你的program我看不明白,但你的想法應該錯了 首先PWM counter的clock和Load PWM value的clock應該是不同的 例如 L是指load,-指不動作,用8-bit PWM L------------|L------------ 256次count up|256次count up 如果count up>L就出0其它時候(count up<=L) 就出1,就可以出到這樣的PWM L(10)-----------------------|L------------ 1,2,3,4,5,6,7,8,9,10,11,....|256次count up 0000000000000000000011111111|PWM output 其實一個PWM只要 1)1個Latch(儲存PWM value) 2)1個counter 3)1個比較器 就做到了 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |