⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.pas

📁 ad9852 code 很好的代码呀!好长时间!
💻 PAS
📖 第 1 页 / 共 2 页
字号:
//
// 2002/3 Charlos Potma
//
// Program to control an Analog Devices AD9852 type DDS
//
// Schematic: see ad9852sch.gif
//
unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Math, ExtCtrls;

type
  TForm_main = class(TForm)
    Edit_clock: TEdit;
    Label_clock: TLabel;
    Edit_frequency: TEdit;
    Label_frequency: TLabel;
    Edit_ftw: TEdit;
    Label_ftw: TLabel;
    Edit_ftwbits: TEdit;
    Label_ftwbits: TLabel;
    Label_ftw1: TLabel;
    Label_ftwbits1: TLabel;
    Button_masterreset: TButton;
    Button_send: TButton;
    Button_exit: TButton;
    Label_ftwhex: TLabel;
    Label_ftwhex1: TLabel;
    Edit_ftwhex: TEdit;
    Edit_portaddress: TEdit;
    Label_portaddress: TLabel;
    Label_status: TLabel;
    Edit_repeat: TEdit;
    Label_repeat: TLabel;
    Bits_sent: TMemo;
    Label_bitssent: TLabel;
    Label_ftwbytes: TLabel;
    Ftw_bytes: TMemo;
    Edit_level: TEdit;
    Label_level: TLabel;
    Label_header: TLabel;
    ComboBox_pllmultiplier: TComboBox;
    CheckBox_pllenabled: TCheckBox;
    Label_pllmultiplier: TLabel;
    Edit_clockeff: TEdit;
    Label_clockeff: TLabel;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Bevel3: TBevel;
    Bevel4: TBevel;
    Edit_controldac: TEdit;
    Label_controldac: TLabel;
    Label_twoscomplement: TLabel;
    CheckBox_fs: TCheckBox;
    CheckBox_max: TCheckBox;
    CheckBox_min: TCheckBox;
    CheckBox_controldac: TCheckBox;
    procedure Button_exitClick(Sender: TObject);
    procedure Button_sendClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button_masterresetClick(Sender: TObject);

    procedure sendCSB;
    procedure sendCSBNOT;
    procedure sendSCLK;
    procedure sendIOReset;
    procedure sendIOUD;
    procedure sendByte(senddata : Byte);
    procedure CheckBox_pllenabledClick(Sender: TObject);
    procedure ComboBox_pllmultiplierChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure CheckBox_fsClick(Sender: TObject);
    procedure CheckBox_maxClick(Sender: TObject);
    procedure CheckBox_minClick(Sender: TObject);
    procedure CheckBox_controldacClick(Sender: TObject);

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

const               //From AD9852/4 datasheet rev.0 :
  MASTERRESET =  1; //bit 0 of data port, active on logic high
                    //pin 2 on db25 connector
  CSB         =  2; //bit 5 of data port, active on logic low
                    //pin 3 on db25 connector
  IORESET     =  4; //bit 3 of data port, active on logic high
                    //pin 4 on db25 connector
  SCLK        =  8; //bit 2 of data port, data is read on rising edge
                    //pin 5 on db25 connector
  SDO         = 16; //bit 1 of data port, data
                    //pin 6 on db25 connector
  IOUD        = 32; //bit 4 of data port, active on rising edge
                    //pin 7 on db25 connector
var
  Form_main   : TForm_main;
  portbits    : Byte;
  portaddress : Smallint;
  bytessent   : Integer;
  controldac  : Boolean;
implementation

{$R *.DFM}

{Source code for inpout32.dll.
Enables 32-bit Visual Basic programs to do direct port I/O
(Inp and Out) under Windows 95.
To be compiled with Borland's Delphi 2.0.
from: www.lvr.com }
procedure Out32(PortAddress:smallint;Value:smallint);
var
   ByteValue:Byte;
begin
     ByteValue:=Byte(Value);
     asm
        push dx
        mov dx,PortAddress
        mov al, ByteValue
        out dx,al
        pop dx
     end;
end;

function Inp32(PortAddress:smallint):smallint;
var
   ByteValue:byte;
begin
   asm
        push dx
        mov dx, PortAddress
        in al,dx
        mov ByteValue,al
        pop dx
    end;
    Inp32:=smallint(ByteValue) and $00FF;
end;

procedure TForm_main.Button_exitClick(Sender: TObject);
begin
  Close;
end;

procedure TForm_main.Button_sendClick(Sender: TObject);
const fourtyeight = 281474976710656; //2^48
var
  ftw                               : int64;
  buf                               : array[0..5] of byte;
  levelhigh, levellow               : byte;
  controldachigh, controldaclow     : byte;
  clock, clockeff, frequency        : double;
  bitnumber, bytenumber, sendrepeat : integer;
  hexstring                         : string;
  level                             : word;
  inputfault                        : boolean;
  pllbyte                           : byte;

begin
  Bytessent := 0;
  inputfault := False;

  //Check clock/pll
  if((strtofloat(Edit_clock.Text) > 300.0))then
  begin
    showmessage('DDS base clock must be >0 and <300 MHz');
    Edit_clock.SelectAll;
    Edit_clock.SetFocus;
    inputfault := True;
  end
  else
  begin
    clock := strtofloat(Edit_clock.Text);
    if Checkbox_pllenabled.Checked = True then
    begin
      if clock * (ComboBox_pllmultiplier.ItemIndex+4) > 300.0 then
      begin
        showmessage('(DDS base Clock) * (Pll multiplier) must be =< 300 MHz');
        ComboBox_pllmultiplier.SelectAll;
        ComboBox_pllmultiplier.SetFocus;
        inputfault := True;
      end
      else
      begin
        clockeff := clock * (ComboBox_pllmultiplier.ItemIndex+4);
        pllbyte := ComboBox_pllmultiplier.ItemIndex+4;
        if clockeff > 200 then
          pllbyte := pllbyte or 64;//set pll high bit
      end;
    end
    else
    begin
      pllbyte := 32;//meaning bypass pll
      clockeff := clock;
    end;
  end;

  //Check DDS output level
  if((strtoint(Edit_level.Text) < 0) or
   (strtoint(Edit_level.Text) > 4095))then
  begin
    showmessage('DDS output level is a 12-bit value between 0 and 4095');
    Edit_level.SelectAll;
    Edit_level.SetFocus;
    inputfault := True;
  end
  else
  begin
    level := strtoint(Edit_level.Text);
    levelhigh := (level and $0f00) shr 8; //bits 9..11
    levellow  := (level and $00ff);       //bits 0..8
  end;

  //Check control dac output level
  if((strtoint(Edit_controldac.Text) < 0) or
   (strtoint(Edit_controldac.Text) > 4095))then
  begin
    showmessage('Control DAC output level is a 12-bit value between 0 and 4095');
    Edit_controldac.SelectAll;
    Edit_controldac.SetFocus;
    inputfault := True;
  end
  else
  begin
    level := strtoint(Edit_controldac.Text);
    controldachigh := (level and $0f00) shr 8; //bits 9..11
    controldaclow  := (level and $00ff);       //bits 0..8
  end;
  //Check DDS frequency

  if((strtofloat(Edit_frequency.Text) < 0.0) or
   (strtofloat(Edit_frequency.Text) > (clockeff/2)))then
  begin
    showmessage('DDS Frequency must be > 0 and < DDS-Clock/2');
    Edit_frequency.SelectAll;
    Edit_frequency.SetFocus;
    inputfault := True;
  end
  else
    frequency := strtofloat(Edit_frequency.Text);
  if(inputfault = False) then
  begin
    //Calculate frequency tuning word
    ftw := trunc((frequency / clockeff) * fourtyeight);
    Edit_ftw.Text := inttostr(ftw);
    hexstring := inttohex(ftw, 12);
    //Calculate frequency tuning word in hexadecimal
    Edit_ftwhex.Text := '';
    for bitnumber := 1 to trunc(Length(hexstring)/2) do
    begin
      if bitnumber =  trunc(Length(hexstring)/2) then
      begin
        Edit_ftwhex.Text :=
        Copy(hexstring, Length(hexstring)-(bitnumber*2-1), 2) +
        Edit_ftwhex.Text;
      end
      else
      begin
        Edit_ftwhex.Text := '.' +
        Copy(hexstring, Length(hexstring)-(bitnumber*2-1), 2) +
        Edit_ftwhex.Text;
      end;
    end;
    //Calculate frequency tuning word in binary
    Edit_ftwbits.Text := '';
    for bytenumber := 0 to 5 do
      buf[bytenumber] := 0;
     //Test ftw bit-for-bit and fill ftwbits edit field accordingly
    for bitnumber := 1 to 48 do
    begin
      //Test ftw bit-for-bit and fill ftwbits edit field accordingly
      bytenumber := bitnumber div 8;
      if ((ftw and trunc(intpower(2,bitnumber-1)))
         = trunc(intpower(2,bitnumber-1))) then
      begin
        Edit_ftwbits.Text := '1' + Edit_ftwbits.Text;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -