//#include //#include //#include #include #include "Detector.inc" #define TRUE 1 #define FALSE 0 unsigned short BASE16=0xC800; unsigned short BASE8=0xC400; unsigned char jumpers; unsigned char adrange; unsigned char dacBrange; unsigned char dacArange; //sbitd takes data and bit, then returns 0x81 or 0x01 based on data[bit] unsigned char sbitd(unsigned int data,unsigned char bit) { return (data & (1< 0xFFF ) count = 0xFFF; else if (count < 0 ) count = 0; outport(DACAddr, count); //Load counts into input register //outport(BASE + 0x08, 0x8000); //Load input register into DAC return double(count)/4096.0L*((DACnum?dacBrange:dacArange)?5.0L:10.0L); //actual V } void EEPROM_Enable(void) { outportb(BASE8+0x0a,0x81);//start bit outportb(BASE8+0x0a,0x01);//Opcode bit 0 (00 = enable/disable) outportb(BASE8+0x0a,0x01);//Opcode bit 1 "" outportb(BASE8+0x0a,0x81);//a5 (must be 1 to enable) outportb(BASE8+0x0a,0x81);//a4 (must be 1 to enable) outportb(BASE8+0x0a,0x01);//a3 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a2 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a1 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a0 (don't care because enable/disable) outportb(BASE8+0x0a,0x00);//end trans } void EEPROM_Disable(void) { outportb(BASE8+0x0a,0x81);//start bit outportb(BASE8+0x0a,0x01);//Opcode bit 0 (00 = enable/disable) outportb(BASE8+0x0a,0x01);//Opcode bit 1 "" outportb(BASE8+0x0a,0x01);//a5 (must be 0 to disable) outportb(BASE8+0x0a,0x01);//a4 (must be 0 to disable) outportb(BASE8+0x0a,0x01);//a3 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a2 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a1 (don't care because enable/disable) outportb(BASE8+0x0a,0x01);//a0 (don't care because enable/disable) outportb(BASE8+0x0a,0x00);//end trans } void EEPROM_Write(unsigned int addr,unsigned int data) { outportb(BASE8+0x0a,0x81);//Start Bit outportb(BASE8+0x0a,0x01);//Opcode Bit 0 (01=write) outportb(BASE8+0x0a,0x81);//Opcode Bit 1 "" outportb(BASE8+0x0a,sbitd(addr,5));//a5 (address to write to) outportb(BASE8+0x0a,sbitd(addr,4));//a4 outportb(BASE8+0x0a,sbitd(addr,3));//a3 outportb(BASE8+0x0a,sbitd(addr,2));//a2 outportb(BASE8+0x0a,sbitd(addr,1));//a1 outportb(BASE8+0x0a,sbitd(addr,0));//a0 outportb(BASE8+0x0a,sbitd(data,15));//d15 outportb(BASE8+0x0a,sbitd(data,14));//d14 outportb(BASE8+0x0a,sbitd(data,13));//d13 outportb(BASE8+0x0a,sbitd(data,12));//d12 outportb(BASE8+0x0a,sbitd(data,11));//d11 outportb(BASE8+0x0a,sbitd(data,10));//d10 outportb(BASE8+0x0a,sbitd(data,9));//d9 outportb(BASE8+0x0a,sbitd(data,8));//d8 outportb(BASE8+0x0a,sbitd(data,7));//d7 outportb(BASE8+0x0a,sbitd(data,6));//d6 outportb(BASE8+0x0a,sbitd(data,5));//d5 outportb(BASE8+0x0a,sbitd(data,4));//d4 outportb(BASE8+0x0a,sbitd(data,3));//d3 outportb(BASE8+0x0a,sbitd(data,2));//d2 outportb(BASE8+0x0a,sbitd(data,1));//d1 outportb(BASE8+0x0a,sbitd(data,0));//d0 outportb(BASE8+0x0a,0x00);//end trans delay(20); } unsigned int EEPROM_Read(unsigned int addr) { unsigned int data=0; outportb(BASE8+0x0a,0x81);//Start Bit outportb(BASE8+0x0a,0x81);//Opcode Bit 0 (10=Read) outportb(BASE8+0x0a,0x01);//Opcode Bit 1 "" outportb(BASE8+0x0a,sbitd(addr,5));//a5 (address to write to) outportb(BASE8+0x0a,sbitd(addr,4));//a4 outportb(BASE8+0x0a,sbitd(addr,3));//a3 outportb(BASE8+0x0a,sbitd(addr,2));//a2 outportb(BASE8+0x0a,sbitd(addr,1));//a1 outportb(BASE8+0x0a,sbitd(addr,0));//a0 // delay(10); // inportb(BASE+0x0a);//dummy bit data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d15 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d14 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d13 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d12 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d11 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d10 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d9 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d8 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d7 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d6 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d5 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d4 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d3 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d2 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d1 data=(data<<1) | ((inportb(BASE8+0x0a) & 0x80)?1:0);//d0 outportb(BASE8+0x0a,0x00);//end com return data; } void CAL_ADC(unsigned char M,unsigned char B) { //write cal to A/D, offset first, then slope outportb(BASE8+0x0b,0x18);//a1 pot 00 is offset outportb(BASE8+0x0b,0x08);//a0 "" outportb(BASE8+0x0b,sbitda(B,7));//data bit 7 of offset outportb(BASE8+0x0b,sbitda(B,6));//data bit 6 of offset outportb(BASE8+0x0b,sbitda(B,5));//data bit 5 of offset outportb(BASE8+0x0b,sbitda(B,4));//data bit 4 of offset outportb(BASE8+0x0b,sbitda(B,3));//data bit 3 of offset outportb(BASE8+0x0b,sbitda(B,2));//data bit 2 of offset outportb(BASE8+0x0b,sbitda(B,1));//data bit 1 of offset outportb(BASE8+0x0b,sbitda(B,0));//data bit 0 of offset outportb(BASE8+0x0b,0x20);//end trans //write slope now outportb(BASE8+0x0b,0x18);//a1 pot 01 is slope of A/D outportb(BASE8+0x0b,0x88);//a0 pot 01 is slope of A/D outportb(BASE8+0x0b,sbitda(M,7));//data bit 7 of slope outportb(BASE8+0x0b,sbitda(M,6));//data bit 6 of slope outportb(BASE8+0x0b,sbitda(M,5));//data bit 5 of slope outportb(BASE8+0x0b,sbitda(M,4));//data bit 4 of slope outportb(BASE8+0x0b,sbitda(M,3));//data bit 3 of slope outportb(BASE8+0x0b,sbitda(M,2));//data bit 2 of slope outportb(BASE8+0x0b,sbitda(M,1));//data bit 1 of slope outportb(BASE8+0x0b,sbitda(M,0));//data bit 0 of slope outportb(BASE8+0x0b,0x20);//end trans } void CAL_DAC(unsigned char DACNum,unsigned char M) { outportb(BASE8+0x0b,0x03);//just to make sure outportb(BASE8+0x0b,0x01);//a1 pot 00 is slope of DAC0 outportb(BASE8+0x0b,sbitd(DACNum,0));//a0 pot 01 is slope of DAC1 outportb(BASE8+0x0b,sbitd(M,7));//data bit 7 of slope outportb(BASE8+0x0b,sbitd(M,6));//data bit 6 of slope outportb(BASE8+0x0b,sbitd(M,5));//data bit 5 of slope outportb(BASE8+0x0b,sbitd(M,4));//data bit 4 of slope outportb(BASE8+0x0b,sbitd(M,3));//data bit 3 of slope outportb(BASE8+0x0b,sbitd(M,2));//data bit 2 of slope outportb(BASE8+0x0b,sbitd(M,1));//data bit 1 of slope outportb(BASE8+0x0b,sbitd(M,0));//data bit 0 of slope outportb(BASE8+0x0b,0x04);//end trans } void CAL(void) { unsigned char DAM, DBM, AM, AB; AB=EEPROM_Read(adrange+0); //byte 0-7 in EEPROM holds "B" for ADC AM=EEPROM_Read(adrange+8); //byte 8-f in EEPROM holds "M" for ADC DAM=EEPROM_Read(dacArange+0x10); //byte x10,x11 in EEPROM holds "M" for DAC0 DBM=EEPROM_Read(dacBrange+0x12); //byte x12,x13 in EEPROM holds "M" for DAC1 CAL_DAC(0,DAM);//write the calibration value from EEPROM to CAL Pots CAL_DAC(1,DBM);// "" CAL_ADC(AM,AB);// "" } void init(void) { jumpers=inportb(BASE8+0x08);//jumper configuration readback adrange=jumpers & 0x07;//d2,d1,d0 are 5/10, uni/bip, 16/8 dacBrange=(jumpers>>3) & 0x01;//d4,d3 are 5/10 for daca,b dacArange=(jumpers>>4) & 0x01; outportb(BASE8 + 0x0E, 0x01); //Select counters 1 & 2 for A/D start CAL(); clrscr(); puts("LPCI-A16-16A Sample\n"); cprintf("A/D: %s %s\r\n" "DAC A: %s\r\n" "DAC B: %s", (adrange&0x02)? ((adrange&0x04)?"ñ5V":"ñ10V") : ((adrange&0x04)?"0-10V":"0-10V(Low Res)") //0-10V(Low Res) is , //in theory 0-20V, (adrange&0x01)?"16ch":"8ch", //but the amps cap dacArange?"0-5V":"0-10V", //at 10V dacBrange?"0-5V":"0-10V" ); } void DacSample(void) { char key=0; double percent=0; char done=0; puts("This sample will write to DAC 0 and DAC 1."); puts("Press 1-0 keys for 10% to 100% DAC value, use \"~\" for 0%"); puts("Press ESC to exit sample"); outportb(BASE16 + 0x04, 0xE000); do{ key=getch(); if (key==27) done=1; if (isdigit(key)) percent=(key-'0')/10.0L; if ((key=='`') || (key=='~')) percent=0.0; if (key=='0') percent=1.0L; //if (key == ' ') { outport(BASE + 0x08, 0x8000); continue; } gotoxy(1,20); printf("Writing to DAC 0: %3.2lf\n",DAC(0,percent*(dacArange?4.9988L:9.9976L))); printf("Writing to DAC 1: %3.2lf\n",DAC(1,(1.0L-percent)*(dacBrange?4.9988L:9.9976L))); }while (!done); puts("Done."); } void ADSample(void) #define STARTCONVERSION (outportb(BASE8+0,0)) #define CHECKFOREOC (!(inportb(BASE8+8) & 0x80)) #define WAITFOREOC for(int timer=10000;!CHECKFOREOC && timer;timer--) #define WAITFORDATA for(int timer=10000;!CHECKFOREOC && timer;timer--) #define READADDATA (inport(BASE16+0)) { int done=0; unsigned data,CHANNEL; float ADMul, FData; unsigned int ADSpan, ADOffset; clrscr(); puts("Free Run A/D, Gain Code 0, polling FIFO\n"); puts("Press ESC to exit."); ADSpan = (adrange&0x04)?10.0:20.0; ADOffset = (adrange&0x02) ? (ADSpan / 2) : 0; ADMul = (float)ADSpan / (float)0x10000; CHANNEL = (adrange & 0x01) ? 0xF0 : 0x70; //inportb(BASE+0x07); //reset gains outportb(BASE8+0x02, CHANNEL); //configure channels outportb(BASE8+0x01, 0); //clear FIFO do{ STARTCONVERSION; WAITFORDATA; gotoxy(1,CHANNEL+9); data=READADDATA; FData = data * ADMul - ADOffset; printf("Current AD channel %1X Reading:%04X = %f ",CHANNEL,data,FData); CHANNEL = (CHANNEL + 1) % ((adrange & 0x01) ? 16 : 8); if (timer<=0) cputs("<--AD TIMEOUT\n\r"); if (kbhit()) if (getch()==27) done=TRUE; }while (!done); } void main(void) { char done=FALSE; clrscr(); if ( !DoDetect(0, &BASE16, &BASE8) ) return; // outport(BASE16 + 4, 0x0000); // outport(BASE16 + 6, 0x0000); do{ init(); puts("\n\nSelect which sample to run:\n 1. DAC Sample\n 2. A/D Sample\n 0. Exit Program\n"); switch(toupper(getch())) { case '1':case 'D':DacSample();break; case '2':case 'A':ADSample();break; case '0':case '3':case 'X':case 27:done=TRUE;break; }//end switch }while (!done); puts("\nDone."); }