home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj9105.zip / GPIB-591.ASC < prev    next >
Text File  |  1991-03-15  |  8KB  |  176 lines

  1. _IMPLEMENTING THE GPIB_
  2. by Don Morgan
  3.  
  4. [LISTING ONE]
  5.  
  6. #include <conio.h>
  7. #include "gpib.h"    /*local header file containing declares and defines */
  8.  
  9. /*Reinitialize local interface, whether from power up or device or interface 
  10. clear. It will do nothing, however to initialize system interrupts to take 
  11. advantage of TMS9914A interrupts; you do that */
  12. void Gpib_Init()
  13. {
  14.     int address;
  15.     outp(AUXCMD,SWRST);
  16.             /*software reset*/
  17.     address = (inp(DIPSW) & 0x1f);
  18.            /* Read dipswitch for address of device. Lower 5 bits carry legal
  19.            address of device, clearing upper bits and enables talker & listener
  20.            functions, as well as, disabling dual primary addressing mode.*/
  21.     outp(ADDRES,address);
  22.     outp(IMSK0,BI | BO | SPAS | END);
  23.                  /*set the interrupts BI, BO, SPAS, and END*/
  24.     outp(IMSK1, DCAS | IFC);
  25.                 /*and DCAS and IFC*/
  26.     outp(SERPOL,0x0);
  27.     serial_poll = serial_poll_mask = 0x0;
  28.                 /*clear the serial poll byte and mask*/
  29.     outp(AUXCMD,SWRSTC);
  30.                 /*clear the software reset*/
  31.  }
  32. /* Each time a condition arrises in the device that sets a bit int the serial 
  33. poll status byte, this routine is called. It is passed the mask (MAV or TRG, at
  34. the moment) for the bit to be set and does three things. 1) sets the bit in 
  35. the serial poll status byte. 2) sets the corresponding bit in global variable 
  36. we created serial_poll. This is for reference, the serial poll status byte is 
  37. write only. 3) It checks to see whether this condition is one the user has
  38. determined should cause an SRQ.*/
  39. void Check_Mask(mask);
  40. {
  41.         serial_poll |= mask;
  42.         outp(SERPOL, serial_poll);
  43.         if(serial_poll_mask & mask)
  44.               outp(AUXCMD,RSV2);
  45. }
  46. /* Vectored to by system interrupts.*/
  47.  void Gpib_Int_Han()
  48.  {
  49.     int status_byte;    /*This is where we will save interrupt status 
  50.                           registers until we are through with them*/
  51.     int chr;            /*character holder*/
  52.     status_byte = inp(ISTAT0);
  53.                           /*get what is in interrupt status register 0*/
  54.     if(status_byte & SPAS) {
  55.                    /*place routine here to perform any maintainance necessary 
  56.                    after controller reads the serial poll byte. If you are 
  57.                    using rsv1, you would reset bit 1 (D0 is MSB), otherwise it
  58.                    may be a reset to any bits gone active since the last SRQ.*/
  59.                    }
  60.      if(status_byte & INT0) {
  61.                    /*this bit will be set if there is at least one active, 
  62.                    unhandled interrupt in int status 0. The interrupts that we
  63.                    will be concerned about here are: BI, BO, SPAS, and END*/
  64.      if(status_byte & BI) {
  65.                    /*checking for a byte received from the bus*/
  66.            chr = inp(DATIN);  /*get the character*/
  67.            if(status_byte & EOI)
  68.                flag = TRUE;
  69.            else;
  70.                flag = FALSE;
  71.            receive(chr,flag); /*receive is your function that accepts 
  72.                               character and places it in whatever buffer 
  73.                               scheme is your favorite. flag, is set if an EOI 
  74.                               is detected signalling the end of a string.*/
  75.            }
  76.     else { if(status_byte & BO) {
  77.           chr = get_byte();
  78.           if((chr >> 0x8) == TRUE) {
  79.                   outp(AUXCMD, FEOI);
  80.                   serial_poll &= ~MAV;
  81.                   outp(SERPOL,serial_poll);
  82.                   /*since this is the last character of string, we take this 
  83.                   time to reset 'message available' bit in serial_poll byte*/
  84.                   }
  85.           outp(AUXCMD,chr);
  86.                  /*Toutine for retrieving next byte from transmit buffer. If 
  87.                  you are using the EOI, include some way to indicate that this
  88.                  is last character of string, as FEOI must be placed in 
  89.                  auxiliary command register before last character is sent. In
  90.                  this case, I assumed upper byte of chr was used to carry a 
  91.                  flag explaining the status of that character*/
  92.           }
  93.        }
  94.    if(status_byte & INT1) {
  95.         inp(ISTAT1);   /*Get contents of interrupt status register 1. Here we  
  96.                        are interested in DCAS (device clear) and IFC 
  97.                        (interface clear*/
  98.         if((ISTAT1 & DCAS) || (ISTAT1 & IFC)) Gpib_Init();
  99.                        /*whether we receive a device clear or an interface 
  100.                        clear, we will do the same thing, reinitialize the 
  101.                        local interface*/
  102.         }
  103.  
  104.  
  105. [LISTING TWO]
  106.  
  107. /*GPIB HEADER FILE*/
  108.  
  109. /* The following are defines for address mapping, they are written assuming 
  110. a linear addressing scheme and an 8 bit bus. Depending on the width and 
  111. decoding scheme you use, these defines might be different.*/
  112.  
  113. #define DIPSW ??       /*place dipswitch setting device address address here*/
  114. #define IEEE  ??       /*place base address of TMS9914A here*/
  115. #define IMSK0  IEEE+0  /*interrupt mask register 0*/
  116. #define ISTAT0 IEEE+0  /*interrupt status register 0*/
  117. #define IMASK1 IEEE+1  /*interrupt mask register 1*/
  118. #define ISTAT1 IEEE+1  /*interrupt status register 1*/
  119. #define ADSTAT IEEE+2  /*address status register*/
  120. #define BUSTAT IEEE+3  /*bus status register*/
  121. #define AUXCMD IEEE+3  /*address register*/
  122. #define ADRSWI IEEE+4  /*address switch register*/
  123. #define ADDRES IEEE+4  /*address register*/
  124. #define SERPOL IEEE+5  /*serial poll register*/
  125. #define CMDPAS IEEE+6  /*command pass through register*/
  126. #define PARPOL IEEE+6  /*parallel poll register*/
  127. #define DATIN  IEEE+7  /*data from bus register*/
  128. #define DATOUT IEEE+7  /*data to bus register*/
  129.  
  130. /*next, define some bit masks to be used in the interrupt routines. These will
  131. be AND'd with interrupt status registers to determine proper action to take*/
  132. #define INT0   0x80   /*interrupt register 0 has an interrupt, int status 0*/
  133. #define INT1   0x40   /*interrupt register 1 has an interrupt, int status 0*/
  134. #define BI     0x20   /*Byte In, int status 0*/
  135. #define B0     0x10   /*Byte Out, int status 0*/
  136. #define DCAS   0x8    /*device clear, int status 1*/
  137. #define EOI    0x8    /*End of Identify, int status 0*/
  138. #define SPAS   0x4    /*serial poll active state, int status 0*/
  139. #define IFC    0x1    /*interface clear, int status 1*/
  140.  
  141. /*now, defines for some of the auxiliary commands used*/
  142. #define SWRST  0x80    /*sets the software reset*/
  143. #define SWRSTC 0x0     /*clears the software reset*/
  144. #define FEOI   0x88    /*sent prior to last byte in string to indicate end*/
  145. #define RSV2   0x98    /*alternate and preferred method of asserting SRQ*/
  146.  
  147. /*these bits are used as definitions of serial poll status byte and mask*/
  148. #define MAV    0x20     /*message available*/
  149. #define TRG    0x1      /*trigger*/
  150.  
  151. /*other stuff*/
  152. #define TRUE 0xff
  153. #define FALSE 0x0
  154.  
  155. /*declarations*/
  156. int serial_poll;        /*global variable containing a record of the mask 
  157.                         written to the serial poll register in the TMS9914A.*/
  158. int serial_poll_mask;   
  159. /*for our purposes, the serial poll status byte and serial poll mask byte is 
  160. to be defined as follows, remember D0 is MSB: 
  161. D0   D1   D2   D3   D4   D5   D6   D7
  162. X    RQS  MAV  X    X    X    X    TRG
  163. RQS is the request for service and is read as a 1 by controller during a 
  164. serial poll when this device has asserted an SRQ. MAV is 'message available' 
  165. and is set when there is something in the transmit buffer, reset otherwise.
  166. TRG is the trigger bit and is set when the oscilloscope has been triggered.*/
  167.  
  168. /*function prototypes, these are example only, yours may well be different*/
  169. extern int get_byte(void);
  170. extern void receive(int,int);
  171. extern void Gpib_Int_Han(void);
  172. extern void Gpib_init(void);
  173. extern void Check_Mask(int);
  174.  
  175.  
  176.