/* DEMO7 : INT_CHAN_0 & INT_CHAN_1 timer interrupt demo */ /* wave generater (with calibration) */ /* (It is designed to be a machine independent) */ /* step1 : Run DEMO7.EXE */ /* -------------------------------------------------------------- */ #include "PIO.H" #define A1_8259 0x20 #define A2_8259 0xA0 #define EOI 0x20 static void interrupt irq_service(); void pio_da16_c0(char cConfig, char cLow, char cHigh); void pio_da16_c1(char cConfig, char cLow, char cHigh); void pio_da16_c2(char cConfig, char cLow, char cHigh); void pio_da16_da(char cChannel_no,int iVal); void init_high(); WORD wBase,wIrq; int irqmask,now_int_state,new_int_state,int_c; WORD wN10V[16],wP10V[16],w00mA[16],w20mA[16],EEP; float fDeltaV[16],fDeltaI[16]; int INT0_L,INT0_H,INT1_L,INT1_H,invert; int v1,v2,v3,v4,h1,h2,h3,h4,MaxChannel; float pi=3.141592654; float d1,d2; int main() { int i,j,k; WORD wBoards,wRetVal,t1,t2,t3,t4,t5,t6; WORD wSubVendor,wSubDevice,wSubAux,wSlotBus,wSlotDevice; char c; clrscr(); /* step1 : find address-mapping of PIO/PISO cards */ wRetVal=PIO_DriverInit(&wBoards,0x80,0x04,0x00);/*for PIO-DA16/8/4*/ printf("\n(1) Threr are %d PIO-DA16/8/4 Cards in this PC",wBoards); if ( wBoards==0 ) exit(0); printf("\n\n-------------- The Configuration Space --------------"); for(i=0;i "); ShowPioPiso(wSubVendor,wSubDevice,wSubAux); } PIO_GetConfigAddressSpace(0,&wBase,&wIrq,&t1,&t2,&t3,&t4,&t5); /* select card_0 */ /* step2 : enable all D/I/O port */ outportb(wBase,0x11); /* /RESET -> 1 */ outportb(wBase+2,0x1c); /* AUX 4/3/2 are D/O, othes D/I */ outportb(wBase+3,0); /* all D/O are Low */ for (i=0; i<64;i++) { if (i<16) { EEP_READ(i,&j,&k); wN10V[i]=(j<<8)+k; } if ((i>=16)&&(i<32)) { EEP_READ(i,&j,&k); wP10V[i-16]=(j<<8)+k; } if ((i>=32)&&(i<48)) { EEP_READ(i,&j,&k); w00mA[i-32]=(j<<8)+k; } if (i>=48) { EEP_READ(i,&j,&k); w20mA[i-48]=(j<<8)+k; } } printf("\n\n(2) DEMO7 D/A Calibrator test\n"); printf("\n<1> PIO-DA16"); printf("\n<2> PIO-DA8"); printf("\n<3> PIO-DA4\n\n"); do { printf("\rSelect (1-3): \b"); c=getche(); switch(c) { case '1': MaxChannel=16; break; case '2': MaxChannel=8; break; case '3': MaxChannel=4; break; default : MaxChannel=0; } } while(MaxChannel==0); for (i=0; i INT_CHAN_0 = !Cout0 = init_low, active_high */ /* --> INT_CHAN_1 = !Cout2 = init_low, active_high */ outportb(wBase+0x2a,0); now_int_state=3; /* now Cout0 & Cout2 is high */ outportb(wBase+5,3); /* enable INT_CHAN_0/1 interrupt */ enable(); } /* -------------------------------------------------------------- */ /* Note : 1.The hold_time of INT_CHAN_0 & INT_CHAN_1 must long */ /* enoug. */ /* 2.The ISR must read the interrupt status again to */ /* identify the active interrupt source. */ /* 3.The INT_CHAN_0 & INT_CHAN_1 can be active at the same */ /* time. */ /* -------------------------------------------------------------- */ void interrupt irq_service() { new_int_state=inportb(wBase+7)&0x03; int_c=new_int_state^now_int_state; if ((int_c&0x01)==1) /* INT_CHAN_0 is active */ { if ((new_int_state&1)==0) /* INT0 change to low now */ { d1=3.6*INT0_L; v1=(sin(d1*pi/180.0)*10+10)/fDeltaV[0]+wN10V[0]; pio_da16_da(0,v1); /* channel_0 */ INT0_L++; if (INT0_L==100) INT0_L=0; } else { INT0_H++; /* INT0 change to high now */ } invert=invert^1; /* generate high_pulse */ } if ((int_c&0x02)==2) /* INT_CHAN_1 is active */ { if ((new_int_state&2)==0) /* INT1 change to low now */ { d2=3.6*INT1_L; v2=(sin(d2*pi/180.0)+1)*10/fDeltaI[1]+w00mA[1]; pio_da16_da(1,v2); /* channel_1 */ INT1_L++; if (INT1_L==100) INT1_L=0; } else /* INT1 change to high now */ { INT1_H++; } invert=invert^2; /* generate high_pulse */ } now_int_state=new_int_state; /* updata interrupt status */ outportb(wBase+0x2a,invert); /* generate high_pulse */ if (wIrq>=8) outportb(A2_8259,0x20); outportb(A1_8259,0x20); } /* -------------------------------------------------------------- */ void pio_da16_c0(char cConfig, char cLow, char cHigh) /* COUNTER0 */ { outportb(wBase+0xcc,cConfig); outportb(wBase+0xc0,cLow); outportb(wBase+0xc0,cHigh); } void pio_da16_c1(char cConfig, char cLow, char cHigh) /* COUNTER1 */ { outportb(wBase+0xcc,cConfig); outportb(wBase+0xc4,cLow); outportb(wBase+0xc4,cHigh); } void pio_da16_c2(char cConfig, char cLow, char cHigh) /* COUNTER2 */ { outportb(wBase+0xcc,cConfig); outportb(wBase+0xc8,cLow); outportb(wBase+0xc8,cHigh); } void pio_da16_da(char cChannel_no,int iVal) { iVal=iVal+(cChannel_no%4)*0x4000; /* cChannel_no : 0 - 15 */ outportb(wBase+0xf0,iVal); /* iVal : 0x0000 - 0x3fff */ outportb(wBase+0xf4,(iVal>>8)); outportb(wBase+0xe0+4*(cChannel_no/4),0xff); } /* -------------------------------------------------------------- */ /* AUX2= D/O = CS of EEP AUX3= D/O = SK of EEP */ /* AUX4= D/O = DI of EEP AUX5= D/I = DO of EEP */ /* -------------------------------------------------------------- */ COMM() /* send start 2-bit (0,1) */ { EEP=0x04; outportb(wBase+3,EEP); /* CS=H */ EEP=0x0C; outportb(wBase+3,EEP); /* SK=H */ /* DI=0 */ EEP=0x04; outportb(wBase+3,EEP); /* SK=L */ EEP=0x14; outportb(wBase+3,EEP); /* DI=1 */ EEP=0x1c; outportb(wBase+3,EEP); /* SK=H */ } /* -------------------------------------------------------------- */ WR_BYTE(int val) /* send the next 8-bit */ { int i,j; for (i=0; i<8; i++) { EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ j=val&0x80; val=val<<1; if (j==0) /* DI=0 */ { EEP=EEP&0x0C; outportb(wBase+3,EEP); } else /* DI=1 */ { EEP=EEP|0x10; outportb(wBase+3,EEP); } EEP=EEP|0x08; outportb(wBase+3,EEP); /* SK=H */ } } /* -------------------------------------------------------------- */ RD_BYTE() /* read the next 8-bit */ { int i,j,val,k; val=0x80; k=0; for (i=0; i<8; i++) { EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ j=(inportb(wBase+7))&0x20; if (j!=0) k+=val; /* DO=1 */ EEP=EEP|0x08; outportb(wBase+3,EEP); /* SK=H */ val=val>>1; } return(k); } /* -------------------------------------------------------------- */ EEP_WR_EN() /* write Enable */ { COMM(); WR_BYTE(0x30); /* write enable command */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ EEP=EEP&0x18; outportb(wBase+3,EEP); /* CS=L */ EEP=EEP&0x0C; outportb(wBase+3,EEP); /* DI=0 */ } /* -------------------------------------------------------------- */ EEP_WR_DIS() /* write Disable */ { COMM(); WR_BYTE(0); /* write disable command */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ EEP=EEP&0x18; outportb(wBase+3,EEP); /* CS=L */ EEP=EEP&0x0C; outportb(wBase+3,EEP); /* DI=0 */ } /* -------------------------------------------------------------- */ EEP_WRITE(int ADDR, int HI, int LO) /* write 16-bit data to EEP */ { int i,j,k; COMM(); ADDR=ADDR|0x40; WR_BYTE(ADDR); /* write command & address */ WR_BYTE(HI); /* high 8-bit data */ WR_BYTE(LO); /* low 8-bit data */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ EEP=EEP&0x18; outportb(wBase+3,EEP); /* CS=L */ EEP=EEP&0x0C; outportb(wBase+3,EEP); /* DI=0 */ for (i=0; i<10; i++) /* delay>250 ns (for EEP) */ { EEP=EEP|0x08; outportb(wBase+3,EEP); /* SK=H */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ } EEP=EEP|0x04; outportb(wBase+3,EEP); /* CS=H */ for (k=0; k<10; k++) /* wait until write finish */ for (i=0; i<30000; i++) { EEP=EEP|0x08; outportb(wBase+3,EEP);/* SK=H */ j=inportb(wBase+7)&0x20; EEP=EEP&0x14; outportb(wBase+3,EEP);/* SK=L */ if (j!=0) return(0); /* write is finished */ } EEP=EEP&0x18; outportb(wBase+3,EEP); /* CS=L */ return(1); /* error --> time out ? */ } /* -------------------------------------------------------------- */ EEP_READ(int ADDR, int *HI, int *LO) /* read 16-bit data from EEP */ { int i,j,k; COMM(); ADDR=ADDR|0x80; WR_BYTE(ADDR); /* read command & address */ EEP=EEP&0x0C; outportb(wBase+3,EEP); /* DI=0 */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ EEP=EEP|0x08; outportb(wBase+3,EEP); /* SK=H */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ (*HI)=RD_BYTE(); /* read high 8-bit data */ (*LO)=RD_BYTE(); /* read low 8-bit data */ EEP=EEP&0x14; outportb(wBase+3,EEP); /* SK=L */ EEP=EEP&0x18; outportb(wBase+3,EEP); /* CS=L */ }