| ELSIF rising_edge(CLK) THEN 
 adcclkcount<=adcclkcount+1;
 if adcclkcount=1 then
 adcclkcount<=0;
 if currentstate=nextstate then
 case currentstate is
 when poweron=>
 powercount<=powercount+1;
 DACOUTQ<="1000000000";
 DACOUTI<="1000000000";
 powerflag<='1';
 dactest<=x"00";
 when poweroff=>
 powercount<=powercount+1;
 powerflag<='0';
 dactest<=x"01";
 when halfrise=>
 --test0<=not test0;
 DACOUTQ<=dacarray2(count);
 DACOUTI<=1023-dacarray2(count);
 latch<='0';
 dactest<=x"02";
 when maxvalue=>
 moduate<='1';
 savestate<=maxvalue;
 dactest<=x"03";
 iostate<='0';
 DACOUTQ<="1111111111";
 DACOUTI<="0000000000";
 if latch='0' then
 --test0<=not test0;
 latch<='1';
 case cmdstate is
 when cmdAck=>
 if codenum>3 then
 codeindex<=codeindex+1;
 processdatashift(511 downto 1)<=processdatashift(510 downto 0);
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 
 when cmdQueryRep=>
 --test0<=not test0;
 if codenum>3 then
 codeindex<=codeindex+1;
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 
 when others=>
 --when cmdQuery=>
 
 if codenum>preamble then
 codeindex<=codeindex+1;
 if ((codeindex mod 16)=13)or ((codeindex mod 16)=14) then
 updataflag<='1';
 --test1<='1';
 end if;
 if (codeindex mod 16)=15 then
 updataflag<='0';
 --shifttemp<="0010110000000000";                --Q=0 DR=64/3
 
 --shifttemp<="0110010000000000";                --Q=2 DR=64/3
 shifttemp<=Query(5 downto 0 )& "0000000000";                --Q=2 DR=64/3
 
 --shifttemp<="0100110000000000";                        --DR=8
 --shifttemp<=codearray2;
 --shiftloop<=shiftloop+1;
 else
 shifttemp(15 downto 1)<=shifttemp(14 downto 0);
 end if;
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 end case;
 end if;
 
 when minvalue=>
 moduate<='0';
 savestate<=minvalue;
 dactest<=x"04";
 iostate<='0';
 DACOUTQ<="0000000000";
 DACOUTI<="1111111111";
 if latch='0' then
 --test0<=not test0;
 latch<='1';
 case cmdstate is
 when cmdAck=>
 if codenum>3 then
 codeindex<=codeindex+1;
 processdatashift(511 downto 1)<=processdatashift(510 downto 0);
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 
 when cmdQueryRep=>
 --test0<=not test0;
 if codenum>3 then
 codeindex<=codeindex+1;
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 
 when others=>
 --when cmdQuery=>
 
 if codenum>preamble then
 codeindex<=codeindex+1;
 if ((codeindex mod 16)=13)or ((codeindex mod 16)=14) then
 updataflag<='1';
 end if;
 if (codeindex mod 16)=15 then
 updataflag<='0';
 shifttemp<="0010110000000000";
 --test1<='0';
 --shifttemp<="1111000010101010";
 --?????????????shifttemp<=codearray2;
 --shiftloop<=shiftloop+1;
 else
 shifttemp(15 downto 1)<=shifttemp(14 downto 0);
 end if;
 else
 codeindex<=0;
 codenum<=codenum+1;
 end if;
 end case;
 end if;
 
 when newcmd=>
 
 --if (cmdstate=cmdQUERY)then
 --        if suspendflag='1' then
 --                powerflag<='1';
 --                test0<='1';
 --        else
 --                powerflag<='0';
 --                test0<='0';
 --        end if;
 --else
 --        powerflag<='0';
 --        test0<='0';
 --end if;
 test0<=not test0;
 --powerflag<='0';
 
 if not(cmdstate=cmdQueryRep)then
 if suspendflag='1' then
 powerflag<='1';
 end if;
 else
 Queryrepcount<=Queryrepcount+1;
 if Queryrepcount=QrepRange then
 powerflag<='0';
 end if;
 end if;
 
 if savestate=maxvalue then
 DACOUTI<="0000000000";
 DACOUTQ<="1111111111";
 else
 DACOUTQ<="0000000000";
 DACOUTI<="1111111111";
 end if;
 newcmdexecd<='1';
 --test0<='1';
 adcclkcount<=0;
 count<=0;
 --processdatashift(511 downto 494)<=processdata(510 downto 495)&"10";
 --要补上不同命令状态的分支 差异
 processdatashift(511 downto 494)<="01"&shiftrn16;
 codeindex<=0;
 codenum<=0;
 latch<='0';
 if (cmdstate=cmdQUERY)or(cmdstate=cmdQueryRep)then
 if flag0='1' then
 cmdstate<=cmdAck;
 dactest<=x"05";
 else
 dactest<=x"25";
 cmdstate<=cmdQueryRep;
 end if;
 else
 dactest<=x"15";
 cmdstate<=cmdQueryRep;
 end if;
 
 when fallhalf=>
 dactest<=x"06";
 if iostate='0' then
 --test0<=not test0;
 iostate<='1';
 end if;
 DACOUTQ<=dacarray2(249-count);
 DACOUTI<=1023-dacarray2(249-count);
 prestate<='1';        ----fallhalf;
 
 when halfvalue=>
 dactest<=x"07";
 powercount<=0;
 iostate<='0';
 newcmdexecd<='0';
 --test0<='0';
 DACOUTQ<="1000000000";
 DACOUTI<="1000000000";
 if latch='0' then
 codenum<=codenum+1;
 latch<='1';
 end if;
 
 when halffall=>
 dactest<=x"08";
 if iostate='0' then
 --test0<=not test0;
 iostate<='1';
 end if;
 DACOUTQ<=1023-dacarray2(count)(9 downto 0);
 DACOUTI<=dacarray2(count)(9 downto 0);
 prestate<='0';                        --halffall;
 latch<='0';
 
 when rise=>
 dactest<=x"09";
 if iostate='0' then
 --test0<=not test0;
 iostate<='1';
 end if;
 DACOUTQ<=dacarray1(count);
 DACOUTI<=1023-dacarray1(count);
 --if count<127 then
 --        DACOUTI<=dacarray1(count+128);
 --else
 --        DACOUTI<=1023-dacarray1(count-128);
 --end if;
 
 
 --DACOUTQ<=conv_std_logic_vector(count,8)&"00";
 --DACOUTI<=1023-(conv_std_logic_vector(count,8)&"00");
 latch<='0';
 
 when fall=>
 dactest<=x"0a";
 if iostate='0' then
 --test0<=not test0;
 iostate<='1';
 end if;
 DACOUTQ<=dacarray1(489-count);
 DACOUTI<=1023-dacarray1(489-count);
 
 --if count<127 then
 --        DACOUTI<=1023-dacarray1(count+128);
 --else
 --        DACOUTI<=dacarray1(count-128);
 --end if;
 
 --DACOUTQ<=1023-(conv_std_logic_vector(count,8)&"00");
 --DACOUTI<=conv_std_logic_vector(count,8)&"00";
 
 
 
 latch<='0';
 
 when risehalf=>
 dactest<=x"0b";
 if iostate='0' then
 --test0<=not test0;
 iostate<='1';
 end if;
 DACOUTQ<=1023-dacarray2(249-count);
 DACOUTI<=dacarray2(249-count);
 
 when others=>
 dactest<=x"ff";
 DACOUTQ<="1000000000";
 DACOUTI<="1000000000";
 end case;
 count<=count+1;
 else
 DACOUTQ<=dactemp;
 DACOUTI<=1023-dactemp;
 count<=0;
 end if;
 currentstate<=nextstate;
 end if;
 END IF;
 END PROCESS;
 
 PROCESS (currentstate,prestate,count,RESET,codenum, preamble, codeindex, bitlength, shifttemp,cmdstate, wrcount, processdatashift, savestate, newcmdexecd)
 variable codetemp:integer range 0 to 65535;
 BEGIN
 if reset='1' then
 --cmdstate<=cmdQuery;
 --test1<='0';
 trigerflag<='0';
 
 endflag<='0';
 saveflag<='0';
 else
 case currentstate is
 when ready=>
 if endflag='0'then
 --nextstate<=halfrise;
 nextstate<=poweron;
 dactemp<="1000000000";
 finishout<='0';
 test1<='1';
 addrsel<='0';
 power<='1';
 else
 nextstate<=ready;
 end if;
 
 when poweron=>
 if powercount=2000 then
 nextstate<=halfrise;
 else
 nextstate<=poweron;
 end if;
 
 when poweroff=>
 if powercount>2000 then
 endflag<='1';
 power<='0';
 nextstate<=ready;
 test1<='0';
 else
 nextstate<=poweroff;
 end if;
 
 when halfrise=>
 if count=249 then
 dactemp<="1111111111";
 nextstate<=maxvalue;
 else
 nextstate<=halfrise;
 dactemp<="1000000000";
 end if;
 
 --2
 when maxvalue=>
 trigerflag<='1';
 dactemp<="1111111111";
 case cmdstate is
 when cmdAck=>
 receivetype<="00001";
 if not(codenum>3) then
 codetemp:=codearray1(codenum);
 else
 if not(codeindex=18) then
 if processdatashift(511)='1' then
 codetemp:=504;
 else
 codetemp:=10;
 end if;
 else
 codetemp:=40000;
 addrsel<='1';
 nextstate<=newcmd;
 
 end if;
 end if;
 if count=codetemp then
 --dactemp<="1111111111";
 if codenum<2 then
 nextstate<=fallhalf;
 else
 if count=40000 then
 nextstate<=newcmd;
 
 else
 nextstate<=fall;
 end if;
 end if;
 else
 nextstate<=maxvalue;
 if (count=1140) and (codenum>1) then
 finishout<='1';
 end if;
 if (overtime='1') and (count>2000)then
 nextstate<=newcmd;
 
 end if;
 end if;
 
 when cmdQueryRep=>
 
 if not(codenum>3) then
 codetemp:=codearray1(codenum);
 else
 if not(codeindex=4) then
 codetemp:=10;
 else
 codetemp:=3600;
 
 nextstate<=newcmd;
 
 end if;
 end if;
 if count=codetemp then
 --dactemp<="1111111111";
 if codenum<2 then
 nextstate<=fallhalf;
 else
 if count=3600 then
 nextstate<=newcmd;
 
 else
 nextstate<=fall;
 end if;
 end if;
 else
 nextstate<=maxvalue;
 --if (count=1140) and (codenum>1) then
 if count=1140 then
 finishout<='1';
 end if;
 end if;
 
 when others=>
 
 receivetype<="00000";
 if not(codenum>preamble) then
 codetemp:=codearray(codenum);
 else
 if not(codeindex=bitlength) then
 if shifttemp(15)='1' then
 codetemp:=504;
 else
 codetemp:=10;
 end if;
 else
 codetemp:=3600;
 addrsel<='1';
 nextstate<=newcmd;
 
 end if;
 end if;
 if count=codetemp then
 
 --dactemp<="1111111111";
 if codenum<2 then
 nextstate<=fallhalf;
 else
 
 if count=3600 then
 nextstate<=newcmd;
 else
 nextstate<=fall;
 end if;
 end if;
 else
 nextstate<=maxvalue;
 if (count=1140) and (codenum>1) then
 finishout<='1';
 end if;
 end if;
 end case;
 
 when newcmd=>
 
 saveflag<='1';
 finishout<='0';
 --下面这部分本来是延时关闭调制,现在程序并非发了QUERY就关闭,所以要以后再补充完善,现在继续发其他命令
 --if count>39999 then
 --        if savestate=maxvalue then
 --                nextstate<=fallhalf;
 --        else
 --                nextstate<=risehalf;
 --        end if;
 --else
 --test1<=not test1;
 --1008if (newcmdexecd='1') and (not(cmdstate=cmdAck)) then
 
 if savestate=maxvalue then
 nextstate<=fallhalf;
 dactemp<="1111111111";
 else
 nextstate<=risehalf;
 dactemp<="0000000000";
 --test0<='0';
 end if;
 
 if (newcmdexecd='1')then
 --test0<=not test0;
 --if not (cmdstate=cmdACK) then
 --        trigerflag<='0';
 --        test0<='0';
 --else
 trigerflag<='1';
 --test0<='1';
 --end if;
 --test0<='0';
 --if not(cmdstate=cmdAck)then
 
 --原来执行完ACK关闭RF调制,现在改为执行QUERYREP命令
 
 --nextstate<=fallhalf;
 
 else
 nextstate<=newcmd;
 end if;
 --end if;
 
 --3
 when fallhalf=>
 if count=249 then
 dactemp<="1000000000";
 if codeindex=bitlength then
 nextstate<=halfvalue;
 else
 nextstate<=halfvalue;
 end if;
 else
 dactemp<="1000000000";
 nextstate<=fallhalf;
 end if;
 
 --4
 when halfvalue=>
 dactemp<="1000000000";
 --if trigerflag='0' then
 if powerflag='1' then
 --if codeindex=bitlength then
 --        nextstate<=halfvalue;
 --else
 if count=91 then                                                                                                        --217
 dactemp<="1000000000";
 --if prestate=fallhalf then
 if prestate='1'then
 nextstate<=halffall;
 else
 nextstate<=halfrise;
 end if;
 else
 nextstate<=halfvalue;
 end if;
 --end if;
 else
 nextstate<=poweroff;
 end if;
 --5
 when halffall=>
 if count=249 then
 dactemp<="0000000000";
 nextstate<=minvalue;
 else
 dactemp<="1000000000";
 nextstate<=halffall;
 end if;
 
 --6
 when minvalue=>
 trigerflag<='1';
 dactemp<="0000000000";
 case cmdstate is
 when cmdAck=>
 receivetype<="00001";
 if not(codenum>3) then
 codetemp:=codearray1(codenum);
 else
 if not(codeindex=18) then
 if processdatashift(511)='1' then
 codetemp:=504;
 else
 codetemp:=10;
 end if;
 else
 codetemp:=40000;
 addrsel<='1';
 nextstate<=newcmd;
 
 end if;
 end if;
 if count=codetemp then
 --dactemp<="0000000000";
 if codenum<2 then
 nextstate<=risehalf;
 else
 if count=40000 then
 nextstate<=newcmd;
 
 else
 nextstate<=rise;
 end if;
 end if;
 else
 nextstate<=minvalue;
 if (count=1140) and (codenum>1) then
 finishout<='1';
 end if;
 if (overtime='1') and (count>2000)then
 nextstate<=newcmd;
 
 end if;
 end if;
 
 when cmdQueryRep=>
 
 if not(codenum>3) then
 codetemp:=codearray1(codenum);
 else
 if not(codeindex=4) then
 codetemp:=10;
 else
 codetemp:=3600;
 nextstate<=newcmd;
 
 end if;
 end if;
 
 if count=codetemp then
 --dactemp<="0000000000";
 if codenum<2 then
 nextstate<=risehalf;
 else
 if count=3600 then
 nextstate<=newcmd;
 
 else
 nextstate<=rise;
 end if;
 end if;
 else
 nextstate<=minvalue;
 if count=1140 then
 finishout<='1';
 end if;
 end if;
 
 when others=>
 --when cmdQuery=>
 
 receivetype<="00000";
 if not(codenum>preamble) then
 codetemp:=codearray(codenum);
 else
 if not(codeindex=bitlength) then
 if shifttemp(15)='1' then
 codetemp:=504;
 else
 codetemp:=10;
 end if;
 else
 codetemp:=3600;
 addrsel<='1';
 nextstate<=newcmd;
 
 end if;
 end if;
 if count=codetemp then
 --dactemp<="0000000000";
 if codenum<2 then
 nextstate<=fallhalf;
 else
 if count=3600 then
 nextstate<=newcmd;
 
 
 else
 nextstate<=rise;
 end if;
 end if;
 else
 if (count=1140) and (codenum>1) then
 finishout<='1';
 end if;
 nextstate<=minvalue;
 end if;
 end case;
 
 --7
 when rise=>
 if count=489 then
 dactemp<="1111111111";
 nextstate<=maxvalue;
 else
 dactemp<="1000000000";
 nextstate<=rise;
 end if;
 
 --8
 when fall=>
 if count=489 then
 dactemp<="0000000000";
 nextstate<=minvalue;
 else
 dactemp<="1000000000";
 nextstate<=fall;
 end if;
 
 --9
 when risehalf=>
 dactemp<="1000000000";
 if count=249 then
 if codeindex=bitlength then
 nextstate<=halfvalue;
 else
 nextstate<=halfvalue;
 end if;
 else
 nextstate<=risehalf;
 end if;
 
 when others=>
 ----test0<='0';
 dactemp<="1000000000";
 nextstate<=ready;
 end case;
 end if;
 END PROCESS;
 
 process (CLK,RESET,testclkcount)
 begin
 if reset='1' then
 iqstate<='0';
 DACOUT<="1000000000";
 else
 if falling_edge(clk) then
 --test1<=not test1;
 --DACOUT<=conv_std_logic_vector(testclkcount,8);
 --DACTEST<=DACOUTQ;
 case iqstate is
 when '0'=>
 DACOUT<=DACOUTI;
 DAC01OUT<=DACOUTI(1 downto 0);
 --DACOUT<=DACOUTQ;
 when others=>
 DACOUT<=DACOUTQ;
 DAC01OUT<=DACOUTQ(1 downto 0);
 end case;
 iqstate<=not iqstate;
 end if;
 end if;
 end process ;
 |