//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "PAI1216u.h" #include "ACCES32.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #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-AI12-16", "PCI-AI12-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 TForm1::TForm1(TComponent* Owner) : TForm(Owner) { VoltsDisplay[0] = Edit0; VoltsDisplay[1] = Edit1; VoltsDisplay[2] = Edit2; VoltsDisplay[3] = Edit3; VoltsDisplay[4] = Edit4; VoltsDisplay[5] = Edit5; VoltsDisplay[6] = Edit6; VoltsDisplay[7] = Edit7; VoltsDisplay[8] = Edit8; VoltsDisplay[9] = Edit9; VoltsDisplay[10] = Edit10; VoltsDisplay[11] = Edit11; VoltsDisplay[12] = Edit12; VoltsDisplay[13] = Edit13; VoltsDisplay[14] = Edit14; VoltsDisplay[15] = Edit15; CountsDisplay[0] = Edit16; CountsDisplay[1] = Edit17; CountsDisplay[2] = Edit18; CountsDisplay[3] = Edit19; CountsDisplay[4] = Edit20; CountsDisplay[5] = Edit21; CountsDisplay[6] = Edit22; CountsDisplay[7] = Edit23; CountsDisplay[8] = Edit24; CountsDisplay[9] = Edit25; CountsDisplay[10] = Edit26; CountsDisplay[11] = Edit27; CountsDisplay[12] = Edit28; CountsDisplay[13] = Edit29; CountsDisplay[14] = Edit30; CountsDisplay[15] = Edit31; GetCardInfo(); ChannelsCombo->ItemIndex = 0; } //--------------------------------------------------------------------------- void __fastcall TForm1::ExitBtnClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::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) { Memo1->Lines->Clear(); Memo1->Lines->Append("No Cards found!"); Memo1->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."; Memo1->Lines->Append(AText); } Memo1->Lines->Append("Make sure you have run PCIFIND.EXE."); RunFlag = False; } if (RunFlag) CardCombo->ItemIndex = 0; DriverRegistry->Free(); } //--------------------------------------------------------------------------- void __fastcall TForm1::GetDataBtnClick(TObject *Sender) { if (!RunFlag) Memo1->Lines->Append("Cannot run without card!"); else if (Testing) { Timer1->Enabled = false; Testing = false; GetDataBtn->Caption = "Get Data"; } else { Timer1->Enabled = true; Testing = true; GetDataBtn->Caption = "Stop Data"; } } //--------------------------------------------------------------------------- void __fastcall TForm1::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 TForm1::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 { Memo1->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 TForm1::SetChannel(unsigned long BASE, unsigned char channel) { OutPortB(BASE+2, channel << 4); //channel selected in upper nibble at base+2 } //--------------------------------------------------------------------------- void __fastcall TForm1::StartConversion(unsigned long BASE) { OutPortB(BASE+0, 0); //write anything to base+0 to start conversion } //--------------------------------------------------------------------------- unsigned short __fastcall TForm1::WaitForEOC(unsigned long BASE) { unsigned short timeout=0xFFFF; while(((InPortB(BASE+4) & 0x80) == 0) && timeout--); return timeout; //0==error } //--------------------------------------------------------------------------- short __fastcall TForm1::RetrieveADConversion(unsigned long BASE) { return (InPort(BASE+0) & 0x0FFF); } //--------------------------------------------------------------------------- void __fastcall TForm1::CheckFifo(unsigned long BASE) { while((InPortB(BASE+4) & 0x04) == 0x04);//bit goes low when fifo half full } //--------------------------------------------------------------------------- void __fastcall TForm1::EnableCounters(unsigned long BASE) { OutPortB(BASE+4, 0x01); //set counter enable bit, starts conversions } //--------------------------------------------------------------------------- void __fastcall TForm1::DisableCounters(unsigned long BASE) { OutPortB(BASE+4, 0); } //--------------------------------------------------------------------------- void __fastcall TForm1::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 TForm1::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 TForm1::Timer1Timer(TObject *Sender) { if (WhichCard[CardCombo->ItemIndex] == 1) { Fifo(WhichAddress[CardCombo->ItemIndex]); } else NoFifo(WhichAddress[CardCombo->ItemIndex]); } //--------------------------------------------------------------------------- void __fastcall TForm1::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); } } //---------------------------------------------------------------------------