/* ************************************************************** */ /* * Module HP33220 * This module contains I/O functions for the HP33220 function * generator. * * Zachary Wolf * 8/26/04 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "hp33220.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int hp33220_open_dev(int gpib_board_addr, int gpib_dev_addr); void hp33220_close_dev(int dev_ID); int hp33220_out(int dev_ID, char* buf); int hp33220_in(int dev_ID, char* buf); int hp33220_serial_poll(int dev_ID, int* spoll); int hp33220_check_dev_open(int dev_ID); int hp33220_get_errors(int dev_ID); void hp33220_message(char* msg); void hp33220_error(char* msg); /* ************************************************************** */ /* PRIVATE DEVICE TABLE */ /* * This table allows several devices of the same type to be * used in the system. * dev_addr, contains the GPIB addresses of opened devices * dev_descr, contains the device descriptors of opened devices * dev_count, contains the number of devices open of this type */ static int dev_addr[HP33220_MAX_NUM_DEV + 1]; static int dev_descr[HP33220_MAX_NUM_DEV + 1]; static int dev_count; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ /* * cmd, buffer for GPIB I/O strings * msg, buffer for messages to Standard I/O */ static char cmd[HP33220_MAX_CMD + 1]; static char msg[HP33220_MAX_CMD + 1]; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * hp33220_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Input: * gpib_board_addr, address of the GPIB board the device is connected to (0, 1, ...) * gpib_dev_addr, GPIB address of the device (1 to 30, 0 is reserved) * * Output: * dev_ID, identifier for future references to the device * * Zachary Wolf * 8/26/04 */ void hp33220_init(int gpib_board_addr, int gpib_dev_addr, int* ID) { /* Declare variables */ int dev_ID; int err; /* Message */ hp33220_message(""); hp33220_message("Initializing the HP33220..."); /* Check input parameters */ if (gpib_board_addr < 0 || gpib_board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) ampl = 10.; } if (freq < 1.e-6 || freq > 2.e7) { Fmt(msg, "%s 2.e7) freq = 2.e7; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Reset the HP33220 so it is in a known state */ hp33220_out(dev_ID, "*RST"); /* Set the load to high impedance */ //hp33220_out(dev_ID, "OUTP:LOAD INF"); /* Set the load mmm*/ hp33220_set_load(dev_ID, HP33220_GEN_LOAD); /* No DC offset */ hp33220_out(dev_ID, "VOLT:OFFS 0."); /* Set the function to sine wave */ hp33220_out(dev_ID, "FUNC SIN"); /* Set the amplitude */ hp33220_out(dev_ID, "VOLT:UNIT VPP"); Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) ampl = 10.; } if (freq < 1.e-6 || freq > 2.e7) { Fmt(msg, "%s 2.e7) freq = 2.e7; } if (duty_cycle < 20 || duty_cycle > 80) { Fmt(msg, "%s 80) duty_cycle = 80; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Reset the HP33220 so it is in a known state */ hp33220_out(dev_ID, "*RST"); /* Set the load to high impedance */ //hp33220_out(dev_ID, "OUTP:LOAD INF"); /* Set the load mmm*/ hp33220_set_load(dev_ID, HP33220_GEN_LOAD); /* No DC offset */ hp33220_out(dev_ID, "VOLT:OFFS 0."); /* Set the function to square wave */ hp33220_out(dev_ID, "FUNC SQU"); /* Set the amplitude */ hp33220_out(dev_ID, "VOLT:UNIT VPP"); Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) ampl = 10.; } if (freq < 1.e-6 || freq > 2.e5) { Fmt(msg, "%s 2.e5) freq = 2.e5; } if (symm < 0 || symm > 100) { Fmt(msg, "%s 100) symm = 100.; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Reset the HP33220 so it is in a known state */ hp33220_out(dev_ID, "*RST"); /* Set the load to high impedance */ //hp33220_out(dev_ID, "OUTP:LOAD INF"); /* Set the load mmm*/ hp33220_set_load(dev_ID, HP33220_GEN_LOAD); /* No DC offset */ hp33220_out(dev_ID, "VOLT:OFFS 0."); /* Set the function to ramp wave */ hp33220_out(dev_ID, "FUNC RAMP"); /* Set the amplitude */ hp33220_out(dev_ID, "VOLT:UNIT VPP"); Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) ampl = 10.; } if (period < 2.e-7 || period > 2.e3) { Fmt(msg, "%s 2.e3) period = 2.e3; } if (width < 2.e-8 || width > 2.e3) { Fmt(msg, "%s 2.e3) width = 2.e3; } if (edge_time < 5.e-9 || edge_time > 1.e-7) { Fmt(msg, "%s 1.e-7) edge_time = 1.e-7; } if (period < width + 1.6 * edge_time) { Fmt(msg, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) ampl = 10.; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Set the amplitude */ hp33220_out(dev_ID, "VOLT:UNIT VPP"); Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 2.e7) { Fmt(msg, "%s 2.e7) freq = 2.e7; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Set the frequency */ Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s%f", &l); ampl = ampl / 2.; if (dc_offset - ampl < -10. || dc_offset + ampl > 10.) { Fmt(msg, "%s 10.) dc_offset = 10. - ampl; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Set the dc_offset */ Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 2.e3) { Fmt(msg, "%s 2.e3) period = 2.e3; } /* Exit if testing without hardware */ #ifdef DUMMY_DEVICES return; #endif /* Check to make sure the device is open */ dev_open = hp33220_check_dev_open(dev_ID); if (dev_open != 1) { hp33220_error("Device not open"); return; } /* Set the period */ Fmt(cmd, "%s HP33220_MAX_NUM_DEV) { Fmt(msg, "%s%f", ampl); /* Convert from Vpp to V0p */ *ampl = *ampl / 2.; /* Check for errors */ err = hp33220_get_errors(dev_ID); if (err != 0) { hp33220_error("The HP33220 has an error"); return; } /* Done */ return; } /* ************************************************************** */ /* * hp33220_get_freq * This function gets the frequency of the output. * * Input: * dev_ID, device identifier * * Output: * freq, frequency of the output voltage (Hz) * * Zachary Wolf * 11/9/04 */ void hp33220_get_freq(int dev_ID, double* freq) { /* Declare variables */ int dev_open; int err; /* Check input parameters */ if (dev_ID < 1 || dev_ID > HP33220_MAX_NUM_DEV) { Fmt(msg, "%s%f", freq); /* Check for errors */ err = hp33220_get_errors(dev_ID); if (err != 0) { hp33220_error("The HP33220 has an error"); return; } /* Done */ return; } /* ************************************************************** */ /* * hp33220_get_dc_offset * This function gets the DC offset of the output. * * Input: * dev_ID, device identifier * * Output: * offset, DC offset of the output voltage (V) * * Zachary Wolf * 11/9/04 */ void hp33220_get_dc_offset(int dev_ID, double* offset) { /* Declare variables */ int dev_open; int err; /* Check input parameters */ if (dev_ID < 1 || dev_ID > HP33220_MAX_NUM_DEV) { Fmt(msg, "%s%f", offset); /* Check for errors */ err = hp33220_get_errors(dev_ID); if (err != 0) { hp33220_error("The HP33220 has an error"); return; } /* Done */ return; } /* ************************************************************** */ /* * hp33220_exit * This function leaves the HP33220 in a desired state and * closes the device. * * Input: * dev_ID, device identifier * * Zachary Wolf * 7/27/98 */ void hp33220_exit(int dev_ID) { /* Declare variables */ int dev_open; /* Check input parameters */ if (dev_ID < 1 || dev_ID > HP33220_MAX_NUM_DEV) { Fmt(msg, "%s 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s%s[t-]", buf); /* Done */ return 0; } /* ************************************************************** */ /* * hp33220_serial_poll * This function performs a serial poll of the device. * * Input: * dev_ID, device identifier * * Output: * spoll, contents of the serial poll register * err, 0 if ok, -1 otherwise * * Zachary Wolf * 11/8/04 */ int hp33220_serial_poll(int dev_ID, int* spoll) { /* Declare variables */ int err; char spoll_char; /* Perform the serial poll */ err = ibrsp(dev_descr[dev_ID], &spoll_char); /* Check for errors */ if (err & 0x8000) { Fmt(msg, "%s%i", spoll); /* Done */ return 0; } /* ************************************************************** */ /* * hp33220_check_dev_open * This function checks to see if the specified device is open. * If the device has been opened, a 1 is returned, 0 otherwise. * * Input: * dev_ID, device identifier * * Output: * status, 1 if device is open, 0 otherwise * * Zachary Wolf * 7/27/98 */ int hp33220_check_dev_open(int dev_ID) { /* See if the board descriptor has a positive value */ if (dev_descr[dev_ID] > 0) { return 1; /* Open */ } else { return 0; /* Not open */ } } /* ************************************************************** */ /* * hp33220_get_errors * This function obtains device errors. * The operator is alerted if there is an error. * * Input: * dev_ID, device identifier * * Output: * err, 0 if no errors, -1 otherwise * * Zachary Wolf * 8/26/04 */ int hp33220_get_errors(int dev_ID) { /* Declare variables */ int status; int err; int disp_err; /* Initialization */ err = 0; disp_err = 0; /* Get the HP33220 status byte */ hp33220_out(dev_ID, "*STB?"); hp33220_in(dev_ID, cmd); /* Extract the status value */ Scan(cmd, "%s>%i", &status); /* Alert the operator if there is an error */ if ((status & 4) != 0) { Fmt(msg, "%s