?? uport.~pas
字號(hào):
unit UPort;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Spin, ComCtrls, ExtCtrls;
type
TForm1 = class(TForm)
GroupBox_OutPut: TGroupBox;
Edit_PortOutVoltage: TEdit;
Btn_PortOut: TButton;
GroupBox_Input: TGroupBox;
RadioBtn_OutputValue: TRadioButton;
RadioBtn_OutputVoltage: TRadioButton;
Edit_PortInCh0: TEdit;
Btn_PortIn: TButton;
Edit_PortOutValue: TEdit;
Label1: TLabel;
Label2: TLabel;
Label_Channel0: TLabel;
Label_Channel1: TLabel;
Label_Channel2: TLabel;
Label_Channel3: TLabel;
Label_Channel4: TLabel;
Label_Channel5: TLabel;
Label_Channel6: TLabel;
Label_Channel7: TLabel;
Edit_PortInCh1: TEdit;
Edit_PortInCh2: TEdit;
Edit_PortInCh3: TEdit;
Edit_PortInCh4: TEdit;
Edit_PortInCh5: TEdit;
Edit_PortInCh6: TEdit;
Edit_PortInCh7: TEdit;
Edit_VoltageInCh0: TEdit;
Edit_VoltageInCh1: TEdit;
Edit_VoltageInCh2: TEdit;
Edit_VoltageInCh3: TEdit;
Edit_VoltageInCh4: TEdit;
Edit_VoltageInCh5: TEdit;
Edit_VoltageInCh6: TEdit;
Edit_VoltageInCh7: TEdit;
Label3: TLabel;
Label4: TLabel;
UpDown_Voltage: TUpDown;
Label5: TLabel;
Edit_PortInCh9: TEdit;
Label6: TLabel;
Edit_PortInCh8: TEdit;
GroupBox_Status: TGroupBox;
RadioBtn_AutoIO: TRadioButton;
RadioBtn_ManIO: TRadioButton;
Timer_IO: TTimer;
Edit_Interval: TEdit;
UpDown_Interval: TUpDown;
Label7: TLabel;
Label8: TLabel;
procedure Btn_PortInClick(Sender: TObject);
procedure Btn_PortOutClick(Sender: TObject);
procedure UpDown_VoltageClick(Sender: TObject; Button: TUDBtnType);
procedure RadioBtn_AutoIOClick(Sender: TObject);
procedure RadioBtn_ManIOClick(Sender: TObject);
procedure UpDown_IntervalClick(Sender: TObject; Button: TUDBtnType);
procedure Timer_IOTimer(Sender: TObject);
private
{ Private declarations }
InVoltage:Real; //輸入電壓
OutVoltage:Real; //輸出電壓
OutValue:SmallInt; //輸出數(shù)值
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//單字節(jié)端口輸入
Function PortIn(PortNum:Word):Byte;
var
a:Byte;
begin
asm
mov DX,PortNum;
in AL,DX;
mov a,AL;
end;{end of asm}
PortIn:=a;
end;
//單字節(jié)端口輸出
Procedure PortOut(PortNum:Word;a:Byte);
begin
asm
mov DX,PortNum;
mov AL,a;
out DX,AL;
end;{end of asm}
end;
procedure TForm1.Btn_PortInClick(Sender: TObject);
var
PortNum:Word;
InValue:integer;
InValueH,InValueL:Byte;
i:integer;
begin
for i:=0 to 7 do
begin
PortNum:=$342;
PortOut(PortNum,i); //設(shè)定通道
PortNum:=$340;
InValueL:=PortIn(PortNum); //輸入電壓低位
PortNum:=$341;
InValueH:=PortIn(PortNum); //輸入電壓高位
InValueL:=InValueL div 16;
InValue:=InValueH*16+InValueL; //計(jì)算輸入數(shù)值
//計(jì)算輸入電壓值
InVoltage:=10*(InValue-2048)/2048;
//顯示電壓值
case i of
0:
begin
Edit_PortInCh0.text:=inttostr(InValue);
Edit_VoltageInCh0.text:=FormatFloat('0.0',InVoltage);
end;
1:
begin
Edit_PortInCh1.text:=inttostr(InValue);
Edit_VoltageInCh1.text:=FormatFloat('0.0',InVoltage);
end;
2:
begin
Edit_PortInCh2.text:=inttostr(InValue);
Edit_VoltageInCh2.text:=FormatFloat('0.0',InVoltage);
end;
3:
begin
Edit_PortInCh3.text:=inttostr(InValue);
Edit_VoltageInCh3.text:=FormatFloat('0.0',InVoltage);
end;
4:
begin
Edit_PortInCh4.text:=inttostr(InValue);
Edit_VoltageInCh4.text:=FormatFloat('0.0',InVoltage);
end;
5:
begin
Edit_PortInCh5.text:=inttostr(InValue);
Edit_VoltageInCh5.text:=FormatFloat('0.0',InVoltage);
end;
6:
begin
Edit_PortInCh6.text:=inttostr(InValue);
Edit_VoltageInCh6.text:=FormatFloat('0.0',InVoltage);
end;
7:
begin
Edit_PortInCh7.text:=inttostr(InValue);
Edit_VoltageInCh7.text:=FormatFloat('0.0',InVoltage);
end;
end;//end of case
end;//end of for i:=0 to 7 do
PortOut($342,8); //設(shè)定通道8
InValueL:=PortIn($340); //輸入位置低位
InValueH:=PortIn($341); //輸入位置高位
InValue:=InValueH*256+InValueL; //計(jì)算輸入數(shù)值(編碼器旋轉(zhuǎn)位置)
Edit_PortInCh8.text:=inttostr(InValue);
PortOut($342,9); //設(shè)定通道9
InValueL:=PortIn($340); //輸入位置低位
InValueH:=PortIn($341); //輸入位置高位
InValue:=InValueH*256+InValueL; //計(jì)算輸入數(shù)值(編碼器旋轉(zhuǎn)速度)
Edit_PortInCh9.text:=inttostr(InValue);
end;
procedure TForm1.Btn_PortOutClick(Sender: TObject);
var
PortNum:Word;
OutValueH,OutValueL:Byte;
begin
//設(shè)定數(shù)值輸出
if RadioBtn_OutputValue.checked then
begin
OutValue:=strtoint(Edit_PortOutValue.text);
end;
//設(shè)定電壓值輸出
if RadioBtn_OutputVoltage.checked then
begin
OutVoltage:=UpDown_Voltage.position /10;
OutValue:=round(OutVoltage*4095/10);
Edit_PortOutValue.text:=inttostr(OutValue);
end;
//電壓輸出
if OutValue>4095 then OutValue:=4095;
OutValueH:=OutValue div 256;
OutValueL:=OutValue mod 256;
PortNum:=$240;
PortOut(PortNum,OutValueH);
PortNum:=$241;
PortOUt(PortNum,OutValueL);
end;
procedure TForm1.UpDown_VoltageClick(Sender: TObject; Button: TUDBtnType);
begin
Edit_PortOutVoltage.text:=FormatFloat('0.0',UpDown_Voltage.Position / 10);
OutVoltage:=UpDown_Voltage.position /10;
OutValue:=round(OutVoltage*4095/10);
Edit_PortOutValue.text:=inttostr(OutValue);
//直接輸出
if RadioBtn_AutoIO.checked then Btn_PortOutClick(Sender);
end;
procedure TForm1.RadioBtn_AutoIOClick(Sender: TObject);
begin
if not Timer_IO.Enabled then Timer_IO.Enabled :=true;
end;
procedure TForm1.RadioBtn_ManIOClick(Sender: TObject);
begin
if Timer_IO.Enabled then Timer_IO.Enabled :=false;
end;
procedure TForm1.UpDown_IntervalClick(Sender: TObject; Button: TUDBtnType);
begin
Timer_IO.interval:=UpDown_Interval.position*10;
Edit_Interval.text:=inttostr(Timer_IO.interval);
end;
procedure TForm1.Timer_IOTimer(Sender: TObject);
begin
Btn_PortInClick(Sender);
end;
end.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -