📄 formmain.pas
字号:
unit FormMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Driver;
type
TfrmMain = class(TForm)
grpDevSelect: TGroupBox;
labDeviceName: TLabel;
btnSelectDevice: TButton;
grpPWMConfiguration: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
chkPWMEnable0: TCheckBox;
chkPWMEnable1: TCheckBox;
txtPeriod0: TEdit;
txtPeriod1: TEdit;
txtHiPeriod0: TEdit;
txtHiPeriod1: TEdit;
chkPWMEnable2: TCheckBox;
txtPeriod2: TEdit;
txtHiPeriod2: TEdit;
btnRun: TButton;
btnStop: TButton;
btnExit: TButton;
procedure btnSelectDeviceClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure chkPWMEnable0Click(Sender: TObject);
procedure chkPWMEnable1Click(Sender: TObject);
procedure chkPWMEnable2Click(Sender: TObject);
procedure btnExitClick(Sender: TObject);
procedure btnRunClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
private
{ Private declarations }
chkPWMEnable : array[0..2] of TCheckBox;
txtPeriod : array[0..2] of TEdit;
txtHiPeriod : array[0..2] of TEdit;
public
{ Public declarations }
lDeviceNum : Longint;
lDeviceHandle : Longint;
szDescription : array[0..81] of Char;
ErrorNum : Longint;
ptDevFeatures : DevFeatures; { structure for device features }
ptDevGetFeatures : PT_DeviceGetFeatures;
ptCounterPWMSetting : PT_CounterPWMSetting;
end;
var
frmMain: TfrmMain;
implementation
{$R *.DFM}
{*************************************************************
* Function: Handle the error code. If the input error code > 0,
* it means some error apperent. This function can
* show the error message to a message box and stop
* this application.
* Input: The error code.
* return: none
************************************************************* }
Function DoesErr(var lErrCode: LongInt): integer;
var
szErrMsg : string[100];
pszErrMsg : PChar;
begin
{Check the pressed error code}
If (lErrCode <> 0) Then
Begin
pszErrMsg := @szErrMsg;
DRV_GetErrorMessage(lErrCode, pszErrMsg);
Application.MessageBox(pszErrMsg, 'Error!!', MB_OK);
DoesErr := 1;
End
Else
DoesErr := 0;
end;
procedure TfrmMain.btnSelectDeviceClick(Sender: TObject);
begin
DRV_SelectDevice(Handle, True, lDeviceNum, szDescription);
labDeviceName.Caption := szDescription;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
chkPWMEnable[0] := chkPWMEnable0;
chkPWMEnable[1] := chkPWMEnable1;
chkPWMEnable[2] := chkPWMEnable2;
txtPeriod[0] := txtPeriod0;
txtPeriod[1] := txtPeriod1;
txtPeriod[2] := txtPeriod2;
txtHiPeriod[0] := txtHiPeriod0;
txtHiPeriod[1] := txtHiPeriod1;
txtHiPeriod[2] := txtHiPeriod2;
DRV_SelectDevice(Handle, True, lDeviceNum, szDescription);
labDeviceName.Caption := szDescription;
end;
procedure TfrmMain.chkPWMEnable0Click(Sender: TObject);
begin
If chkPWMEnable0.Checked Then
Begin
txtPeriod0.Enabled := True;
txtHiPeriod0.Enabled := True;
End
Else
Begin
txtPeriod0.Enabled := False;
txtHiPeriod0.Enabled := False;
End
end;
procedure TfrmMain.chkPWMEnable1Click(Sender: TObject);
begin
If chkPWMEnable1.Checked Then
Begin
txtPeriod1.Enabled := True;
txtHiPeriod1.Enabled := True;
End
Else
Begin
txtPeriod1.Enabled := False;
txtHiPeriod1.Enabled := False;
End
end;
procedure TfrmMain.chkPWMEnable2Click(Sender: TObject);
begin
If chkPWMEnable2.Checked Then
Begin
txtPeriod2.Enabled := True;
txtHiPeriod2.Enabled := True;
End
Else
Begin
txtPeriod2.Enabled := False;
txtHiPeriod2.Enabled := False;
End
end;
procedure TfrmMain.btnExitClick(Sender: TObject);
begin
Close;
end;
procedure TfrmMain.btnRunClick(Sender: TObject);
var
code: Integer;
i: Integer;
begin
ErrorNum := DRV_DeviceOpen(lDeviceNum, lDeviceHandle);
If ( DoesErr(ErrorNum) = 1 ) Then
Exit;
ptDevGetFeatures.buffer := @ptDevFeatures;
ptDevGetFeatures.size := sizeof(ptDevFeatures);
ErrorNum := DRV_DeviceGetFeatures(lDeviceHandle, ptDevGetFeatures);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
{ check number of counter channels }
If ptDevFeatures.usMaxTimerChl = 0 Then
Begin
MessageBox(Handle, 'No Counter Channel', 'Driver Message', IDOK);
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
For i := 0 To 2 Do
Begin
If chkPWMEnable[i].Checked Then
Begin
{ Config channel i PWM }
ptCounterPWMSetting.Port := i;
Val(txtPeriod[i].Text, ptCounterPWMSetting.Period, code);
Val(txtHiPeriod[i].Text, ptCounterPWMSetting.HiPeriod, code);
ptCounterPWMSetting.GateMode := 0;
ptCounterPWMSetting.OutCount := 0;
ErrorNum := DRV_CounterPWMSetting(lDeviceHandle, ptCounterPWMSetting);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
DRV_DeviceClose(lDeviceHandle);
Exit
End;
{ Enable Channel i PWM }
ErrorNum := DRV_CounterPWMEnable(lDeviceHandle, i);
If ( DoesErr(ErrorNum) = 1 ) Then
Begin
DRV_DeviceClose(lDeviceHandle);
Exit;
End;
End;
End;
grpPWMConfiguration.Enabled := False;
grpDevSelect.Enabled := False;
btnRun.Enabled := False;
btnStop.Enabled := True;
btnExit.Enabled := False;
end;
procedure TfrmMain.btnStopClick(Sender: TObject);
var
i: Integer;
begin
For i := 0 To 2 Do
Begin
{ Stop Channel i PWM }
ErrorNum := DRV_CounterReset(lDeviceHandle, i);
End;
DRV_DeviceClose(lDeviceHandle);
grpPWMConfiguration.Enabled := True;
grpDevSelect.Enabled := True;
btnRun.Enabled := True;
btnStop.Enabled := False;
btnExit.Enabled := True;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -