//--------------------------------------------------------------------------- #include #include "Sample4u.h" #include "acces32.h" #include "irqthrd.h" #include "aiowdm.h" #include #pragma hdrstop //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #define counts 1000 IRQThread *Thrd; int DacNum, Index, BaseAddress; Word progstruct[20000]; struct TCardData { long CardNum; unsigned long Base; }; TCardData CardData[10]; TSample4Form *Sample4Form; //--------------------------------------------------------------------------- __fastcall TSample4Form::TSample4Form(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TSample4Form::FormCreate(TObject *Sender) { unsigned long DeviceID, NameSize, Base; AnsiString Name = ""; int i, CardIndex; if (InPortB(0x61) == 0xAA55) { Application->MessageBox("ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.", "Warning", IDOK); } CardNum = -1; Index = 0; CardIndex = 0; GoFlag = False; DeviceID = 0; Base = 0; for (i = 0; i < GetNumCards();i++) { NameSize = 255; Name.SetLength(NameSize); QueryCardInfo(i, &DeviceID, &Base, &NameSize, Name.c_str()); Name.SetLength(NameSize-1); if ((DeviceID == 0x6cb0) || (DeviceID == 0x6ca8)) { CardBox->Items->Add(Name + " [" + IntToHex(int(Base), 4) + "]"); CardData[CardIndex].CardNum = i; CardData[CardIndex].Base = Base; CardIndex++; } // end if } // end for CardBox->ItemIndex = 0; DACBox->ItemIndex = 0; } // end FormCreate //--------------------------------------------------------------------------- void __fastcall TSample4Form::ISR() //interrupt handler { if (GoFlag) { Index++; OutPort(BaseAddress+(DacNum*2), progstruct[Index]); Index %= counts; } // end if } // end ISR void InitIRQ() { Thrd = new IRQThread(false); Thrd->Resume(); } // end InitIRQ void CtrMode(Word addr, byte cntr, byte mode) { int ctrl; ctrl = (cntr << 6) | 0x30 | (mode << 1); OutPortB(addr+3,ctrl); } // end CtrMode void CtrLoad(Word addr, Word c, Word val) { OutPortB(addr+c,(val & 0x00FF)); OutPortB(addr+c,((val >> 8) & 0x00ff)); } // end CtrLoad void Stop() { Sample4Form->GoFlag = False; InPortB(BaseAddress+6); //disable CTR to promptDAC InPortB(BaseAddress+4); //disable IRQ } // end Stop void Start() { Sample4Form->GoFlag = True; InPortB(BaseAddress+5); //enable CTR to promptDAC InPortB(BaseAddress+3); //enable IRQ } // end Start void TSample4Form::HandleError(long ErrorMsg) { if (GoFlag) if (ErrorMsg == ERROR_INVALID_FUNCTION) Application->MessageBox("Error: this card already has a pending IRQ request.", "IRQ request aborted.", MB_OK); else Application->MessageBox("Some error occurred in the driver.", "IRQ request aborted.", MB_OK); } // end HandleError void __fastcall TSample4Form::StartTestButtonClick(TObject *Sender) { int channel; if ((DACBox->Text != "") && (CardBox->Text != "")) { CardNum = CardData[CardBox->ItemIndex].CardNum; BaseAddress = CardData[CardBox->ItemIndex].Base; DacNum = StrToInt("0x"+DACBox->Text); BoardBox->Visible = False; CurveBox->Visible = True; StatusBox->Visible = True; CtrMode(BaseAddress + 0x24,1,2); //counter 1 mode 2 CtrMode(BaseAddress + 0x24,2,2); //counter 2 mode 2 CtrLoad(BaseAddress + 0x24,1,0x5); //load 10 to counter 1 CtrLoad(BaseAddress + 0x24,2,0x30); //load 1000 to counter 2 for (channel = 0; channel <= 15; channel++) OutPort(BaseAddress + (channel * 2), 0x800); InPortB(BaseAddress + 10); InPortB(BaseAddress + 15); InitIRQ(); memset(progstruct,0,sizeof(progstruct)); //clear buffer CalcuLabel->Caption = ""; GenerLabel->Caption = ""; } // end if } // end InputButtonClick //--------------------------------------------------------------------------- void __fastcall TSample4Form::SineButtonClick(TObject *Sender) { Word i; double rads, sine; Stop(); CalcuLabel->Caption = "Calculating sine wave points....."; rads = 2.0 * M_PI / (counts - 1); // rad per count for (i = 0; i <= counts; i++) { sine = (sin(rads * i) + 1.0) * 2047; progstruct[i] = sine; } // end for GenerLabel->Caption = "Generating sine wave, press any key to stop...."; Start(); } // end SineButtonClick //--------------------------------------------------------------------------- void __fastcall TSample4Form::TriangleButtonClick(TObject *Sender) { Word i; double slope,temp; Stop(); CalcuLabel->Caption = "Calculating triangle wave points....."; slope = 4095.0 / counts * 2.0; // wave form slope for (i = 0; i <= (counts / 2); i++) { temp = slope * i; progstruct[i] = temp; temp = 4095 - temp; progstruct[i + counts / 2] = temp; } // end for GenerLabel->Caption = "Generating triangle wave, press any key to stop...."; Start(); } // end TriangleButtonClick //--------------------------------------------------------------------------- void __fastcall TSample4Form::SawButtonClick(TObject *Sender) { Word i; double slope,temp; Stop(); CalcuLabel->Caption = "Calculating saw tooth wave points....."; slope = 4095.0 / counts; // saw tooth slope for (i = 0; i <= counts; i++) { temp = slope * i; progstruct[i] = temp; progstruct[i] %= 4095; } // end for GenerLabel->Caption = "Generating saw tooth wave, press any key to stop...."; Start(); } // end SawButtonClick //--------------------------------------------------------------------------- void __fastcall TSample4Form::StopTestButtonClick(TObject *Sender) { Stop(); Thrd->Terminate(); AbortRequest(CardNum); BoardBox->Visible = True; CurveBox->Visible = False; StatusBox->Visible = False; } // end StopTestButtonClick //--------------------------------------------------------------------------- void __fastcall TSample4Form::FormCloseQuery(TObject *Sender, bool &CanClose) { /* Abort all pending IRQ requests. If we don't do this before exiting, later when an IRQ comes in or somebody else calls AbortRequest the pending IRQ request will attempt to unlock a thread that no longer exists, which is bad. */ if (CardNum >= 0) { Stop(); AbortRequest(CardNum); } } // end FormCloseQuery void __fastcall TSample4Form::ExitButtonClick(TObject *Sender) { if (CardNum >= 0) { Stop(); AbortRequest(CardNum); } exit(0); } //--------------------------------------------------------------------------- void __fastcall TSample4Form::FormClose(TObject* Sender, TCloseAction &Action) { /* Abort all pending IRQ requests. If we don't do this before exiting, later when an IRQ comes in or somebody else calls AbortRequest the pending IRQ request will attempt to unlock a thread that no longer exists, which is bad. */ if (CardNum >= 0) { Stop(); AbortRequest(CardNum); } } // end FormClose