unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;//System.ComponentModel;

type

    TForm1 = class(TForm)
    ButtonDownLoad: TButton;
    ButtonOpen: TButton;
    ButtonStartFrq: TButton;
    EditFrq: TEdit;
    Label1: TLabel;
    btnRom: TButton;
    Label2: TLabel;
    Label3: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ButtonDownLoadClick(Sender: TObject);
    procedure ButtonOpenClick(Sender: TObject);
    procedure ButtonStartFrqClick(Sender: TObject);

    procedure btnRomClick(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


//Control memory setting
function SetCON(RegVal:Byte;index:Integer):Boolean; stdcall;external 'DDSCON.DLL';
//8 bits digital I/O control
Function DigitalInOut(OutVal : Byte; InVal:PByte ;Index:Integer): Boolean; stdcall;external 'DDSCON.DLL';
//Set frequency of output sample point
Function SetAD9850(FrqVal : LongWord;Index:Integer):Boolean; stdcall;external 'DDSCON.DLL';
//Set relay state
Function SetRelay(RelayVal : Byte;index:Integer):Boolean ; stdcall;external 'DDSCON.DLL';
//Set address counter's last address
Function SetEndAddr(EndAddrVal : LongWord;index:Integer) :Boolean; stdcall;external 'DDSCON.DLL';
//Set output filter cut-off frequency
Function SetFir(FirVal : Byte;index:Integer): Boolean; stdcall;external 'DDSCON.DLL';
//Clear the address counter and start
Function FrameNegtivePulse(index:Integer):Boolean; stdcall;external 'DDSCON.DLL';
//Read the value of the counter
Function GetFrqMeasureResult(index:Integer):LongWord ; stdcall;external 'DDSCON.DLL';
//Transfers Wave form data to the signal fountain
Function SendDataPackage64(Buf:PByteArray; n:Integer;index:Integer):Boolean; stdcall;external 'DDSCON.DLL';
//Select frequency counter input
Function SetFinSource(FinVal:Byte;index:Integer):Boolean ; stdcall;external 'DDSCON.DLL';
//Reset counter and startup frequency one time test
Function StartFrqMeasure(index:Integer):Boolean; stdcall;external 'DDSCON.DLL';
//Initlize USB Device
Function GetItemNum(addr:PInteger;leng:PInteger):Integer;stdcall;external 'DDSCON.DLL';
//Write Address
Function Write24C01(addr:Byte;dat:Integer;index:Integer):Boolean;stdcall;external 'DDSCON.DLL';
//Read Address
Function Read24C01(addr:Byte;dat:PInteger;index:Integer):Boolean;stdcall;external 'DDSCON.DLL';



procedure TForm1.FormCreate(Sender: TObject);

var
    res:integer;
    addr:Array[0..126] of integer;
    leng:integer;
    i:integer;
begin
        //addr[0]:=0;
        leng:=0;
        for   i:=0 to 126 Do
        Begin
                addr[i]:=0;
        End;
        res:=GetItemNum(@(addr[0]),@leng);
  
   if res=0 then
   begin
   label3.caption:=IntToStr(leng);
   SetCON ($00,0);                //Constant  waveform,Timing Trigger,Fall Trigger,FRQ_TEST on,COUT_NOT LATCH
   SetRelay ($80,0);             //frequency DC coupling
   SetRelay ($04,0);                //output attenuate 20dB
   SetRelay ($10,0);               //normal scope
   SetRelay ($01,0);                //Filter cut-off frequency less than 5M
   SetFinSource ($01,0);            //Select low frequency input
   SetFir ($04,0);                  //Set output filter cut-off frequency  10KHz
   end;
end;
///////////////////////////////////////////////////////////////////////////
procedure TForm1.ButtonDownLoadClick(Sender: TObject);
  Var
     Temp:Variant;
     Buf:Array[0..1999] of Byte;
     i:Word;
     Freq:Integer;
     WavePointNumber:Integer;
     te:Integer;
     Offset:Double;
     Amp:Double;
     fun:Double;
begin
  Freq:=1000;       //Frequency=1KHz
  WavePointNumber:=1000;    //output sample Point Number=100
  Offset:=0;
  Amp:=2.0;
  if Freq * 1000 > 50000000 then
  begin
        WavePointNumber := Trunc(50000000 / Freq);
        te := 2147483647;
  end
  else
  begin
        te:=Trunc(Freq*WavePointNumber/0.023283);
  end;
    //Frequency of output sample point
  SetAD9850(te,0);                      //Set frequency of output sample point
  For i := 0 To WavePointNumber-1 Do                       //calculate sine wave
  Begin
        fun:=Offset*1.0/10000+Amp/10*Sin(i*1.0/WavePointNumber*2*3.14159265);
     Temp := Trunc(8192 - 8192 * fun);
     if Temp > 16383 then
     begin
     Temp := 16383;
     end
     else if Temp < 0 then
     begin
     Temp := 0;
     end;
     Buf[2*i] := Trunc(Temp Mod 256);
     Buf[2*i+1] := Trunc(Temp/256);
  End;
  SetEndAddr (WavePointNumber-1,0);                         //Set address counter's last address
  SendDataPackage64(@(Buf[0]), WavePointNumber*2,0); //Transfers Wave form data to the signal fountain
  FrameNegtivePulse(0);                     // Clear the address counter and start
end;

procedure TForm1.ButtonOpenClick(Sender: TObject);
begin
   SetRelay ($08,0);                        //open DDS
end;

procedure TForm1.ButtonStartFrqClick(Sender: TObject);
begin
  StartFrqMeasure(0);                      //Reset counter and startup frequency one time test
  Sleep(1100);                            //Sleep more than 1 second
  EditFrq.Text := IntToStr(GetFrqMeasureResult(0));          //Read the value of the counter
end;

procedure TForm1.btnRomClick(Sender: TObject);
      var
       temp:integer;
       dat:integer;
      begin
       dat:=256;
       Write24C01(0,dat,0);
       Sleep(100);
       Read24C01(0,@temp,0);
       if temp=dat  then
       begin
              Application.MessageBox('Read/Write OK!','OK',MB_OK);
       end
       else
       begin
              Application.MessageBox('Read/Write Error!','ERROR',MB_OK);
       end;

end;

end.

