--**********************分频进程*************************
process(clk)
variable cnt1 : integer range 0 to 100;
variable cnt2 : integer range 0 to 20;
begin
if clk'event and clk='1' then
if cnt1=100 then
cnt1:=0;
if cnt2=20 then
cnt2:=0;
clock<=not clock;
if(cnt=3)then
cnt<=0;
else
cnt<=cnt+1;
end if;
else
cnt2:=cnt2+1;
end if;
else
cnt1:=cnt1+1;
end if;
end if;
end process;
--**************状态驱动进程**********************
sync :process(clock,reset)
begin
if(reset = '0') then
current_state <= start;
elsif(clock'event and clock='1') then
current_state <= next_state;
end if;
end process sync;
--***************adc驱动进程*******************
comb :process(current_state, intr)
begin
case current_state is
when start => --启动状态
next_state <= convert;
cs <= '0';
wr <= '0';
rd <= '1';
read_data <= '0';
when convert =>--初始化
if(intr = '0') then
next_state <= read1;
else
next_state <= convert;
end if;
cs <= '1';
wr <= '1';
rd <= '1';
read_data <= '0';
when read1 =>--读状态1
next_state <= read2;
cs <= '0';
wr <= '1';
rd <= '0';
read_data <= '1';
when read2 =>--读状态2
next_state <= start;
cs <= '1';
wr <= '1';
rd <= '1';
read_data <= '0';
when others =>--其他状态
next_state <= start;
end case;
end process comb;
--****************读取AD数据********************
get_data: process(clock,reset)
begin
if(reset = '0') then
p<=0;
elsif(clock'event and clock='1') then
if(read_data = '1') then
p<=conv_integer(data_i);
end if;
end if;
end process;
--*********************65536Hz分频进程************************
process(clk)
variable cnt1 : integer range 0 to 762;
begin
if clk'event and clk='1' then
case cnt1 IS
WHEN 381 =>
cp_65k<='1';
cnt1:=cnt1+1;
WHEN 762=>
cnt1:=0;
cp_65k<='0';
cp_wr<='0';
WHEN 20=>
cp_wr<='1';
cnt1:=cnt1+1;
WHEN OTHERS=>
cnt1:=cnt1+1;
end case;
end if;
end process;
--*********************1kHz分频进程************************
process(cp_65k)
variable cnt1 : integer range 0 to 64;
begin
if cp_65k'event and cp_65k='1' then
case cnt1 is
when 32=>cp_1k<='1';
cnt1:=cnt1+1;
when 64=>cnt1:=0;
cp_1k<='0';
when others=>cnt1:=cnt1+1;
end case;
end if;
end process;
--**************DDS地址累加器进程**********************
PROCESS(cp_65k)
BEGIN
IF(cp_65k'EVENT AND cp_65k='1') THEN
--DDS累加器循环累加dds_m
IF dds_add<65535 THEN
dds_add<=dds_add+dds_m;
ELSE
dds_add<=dds_add+dds_m-65536;
END IF;
END IF;
END PROCESS;
--***********************频率加减控制进程***************************
process(cp_1k)
VARIABLE keys:INTEGER RANGE 0 TO 127 :=0; --消抖累加器
begin
if cp_1k='1' then
case key is
when "10"=> --频率加
if keys=127 then
keys:=0;
bell<='1';
if dds_m=1000 then
dds_m<=1;
else
dds_m<=dds_m+1;
end if;
else
keys:=keys+1;
end if;
when "01"=> --频率减
if keys=127 then
keys:=0;
bell<='1';
if dds_m=1000 then
dds_m<=1;
else
dds_m<=dds_m-1;
end if;
else
keys:=keys+1;
end if;
when others=>bell<='0';
end case;
end if;
end process;
end dac;