_UNDERSTANDING THE GPIB_ by Don Morgan [LISTING ONE] /* Scope.c */ #include #include "decl.h" /* a header file containing declares pertinent to the National Instruments library*/ /* Application program variables passed to GPIB functions */ char rd[512]; /* read data buffer*/ int bd; /* board number, in this case that representing controller */ main() { /* Assign unique identifier to board 0 and store in variable bd. Check for error.*/ if ((bd = ibfind ("GPIB0")) < 0) error(); printf("\ninitializing controller"); getch(); /* Send the Interface Clear (IFC). */ if (ibsic (bd) & ERR) error(); /* Turn on Remote Enable (REN) signal so instrument can be programmed. */ if (ibsre (bd,1) & ERR) error(); /* Put scope in remote mode by addressing it to listen, send Device Clear (DCL) message to clear internal device functions, and address GPIB board to talk. */ ibcmd (bd,"?_@' ",4); if (ibsta & ERR) error(); /* Write the function, range, and trigger source instructions to scope. */ ibwrt (bd,":bnc probe",10); if (ibsta & ERR) error(); ibwrt (bd,":acquire:type normal",20); if (ibsta & ERR) error(); ibwrt (bd,":timebase:range 5e-4",20); if (ibsta & ERR) error(); ibwrt (bd,":timebase:delay 0",17); if (ibsta & ERR) error(); ibwrt (bd,":timebase:reference center",26); if (ibsta & ERR) error(); ibwrt (bd,":timebase:mode triggered",24); if (ibsta & ERR) error(); ibwrt (bd,":channel1:probe 10",18); if (ibsta & ERR) error(); ibwrt (bd,":channel:range 1.2",18); if (ibsta & ERR) error(); ibwrt (bd,":trigger:mode edge",18); if (ibsta & ERR) error(); ibwrt (bd,":trigger:slope positive",23); if (ibsta & ERR) error(); ibwrt (bd,":trigger:level 300mv",20); if (ibsta & ERR) error(); ibwrt (bd,":trigger:source channel1",24 ); if (ibsta & ERR) error(); /* Scope is now ready to go. Set up SRQ by first clearing internal data structures. */ ibwrt (bd,"*cls",4); if (ibsta & ERR) error(); /* Clear the trigger by issuing the command to return the bit. */ ibwrt (bd,":ter?",5); if (ibsta & ERR) error(); /* Then make the scope a talker. */ ibcmd (bd,"?_ G",4); /* And become a listener. Read three bytes or stop when an EOI is received. */ ibrd(bd,rd,3); if (ibsta & ERR) error(); /* When the data is in buffer issue an untalk and unlisten, then make the controller a talker again and scope a listener. */ ibcmd (bd,"?_@'",4); /* Enable the SRQ bit within scope that will case an RQS on next trigger. */ ibwrt (bd,"*sre 1",6); if (ibsta & ERR) error(); /* Now wait for the trigger. */ if (ibwait (bd,SRQI) & (ERR)) error(); /* If we are here, scope must have been triggered and must have asserted SRQ command line. Do a serial poll. First unaddress bus devices and and send Serial Poll Enable (SPE) command, followed by scope's talk address, and GPIB board's listen address. */ ibcmd (bd,"?_\x18G ",5); /*UNL UNT SPE TAD MLA*/ if (ibsta & ERR) error(); /* Now read status byte. If it is 0x41, the scope has valid data to send; otherwise it has a fault condition to report. */ ibrd (bd,rd,1); if (ibsta & ERR) error(); if ((rd[0] & 0xFF) != 0x41) error(); /* Note that if more than one device is attached to bus, each device must be checked to see which issued the SRQ. */ /* Complete serial poll by sending the Serial Poll Disable (SPD) message. */ if (ibcmd (bd,"\x19",1) & ERR) error(); /*Send scope untalk and unlisten; make controller talker and scope listener*/ ibcmd (bd,"?_@'"); /*Tell the scope to print the screen and associated data*/ ibwrt(bd,":hardcopy:page automatic",24); ibwrt(bd,":print?",7); /*Make scope a talker and printer a listener; have controller get out of the way for the transfer*/ ibcmd(bd,"?_@!G",5); ibgts(bd,1); /*Wait for the transfer to complete and reassert control over the bus*/ ibwait(END); /*The program terminates with an interface clear*/ ibsic (bd); } /* Simple error routine that reads system variables and returns them. */ error() { printf("\nGPIB function call error:"); printf("\nibsta=0x%x, iberr=0x%x,",ibsta,iberr); printf("\nibcnt=0x%x\n",ibcnt); } Example 1: (a) OUT "IFC"; /*Interface Clear*/ (b) OUT "REN"; /*Remote Enable*/ (c) OUT "?_@'\0X14"; /*? = Unlisten (UNL), _ = Utalk (UNT), @ = My Talk Address of controller (MTA), ' = My Listen address of scope (MLA), 0X14 = Device clear*/ (d) OUT "?_@'"; /*UNL, UNT, MTA of controller, MLA of scope*/ OUT ":ACQUIRE:TYPE NORMAL"; ; /*Now the device dependent configuration data*/ ; OUT ":TRIGGER:SOURCE CHANNEL1"; OUT ":SRQ 1"; /*Enable SRQ on trigger, bit 1 of SRQ mask register*/ OUT "?_"; /*UNL, UNT*/ (e) OUT "?_\X18G "; /*UNL, UNT, Serial Poll Enable (SPE), Talk Address (TAD) of scope, MLA of controller*/ IN &BUFFER; /*Program puts Serial Poll Byte in a buffer*/ OUT "\0X19"; /*Issue Serial Poll Disable (SPD)*/ (f) OUT "?_@'"; /*UNL, UNT, MTA of controller, MLA of scope*/ OUT "PRINT"; /*Tell scope to print*/ OUT "?_@!G"; /*UNL, UNT, MTA of controller, MLA of printer/plotter, MTA of scope*/ OUT "DATA"; /*Controller takes its own attention (ATN) line low to allow scope to talk to printer/plotter*/ WAIT "END"; /*Controller watches command lines for end or identify (EOI), which means that the transfer is complete*/