di2550.txt ;**************************************************************************************************** ; LISTING 1 ; ; "Use your printer port as a high-current ammeter," EDN, July 6, 2000, pg 144 ; ; http://www.ednmag.com/ednmag/reg/2000/070600/designideas.htm#114di6 ;**************************************************************************************************** #include #include #include #include #include #define CLEAR 0x08 /* Clear the counter contents*/ #define ENABLE 0x04 /* Allow the pulses to the counters */ #define INHIBIT 0x00 /* Inhibit the pulses to the counter*/ #define LSNIBBLE 0x00 /* Enable Lowest significant nibble of the counter output*/ int TIMER=0; /* Time Base Variable*/ float CURRENT=0.0; /* Measured Current*/ int CUWORD=0; int MCR, DPLPT1, SPLPT1; unsigned char nib0,nib1,nib2,nib3,byte1,byte2; float y1 = 0.0,y2=0.0; /* Interrupt routine declarations */ void interrupt (*oldvect)(); /* Routine for Time Base of ?1sec*/ unsigned int ISENSE(void); /* Routine declaration for reading the counter output*/ void interrupt timebase() { disable(); outportb(DPLPT1, ENABLE); TIMER++; oldvect(); enable(); } unsigned int ISENSE(void) { int temp=0; y1=0.0; y2=0.0; byte1=0,byte2=0; /*Read four nibbles, nibble 3 the most significant nibble, first*/ outportb(DPLPT1,0x03); nib3=inportb(SPLPT1); /*nibble-2,second*/ outportb(DPLPT1,0x02); nib2=inportb(SPLPT1)>>4; /*nibble-1,third*/ outportb(DPLPT1,0x01); nib1=inportb(SPLPT1); /*nibble-0,the least significant*/ outportb(DPLPT1,0x00); nib0=inportb(SPLPT1)>>4 ; nib3=nib3 & 0xF0; byte2=nib3|nib2; nib1 = nib1 & 0xF0; byte1=nib1 | nib0; /* Convert binary words byte1,byte2 to decimal*/ for(i=8;i<16;i++) { temp=byte2; byte2=byte2 &(0x01); y1=y1+byte2*pow(2,i); byte2=temp; byte2=byte2>>1; } for(i=0;i<8;i++) { temp=byte1; byte1=byte1 &(0x01); y2=y2+byte1*pow(2,i); byte1=temp; byte1=byte1>>1; } CUWORD=(y1+y2); return(CUWORD);/*Return the decimal equivalent of the Counter output*/ } void main(void) /*Main Program starts here*/ { float CF; CF=18.2/18; clrscr(); printf(" Use your PC printer port and INT 1Ch as a high current ammeter"); printf("\n by K.Suresh,PIFS,MSD,IGCAR,Kalpakkam"); /*check up for availablity of printer port */ DPLPT1 = peek(0x40,08); if (DPLPT1 = = 0) { printf("\n\n\n LPT NOT AVAILABLE!……EXITING"); exit(1); } printf("\n LPT1 address = %X, DPLPT1"); /* Address of DATAPORT of LPT1*/ SPLPT1 = DPLPT1+1; /*Address of STATUS PORT*/ oldvect=getvect(0x1C); while(!kbhit()) { outportb(DPLPT1, INHIBIT); /*Inhibit pulses to the counter*/ outportb(DPLPT1, CLEAR); /* Clear the counter contents*/ TIMER=0; while(TIMER<=18)/*Wait for time base generation of ?1 sec*/ { setvect(0x1C,timebase); } outportb(DPLPT1,INHIBIT); setvect(0x1C,oldvect); ISENSE(); CURRENT = ((CUWORD*0.02)*CF); printf("\n\n Measured Current using LPT1 and 0x1Ch=%fAmps",CURRENT); TIMER=0; } setvect(0x1C,oldvect); return 0; } /*END OF MAIN PROGRAM*/