//--------------------------------------------------------------------------- #include #pragma hdrstop #include "ScanUnit.h" #include "ACCES32.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TScanForm *ScanForm; TPCI_COMMON_CONFIG buf[64]; unsigned Address8, Address16; bool done = false; //--------------------------------------------------------------------------- __fastcall TScanForm::TScanForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void CtrMode(unsigned long addr, byte cntr, byte mode) { int ctrl; ctrl = ((cntr << 6) | 0x30 | (mode << 1)); OutPortB(addr + 3, ctrl); }; // end CtrMode void CtrLoad(unsigned long addr, unsigned c, unsigned val) { OutPortB(addr + c, val & 0x00FF); OutPortB(addr + c, (val >> 8) & 0x00FF); }; // end CtrLoad unsigned long CtrRead(unsigned long addr, unsigned c) { OutPortB(addr + 3, c << 6); return InPortB(addr + c) + (InPortB(addr + c) << 8); }; // end CtrRead void __fastcall TScanForm::FormCreate(TObject *Sender) { int num, i; bool found; found = false; DriverRegistry = new TRegistry; DriverRegistry->RootKey = HKEY_LOCAL_MACHINE; 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); } DriverRegistry->OpenKey(MyKey, true); try { num = DriverRegistry->ReadInteger("NumDevices"); } catch ( ... ) { num = 0; } if (num > 0) DriverRegistry->ReadBinaryData("PCICommonConfig",buf,num*sizeof(TPCI_COMMON_CONFIG)); for (i = 0; i < num; i++) { if (found) break; switch (buf[i].DeviceID) { case 0xECE8: Address16Edit->Text = IntToHex((int)(buf[i].BaseAddresses[2] & 0xFFF8), 4); Address8Edit->Text = IntToHex((int)(buf[i].BaseAddresses[3] & 0xFFF8), 4); found = true; break; } // end switch } // end for i if (!found) Application->MessageBox("No LPCI-A16-16A found. Please check that the card is installed correctly, and you have run PCIFind.", "LPCI-A16-16A not found", MB_ICONWARNING + MB_OK); DriverRegistry->Free(); } // end FormCreate //--------------------------------------------------------------------------- void __fastcall TScanForm::StartButtonClick(TObject *Sender) { int i, j; AnsiString TempString; int maxchan; double span; bool bipolar; Address8 = StrToInt("0x" + Address8Edit->Text); Address16 = StrToInt("0x" + Address16Edit->Text); StartButton->OnClick = StopButtonClick; StartButton->Caption = "End"; done = false; TempString = "Card is configured for "; if ((InPortB(Address8 + 8) & 1) == 1) { maxchan = 15; TempString = TempString + "Single Ended (16CH), "; } else { maxchan = 7; TempString = TempString + "Differential (8CH), "; } // end if/else if ((InPortB(Address8+8) & 4) == 4) span = 10.0; else span = 20.0; if ((InPortB(Address8 + 8) & 2) == 2) { bipolar = true; TempString = TempString + "±" + IntToStr((int)(span / 2)) + " (Bipolar) Volt Range"; } else { bipolar = false; TempString = TempString + "0 - " + IntToStr((int)(span)) + " (Unipolar) Volt Range"; } // end if/else StatusLabel->Caption = TempString; OutPortB(Address8 + 0xD, 1); //turn on 2s complement. CtrMode(Address8 + 0x14, 0, 2); //set counter 0 mode 2 CtrMode(Address8 + 0x14, 1, 2); //set counter 1 mode 2 CtrMode(Address8 + 0x14, 2, 2); //set counter 2 mode 2 CtrLoad(Address8 + 0x14, 0, 5); CtrLoad(Address8 + 0x14, 1, 1000); //divide counter source by (1000x1000) to get 10Hz CtrLoad(Address8 + 0x14, 2, 1000); OutPortB(Address8 + 0x1E, 0xC0); //counter enable OutPortB(Address8 + 0x3, 0); //disable burst //0x20 half //0x80 half while ((InPortB(Address8 + 0x8) & 0x80) != 0x80) InPort(Address16); //read fifo until empty OutPortB(Address8 + 0x2, (maxchan << 4)); //write high and low scan limits OutPortB(Address8 + 0x1B, 0x01); //gate timer, start timing OutPortB(Address8 + 0x1A, 0x11); //GO! 1 sample per channel TempString = ""; for (i = 0; i <= maxchan; i++) { TempString = TempString + Format("%d\t", ARRAYOFCONST((i))); if (i == 7) TempString = TempString; } // end for i OutMemo->Items->Add(TempString); TempString = ""; do { while((InPortB(Address8 + 0x8) & 0x80) > 0) Application->ProcessMessages(); // wait for not empty TempString = ""; while (!((InPortB(Address8 + 0x8) & 0x80) > 0)) { //drain until empty j = InPort(Address16); if (bipolar) j = (short)(j); TempString = TempString + Format("%6.3f\t", ARRAYOFCONST(((j / 65536.0) * span))); }; // end while not InPortB while (OutMemo->Items->Count > ((OutMemo->Height / OutMemo->ItemHeight) - 2)) OutMemo->Items->Delete(1); OutMemo->Items->Add(TempString); Application->ProcessMessages(); } while (!done); CtrMode(Address8 + 0x14, 2, 2); //stop data OutMemo->Items->Add("Program done."); } // end StartButtonClick //--------------------------------------------------------------------------- void __fastcall TScanForm::StopButtonClick(TObject *Sender) { done = true; StartButton->OnClick = StartButtonClick; StartButton->Caption = "Start"; } // end StopButtonClick //--------------------------------------------------------------------------- void __fastcall TScanForm::ExitButtonClick(TObject *Sender) { StopButtonClick(Sender); Close(); } // end ExitButtonClick //---------------------------------------------------------------------------