Subversion Repositories svnkaklik

Rev

Blame | Last modification | View Log | Download

unit LPTMot;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    Timer2: TTimer;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Timer3: TTimer;
    CheckBox1_4: TCheckBox;
    CheckBox1_5: TCheckBox;
    CheckBox1_6: TCheckBox;
    CheckBox1_7: TCheckBox;
    Button9: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure RadioButton4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Timer3Timer(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  end;

var
  Form1: TForm1;
  krok1, krok2, krok3 : Byte;
  msteps: integer;
  smer: byte;
const BA = $378;             { LPT1 }
      vpravo = true;
      vlevo = false;
  mspeed = 10;


implementation

{$R *.DFM}
procedure Motor1 (vpravo : Boolean);
begin
  if vpravo then begin
    krok1 := krok1 * 2;
    if krok1 = 16 then krok1 := 1
  end else begin
    krok1 := krok1 div 2;
    if krok1 = 0 then krok1 := 8;
  end;
  OutPort(BA, krok1 + 16 * krok2);
end;

procedure Motor2 (vpravo : Boolean);
begin
  if vpravo then begin
    krok2 := krok2 * 2;
    if krok2 = 16 then krok2 := 1
  end else begin
    krok2 := krok2 div 2;
    if krok2 = 0 then krok2 := 8;
  end;
  OutPort(BA, krok1 + 16 * krok2);
end;

procedure Motor3 (vpravo : Boolean);
begin
  if vpravo then begin
    krok3 := krok3 * 2;
    if krok3 = 16 then krok3 := 1
  end else begin
    krok3 := krok3 div 2;
    if krok3 = 0 then krok3 := 8;
  end;
  OutPort(BA+2, krok3 XOR 11);
end;

function spinac1: Boolean;
begin
  if (InPort(BA+1) and 8) = 0 then spinac1 := true   else spinac1 := false;
end;

function spinac2: Boolean;
begin
  if (InPort(BA+1) and 16) = 0 then spinac2 := true   else spinac2 := false;
end;

function spinac3: Boolean;
begin
  if (InPort(BA+1) and 32) = 0 then spinac3 := true   else spinac3 := false;
end;

procedure nulpoloha;
begin
  repeat
    Motor1(vlevo);
    delay (5);
  until spinac1;
  repeat
    Motor2(vlevo);
    delay (5);
  until spinac2;
  repeat
    Motor3(vlevo);
    delay (5);
  until spinac3;
end;


procedure pohyb1;
var N : Word;
begin
  for n:= 1 to 100 do begin   { 100 krokù }
    Motor1 (vpravo);
    Motor2 (vpravo);          { všechny motory }
    Motor3 (vpravo);
    delay (5);                { rychle }
  end;
  for n:= 1 to 100 do begin   { 100 krokù }
    Motor1 (vpravo);          { jen motor 1 }
    delay (10);               { pomalu }
  end;
  delay(1000);                { klid 1s }
  for n:= 1 to 100 do begin   { 100 krokù }
    Motor1 (vlevo);
    Motor2 (vlevo);           { všechny motory }
    Motor3 (vlevo);
    delay(5);                 { rychle }
  end;
  for n:= 1 to 100 do begin   { 100 krokù }
    Motor1 (vlevo);           { jen motor 1 }
    delay (10);               { pomalu }
  end;
  delay (1000);               { klid 1s }
end;

procedure TForm1.FormCreate(Sender: TObject);
var ble: integer;
begin
  OpenCom (Pchar('LPT1:'));
  krok1 := 1;
  krok2 := 1;
  krok3 := 1;
  OutPort (BA,0);
  val(Edit1.Text,msteps,ble);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  nulpoloha;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  pohyb1;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
 ble:integer;
begin
   val(Edit1.Text,msteps,ble);
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  OutPort(BA, 1);
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  OutPort(BA, 2);

end;

procedure TForm1.RadioButton3Click(Sender: TObject);
begin
  OutPort(BA, 4);

end;

procedure TForm1.RadioButton4Click(Sender: TObject);
begin
  OutPort(BA, 8);

end;

procedure TForm1.Button3Click(Sender: TObject);
var n:integer;
begin
 for n:=1 to msteps do
 begin
  OutPort(BA, 5);
  delay(mspeed);
  OutPort(BA, 6);
  delay(mspeed);
  OutPort(BA, 10);
  delay(mspeed);
  OutPort(BA, 9);
  delay(mspeed);
 end;
 OutPort(BA, 0);
end;

procedure TForm1.Button4Click(Sender: TObject);
var n:integer;
begin
 for n:=1 to msteps do
 begin
  OutPort(BA, 9); //1
  delay (mspeed);               { pomalu }
  OutPort(BA, 10); //8
  delay (mspeed);              { pomalu }
  OutPort(BA, 6);  //2
  delay (mspeed);               { pomalu }
  OutPort(BA, 5);  //4
  delay (mspeed);               { pomalu }
 end;
 OutPort(BA, 0);
end;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if (smer and 1)=1 then
    Button4Click(Sender)
  else
    Button3Click(Sender);
  inc(smer);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Timer1.Enabled:=true;
  Timer2.Enabled:=true;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  Timer1.Enabled:=false;
  Timer2.Enabled:=false;
end;

procedure TForm1.Button8Click(Sender: TObject);
var n:integer;
begin
 for n:=1 to msteps do
 begin
  OutPort(BA, 1); //1
  delay (mspeed);               { pomalu }
  OutPort(BA, 8); //8
  delay (mspeed);              { pomalu }
  OutPort(BA, 2);  //2
  delay (mspeed);               { pomalu }
  OutPort(BA, 4);  //4
  delay (mspeed);               { pomalu }
 end;
 OutPort(BA, 0);
end;

procedure TForm1.Button7Click(Sender: TObject);
var n:integer;
begin
 for n:=1 to msteps do
 begin
  OutPort(BA, 4); //1
  delay (mspeed);               { pomalu }
  OutPort(BA, 2); //8
  delay (mspeed);              { pomalu }
  OutPort(BA, 8);  //2
  delay (mspeed);               { pomalu }
  OutPort(BA, 1);  //4
  delay (mspeed);               { pomalu }
 end;
 OutPort(BA, 0);
end;

procedure TForm1.Timer3Timer(Sender: TObject);
var
  n : integer;
begin
  n := InPort(BA+1);

  IF $10 = n and $10 then
   CheckBox1_4.Checked := false else CheckBox1_4.Checked := true;
  IF $20 = n and $20 then
   CheckBox1_5.Checked := false else CheckBox1_5.Checked := true;
  IF $40 = n and $40 then
   CheckBox1_6.Checked := false else CheckBox1_6.Checked := true;
  IF $80 = n and $80 then
   CheckBox1_7.Checked := true else CheckBox1_7.Checked := false;
end;

procedure run(turns:integer; speed:integer);
var n:integer;
begin
  for n:=1 to turns do
  begin
    OutPort(BA, 1);
    delay (speed);               { pomalu }
    OutPort(BA, 2);
    delay (speed);               { pomalu }
    OutPort(BA, 4);
    delay (speed);               { pomalu }
  end;
  OutPort(BA, 0);
end;

procedure TForm1.Button9Click(Sender: TObject);
var n: integer;
begin
  for n:=40 downto 10 do run(2,n);
  run(1000,10);
end;

end.