//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Sample1u.h" #include "ACCES32.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TSampleForm *SampleForm; #define MyKey "Software\\PCIFIND\\NTioPCI\\Parameters" TRegistry *DriverRegistry; TPCI_COMMON_CONFIG buf[64]; bool RunFlag; unsigned char WhichCard[64]; unsigned long WhichAddress[64]; const char *Cards[2] = {"PCI-A12-16", "PCI-A12-16A"}; TEdit *VoltsDisplay[16], *CountsDisplay[16]; bool Testing; //--------------------------------------------------------------------------- void CtrMode(unsigned int addr, char cntr, char mode) { OutPortB(addr+3, (cntr << 6) | 0x30 | (mode << 1)); } //--------------------------------------------------------------------------- void CtrLoad(unsigned int addr ,int c,int val) { OutPortB(addr+c,val & 0x00FF); OutPortB(addr+c,(val>>8) & 0x00FF); } //--------------------------------------------------------------------------- __fastcall TSampleForm::TSampleForm(TComponent* Owner) : TForm(Owner) { VoltsDisplay[0] = VoltsEdit0; VoltsDisplay[1] = VoltsEdit1; VoltsDisplay[2] = VoltsEdit2; VoltsDisplay[3] = VoltsEdit3; VoltsDisplay[4] = VoltsEdit4; VoltsDisplay[5] = VoltsEdit5; VoltsDisplay[6] = VoltsEdit6; VoltsDisplay[7] = VoltsEdit7; VoltsDisplay[8] = VoltsEdit8; VoltsDisplay[9] = VoltsEdit9; VoltsDisplay[10] = VoltsEditA; VoltsDisplay[11] = VoltsEditB; VoltsDisplay[12] = VoltsEditC; VoltsDisplay[13] = VoltsEditD; VoltsDisplay[14] = VoltsEditE; VoltsDisplay[15] = VoltsEditF; CountsDisplay[0] = CountsEdit0; CountsDisplay[1] = CountsEdit1; CountsDisplay[2] = CountsEdit2; CountsDisplay[3] = CountsEdit3; CountsDisplay[4] = CountsEdit4; CountsDisplay[5] = CountsEdit5; CountsDisplay[6] = CountsEdit6; CountsDisplay[7] = CountsEdit7; CountsDisplay[8] = CountsEdit8; CountsDisplay[9] = CountsEdit9; CountsDisplay[10] = CountsEditA; CountsDisplay[11] = CountsEditB; CountsDisplay[12] = CountsEditC; CountsDisplay[13] = CountsEditD; CountsDisplay[14] = CountsEditE; CountsDisplay[15] = CountsEditF; GetCardInfo(); ChannelsCombo->ItemIndex = 0; } //--------------------------------------------------------------------------- void __fastcall TSampleForm::ExitBtnClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::GetCardInfo(void ) { int num,i,n; AnsiString AText; n = 0; DriverRegistry = new TRegistry; DriverRegistry->RootKey = HKEY_LOCAL_MACHINE; DriverRegistry->OpenKey(MyKey, True); num = DriverRegistry->ReadInteger("NumDevices"); if (num > 0) DriverRegistry->ReadBinaryData("PCICommonConfig",buf,(sizeof(TPCI_COMMON_CONFIG)*num)); for (i=0; iItems->Add(AText.c_str()); n++; } } if (n == 0) { StatusMemo->Lines->Clear(); StatusMemo->Lines->Append("No Cards found!"); StatusMemo->Lines->Append("This may mean the card is not installed."); if (num > 0) { AText = "NOTE: A card was found, but it is not the "; AText += Cards[0]; AText += " card."; StatusMemo->Lines->Append(AText); } StatusMemo->Lines->Append("Make sure you have run PCIFIND.EXE."); RunFlag = False; } if (RunFlag) CardCombo->ItemIndex = 0; DriverRegistry->Free(); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::GetDataBtnClick(TObject *Sender) { if (!RunFlag) StatusMemo->Lines->Append("Cannot run without card!"); else if (Testing) { TestTimer->Enabled = false; Testing = false; GetDataBtn->Caption = "Get Data"; } else { TestTimer->Enabled = true; Testing = true; GetDataBtn->Caption = "Stop Data"; } } //--------------------------------------------------------------------------- void __fastcall TSampleForm::Fifo(unsigned long BASE) { unsigned char chan, numchan; unsigned short Value; short counts; float volts; numchan = ChannelsCombo->ItemIndex == 0 ? 8 : 16; ResetFifos(BASE);//clear channel and data fifos SetFifoChannels(BASE); CtrMode(BASE+8, 1, 2); CtrLoad(BASE+8, 1, 0x2); CtrMode(BASE+8, 2, 2); CtrLoad(BASE+8, 2, 0xFF); EnableCounters(BASE);//conversions started once counters are enabled CheckFifo(BASE); //check fifo bit to see if it's half full DisableCounters(BASE);//stop conversions while((InPortB(BASE+4) & 0x02) == 0x02) //bit goes low when fifo is empty { Value = InPort(BASE); counts = Value & 0xFFF; if (counts > 0x7FF) counts |= 0xF000; chan = Value >> 12; volts = 20.0 * (counts / 4095.0);//convert to volts: counts*max volt span/max counts if (chan >= numchan) { VoltsDisplay[chan]->Text = ""; CountsDisplay[chan]->Text = ""; } else { VoltsDisplay[chan]->Text = volts; CountsDisplay[chan]->Text = counts; } } } //--------------------------------------------------------------------------- void __fastcall TSampleForm::NoFifo(unsigned long BASE) { unsigned char chan, numchan; short counts; float volts; DisableCounters(BASE); numchan = ChannelsCombo->ItemIndex == 0 ? 8 : 16; for (chan = 0; chan < 16; chan++) { if (chan >= numchan) { VoltsDisplay[chan]->Text = ""; CountsDisplay[chan]->Text = ""; } else { SetChannel(BASE, chan); // write channel, range, SE or diff unsigned long Ticks = GetTickCount(); while (Ticks + 2 > GetTickCount()); StartConversion(BASE); // start conversion if (WaitForEOC(BASE) == 0) //WAITFOREOC returns zero if it times out { StatusMemo->Lines->Append("A/D Timed Out"); VoltsDisplay[chan]->Text = ""; CountsDisplay[chan]->Text = ""; } else { counts = (RetrieveADConversion(BASE)); // sign extend result if (counts & 0x800) counts |= 0xF000; // effectively sign-extend volts = 20.0 * (counts / 4095.0); //convert to volts: counts*max volt span/max counts VoltsDisplay[chan]->Text = volts; CountsDisplay[chan]->Text = counts; } } } } //--------------------------------------------------------------------------- void __fastcall TSampleForm::SetChannel(unsigned long BASE, unsigned char channel) { OutPortB(BASE+2, channel << 4); //channel selected in upper nibble at base+2 } //--------------------------------------------------------------------------- void __fastcall TSampleForm::StartConversion(unsigned long BASE) { OutPortB(BASE+0, 0); //write anything to base+0 to start conversion } //--------------------------------------------------------------------------- unsigned short __fastcall TSampleForm::WaitForEOC(unsigned long BASE) { unsigned short timeout=0xFFFF; while(((InPortB(BASE+4) & 0x80) == 0) && timeout--); return timeout; //0==error } //--------------------------------------------------------------------------- short __fastcall TSampleForm::RetrieveADConversion(unsigned long BASE) { return (InPort(BASE+0) & 0x0FFF); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::CheckFifo(unsigned long BASE) { while((InPortB(BASE+4) & 0x04) == 0x04);//bit goes low when fifo half full } //--------------------------------------------------------------------------- void __fastcall TSampleForm::EnableCounters(unsigned long BASE) { OutPortB(BASE+4, 0x01); //set counter enable bit, starts conversions } //--------------------------------------------------------------------------- void __fastcall TSampleForm::DisableCounters(unsigned long BASE) { OutPortB(BASE+4, 0); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::ResetFifos(unsigned long BASE) { OutPortB(BASE+4, 0x48);//0x40 resets channel fifo, 0x08 resets data fifo for (int i=0; i<1024; i++) InPortB(BASE); InPortB(BASE+6); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::SetFifoChannels(unsigned long BASE) { OutPort(BASE+2, 0x0000); OutPort(BASE+2, 0x1010); OutPort(BASE+2, 0x2020); OutPort(BASE+2, 0x3030); OutPort(BASE+2, 0x4040); OutPort(BASE+2, 0x5050); OutPort(BASE+2, 0x6060); OutPort(BASE+2, 0x7070); OutPort(BASE+2, 0x8080); OutPort(BASE+2, 0x9090); OutPort(BASE+2, 0xA0A0); OutPort(BASE+2, 0xB0B0); OutPort(BASE+2, 0xC0C0); OutPort(BASE+2, 0xD0D0); OutPort(BASE+2, 0xE0E0); OutPort(BASE+2, 0xF0F0); InPort(BASE+2); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::TestTimerTimer(TObject *Sender) { if (WhichCard[CardCombo->ItemIndex] == 1) { Fifo(WhichAddress[CardCombo->ItemIndex]); } else NoFifo(WhichAddress[CardCombo->ItemIndex]); } //--------------------------------------------------------------------------- void __fastcall TSampleForm::FormCreate(TObject *Sender) { 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); } } //---------------------------------------------------------------------------