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

📄 pwmform.cpp

📁 16 relay output channels and 16 isolated digital input channels LED indicators to show activated
💻 CPP
字号:
//---------------------------------------------------------------------------
/*
  Program: PWM
  Description: BCB Demo program for PWM output
  Version: 1.0
  Date: 08/29/02                   Advantech. Co., Ltd.
*/
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PWMForm.h"

#include "..\..\..\..\include\driver.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;

//---------------------------------------------------------------------------
//the static varialble for PWM operation
static ULONG lDeviceNum;                    //Advantech Device Number in your system
static LONG lDriverHandle;                  //Driver handle
static char szDescription[80];              //description for Select Device

static      DEVFEATURES     DevFeatures;    // structure for device features
static      PT_DeviceGetFeatures  ptDevGetFeatures;
static      PT_CounterPWMSetting ptCounterPWMSetting;

static      BYTE        bPort = 0x0;        //output Port 0: disable 1:enable
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
    //This function is for getting the device number
    if(1 == DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription)){
        Application->MessageBoxA("Can not Open Select Device Dialog", "Error");
        btnExitClick(Sender);
    }
    labDeviceName->Caption = AnsiString(szDescription);
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnSelectDeviceClick(TObject *Sender)
{
    //This function is for getting the device number
    DRV_SelectDevice(Handle, True, (ULONG*)&lDeviceNum, szDescription);
    labDeviceName->Caption = AnsiString(szDescription);
}
//---------------------------------------------------------------------------



void __fastcall TfrmMain::chkPWMEnable0Click(TObject *Sender)
{
    //Settings for port0
    if (chkPWMEnable0->Checked){
        txtPeriod0->Enabled = True;
        txtHiPeriod0->Enabled = True;
        bPort |= 0x1;
    }else{
        txtPeriod0->Enabled = False;
        txtHiPeriod0->Enabled = False;
        bPort &= 0xfe;
    }

}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::chkPWMEnable1Click(TObject *Sender)
{
    //Settings for port1
    if (chkPWMEnable1->Checked){
        txtPeriod1->Enabled = True;
        txtHiPeriod1->Enabled = True;
        bPort |= 0x2;
    }else{
        txtPeriod1->Enabled = False;
        txtHiPeriod1->Enabled = False;
        bPort &= 0xfd;
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::chkPluseEnable0Click(TObject *Sender)
{
    if (chkPluseEnable0->Checked){
        txtPluseCount0->Enabled = True;
    }else{
        txtPluseCount0->Enabled = False;
    }        
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::chkPluseEnable1Click(TObject *Sender)
{
    if (chkPluseEnable1->Checked){
        txtPluseCount1->Enabled = True;
    }else{
        txtPluseCount1->Enabled = False;
    }
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::btnExitClick(TObject *Sender)
{
    
    Application->Terminate();
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::txtPeriod0Exit(TObject *Sender)
{
   //Check the validation of input data
    if(txtPeriod0->Text.ToDouble()<=0){
        Application->MessageBoxA("The value must greater than 0","Warning");
        txtPeriod0->SetFocus();
    }
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::txtHiPeriod0Exit(TObject *Sender)
{
    if(txtHiPeriod0->Text.ToDouble()<=0 || txtHiPeriod0->Text.ToDouble()>=txtPeriod0->Text.ToDouble()){
        Application->MessageBoxA("The value must greater than 0 and less than the period time","Warning");
        txtHiPeriod0->SetFocus();
    }
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::txtPeriod1Exit(TObject *Sender)
{
    if(txtPeriod1->Text.ToDouble()<=0){
        Application->MessageBoxA("The value must greater than 0","warning");
        txtPeriod1->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::txtHiPeriod1Exit(TObject *Sender)
{
    if(txtHiPeriod1->Text.ToDouble()<=0 || txtHiPeriod1->Text.ToDouble()>=txtPeriod1->Text.ToDouble()){
        Application->MessageBoxA("The value must greater than 0 and less than the period time","Warning");
        txtHiPeriod1->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::txtPluseCount0Exit(TObject *Sender)
{
    if(txtPluseCount0->Text.ToDouble()<0 || txtPluseCount0->Text.ToDouble()>65535){
        Application->MessageBoxA("The value must between 0 and 65535","Warning");
        txtPluseCount0->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::txtPluseCount1Exit(TObject *Sender)
{
    if(txtPluseCount1->Text.ToDouble()<0 || txtPluseCount1->Text.ToDouble()>65535){
        Application->MessageBoxA("The value must between 0 and 65535","Warning");
        txtPluseCount1->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TfrmMain::btnRunClick(TObject *Sender)
{
    LRESULT  ErrCde;
    char        szErrMsg[80];     

    if((False==chkPWMEnable0->Checked) && (False==chkPWMEnable1->Checked)){
        Application->MessageBoxA("please select a port","Warning");
        return;
    }
    ErrCde = DRV_DeviceOpen(lDeviceNum, (LONG far *) &lDriverHandle);
    if (ErrCde != SUCCESS){
        strcpy(szErrMsg,"Device open error !");
        Application->MessageBoxA((LPCSTR)szErrMsg,"Device Open");
        return;
    }

    ptDevGetFeatures.buffer = (LPDEVFEATURES )&DevFeatures;
    ptDevGetFeatures.size   = sizeof(DEVFEATURES);
    ErrCde = DRV_DeviceGetFeatures(lDriverHandle, (PT_DeviceGetFeatures*)&ptDevGetFeatures);
    if (ErrCde != SUCCESS){
        DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
        Application->MessageBoxA((LPCSTR)szErrMsg,"Driver Message");
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return;
    }
    if (0 == DevFeatures.usMaxTimerChl){
        Application->MessageBoxA((LPCSTR)"No Counter Channel","Driver Message");
        DRV_DeviceClose((LONG far *)&lDriverHandle);
        return;
    }
    //start counter

    if(chkPWMEnable0->Checked){
        ptCounterPWMSetting.Port = 0;
        ptCounterPWMSetting.Period = txtPeriod0->Text.ToDouble();
        ptCounterPWMSetting.HiPeriod = txtHiPeriod0->Text.ToDouble();
        ptCounterPWMSetting.GateMode = 0;
        if(chkPluseEnable0->Checked){
                ptCounterPWMSetting.OutCount = txtPluseCount0->Text.ToDouble();
        }else{
                ptCounterPWMSetting.OutCount = 0;
        }
        ErrCde = DRV_CounterPWMSetting(lDriverHandle, (LPT_CounterPWMSetting)&ptCounterPWMSetting);
        if (ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
                return;
        }
    }

    if(chkPWMEnable1->Checked){
        ptCounterPWMSetting.Port = 1;
        ptCounterPWMSetting.Period = txtPeriod1->Text.ToDouble();
        ptCounterPWMSetting.HiPeriod = txtHiPeriod1->Text.ToDouble();
        ptCounterPWMSetting.GateMode = 0;
        if(chkPluseEnable1->Checked){
                ptCounterPWMSetting.OutCount = txtPluseCount1->Text.ToDouble();
        }else{
                ptCounterPWMSetting.OutCount = 0;
        }
        ErrCde = DRV_CounterPWMSetting(lDriverHandle, (LPT_CounterPWMSetting)&ptCounterPWMSetting);
        if (ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
                return;
        }
    }

    ErrCde = DRV_CounterPWMEnable(lDriverHandle,bPort);
    if(ErrCde != SUCCESS){
                DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
                Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
                return;
    }

    btnRun->Enabled = False;
    btnStop->Enabled = True;
    btnExit->Enabled = False;
    grpPluseOutCount->Enabled = False;
    grpPWMConfiguration->Enabled = False;
    grpDevSelect->Enabled = False;
}
//---------------------------------------------------------------------------


void __fastcall TfrmMain::btnStopClick(TObject *Sender)
{
    LRESULT  ErrCde;
    char szErrMsg[80];
    ErrCde = DRV_CounterReset(lDriverHandle, bPort);
    if(ErrCde != SUCCESS){
          DRV_GetErrorMessage(ErrCde,(LPSTR)szErrMsg);
          Application->MessageBoxA((LPCSTR)szErrMsg, "Driver Message");
    }
    DRV_DeviceClose((LONG far*)&lDriverHandle);
    btnExit->Enabled = True;
    btnRun->Enabled = True;
    btnStop->Enabled = False;
    grpPluseOutCount->Enabled = True;
    grpPWMConfiguration->Enabled = True;
    grpDevSelect->Enabled = True;

}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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