I have studied and coded a bit on the coolrunner card, trying to make a nice PWM led dimmer configuration for the CPLD.
Main components:
1. 30 bit counter running from the 8 MHz input clock pin. Bit 23 of this counter toggles at close to 1 Hz, and is a nice clock signal for testing and debugging the other processes.
2. Set process, counts up and down a 16 bit counter that determines the pulse width based on two pushbuttons, called width.
3. Fast PWM process running from clock counter bit 10, i.e. 8 KHz clock that drives the LED outputs
4. Slow PWM process that causes a nice 'beat' pulse on the LED's.
Next step is to replace the buttons on the Coolrunner cards with a little C program so that the LED's can be dimmed remotely.
Code: Select all
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
entity top is
Port (
clock8MHz : in STD_LOGIC;
led : out STD_LOGIC_VECTOR (3 downto 0);
sw0 : in STD_LOGIC;
sw1 : in STD_LOGIC;
bt0 : in STD_LOGIC;
bt1 : in STD_LOGIC;
j1_1 : in STD_LOGIC
);
end top;
architecture Behavioral of top is
signal reset : STD_LOGIC;
signal clock : STD_LOGIC;
signal count : STD_LOGIC_VECTOR ( 29 downto 0);
signal width : STD_LOGIC_VECTOR (3 downto 0);
--signal pulse : STD_LOGIC_VECTOR (7 downto 0);
signal clk_div : STD_LOGIC;
signal beat_clk : STD_LOGIC;
signal pulse_clk : STD_LOGIC;
signal pwm_clk : STD_LOGIC;
signal output: STD_LOGIC;
signal beat : STD_LOGIC;
signal raspi_comm : STD_LOGIC;
signal longer : STD_LOGIC;
signal shorter : STD_LOGIC;
begin
reset <= sw0;
clock <= clock8MHz;
raspi_comm <= j1_1;
longer <= not bt0;
shorter <= not bt1;
clockdivider_0 : process(reset, clock) begin
if reset = '1' then
count <= "000000000000000000000000000000";
elsif rising_edge (clock) then
count <= count + 1;
end if;
end process clockdivider_0;
clk_div <= count(23);
pulse_clk <= count(20);
beat_clk <= count(18);
pwm_clk <= count (10);
blink_0 : process(clock) begin
if rising_edge (clock) then
if sw1 = '1' then
end if;
end if;
end process blink_0;
led(0) <= not (output); -- Counter bit 23, 8 Mhz/2^23 = 0.95 Hz
led(2) <= not (output);
led(1) <= not (output);
led(3) <= not (output);
set_0 : process(reset, pulse_clk)
begin
if reset = '1' then
width <= "0000";
elsif rising_edge(pulse_clk) then
if longer = '1' then width <= width + 1; end if;
if shorter = '1' then width <= width - 1; end if;
end if;
end process set_0;
pwm_0 : process(reset, pwm_clk)
variable pulse : integer range 0 to 15;
begin
if reset = '1' then
pulse := 0;
output <= '0';
elsif rising_edge (pwm_clk) then
if pulse <= width then
if width = 0 then
output <= '0';
pulse := pulse + 1;
else
output <= '1';
pulse := pulse + 1;
end if;
elsif pulse = 15 then
pulse := 0;
else
output <= '0';
pulse := pulse + 1;
end if;
end if;
end process pwm_0;
pwm_1 : process(reset, beat_clk)
variable pulse : integer range 0 to 15;
begin
if reset = '1' then
pulse := 0;
beat <= '0';
elsif rising_edge (beat_clk) then
if pulse <= 2 then
beat <= '1';
pulse := pulse + 1;
elsif pulse = 15 then
pulse := 0;
else
beat <= '0';
pulse := pulse + 1;
end if;
end if;
end process pwm_1;
end Behavioral;