home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 September / CHIP_CD_1997_09_PL.iso / software / testsoft / labwind / demo.6 / main / instr / scope.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-02  |  4.8 KB  |  206 lines

  1. #include <formatio.h>
  2. #include <ansi_c.h>
  3.  
  4. /*  == Sample Oscilloscope Instrument Module =============================== */
  5. #include "scope.h"
  6.  
  7. int scope_device_closed (void);
  8. int scope_invalid_integer_range (int, int, int, int);
  9. int scope_invalid_real_range (double, double, double, int);
  10. int scope_write_data (char *, int);
  11.  
  12. int scope_err = 0;
  13.  
  14. /* ========================================================================= */
  15. static int bd;
  16. static char cmd[100];
  17. static double ch1_volts_per_div;
  18. static double ch2_volts_per_div;
  19. static double sec_per_div;
  20. static int ch1_coupling;
  21. static int ch2_coupling;
  22.  
  23. /* ========================================================================= */
  24. int CVIFUNC scope_init (int addr)
  25. {
  26.  
  27.   if (scope_invalid_integer_range (addr, 0, 30,  -1) != 0)
  28.     return scope_err;
  29.   if (addr != 1)  {
  30.     scope_err = 223;
  31.     return scope_err;
  32.   }
  33.   bd = 1;
  34.   ch1_volts_per_div = 1.0;
  35.   ch2_volts_per_div = 1.0;
  36.   ch1_coupling = 1;
  37.   ch2_coupling = 1;
  38.   sec_per_div = 0.001;
  39.   scope_err = 0;
  40.   return scope_err;
  41. }
  42.  
  43. /* ========================================================================= */
  44. int CVIFUNC scope_close (void)
  45. {
  46.  
  47.   if (scope_device_closed () != 0)
  48.     {
  49.         scope_err = 223;
  50.         return scope_err;
  51.     }
  52.   bd = 0;
  53.   scope_err = 0;
  54.   return scope_err;
  55. }
  56.  
  57. /* ========================================================================= */
  58. int CVIFUNC scope_config (int chan, double v_div, int coupling, double sec_div)
  59. {
  60.   char *cou_name;
  61.  
  62.   if (scope_device_closed () != 0)
  63.     return scope_err;
  64.   if (scope_invalid_integer_range (chan, 1, 2,  -1) != 0)
  65.     return scope_err;
  66.   if (scope_invalid_real_range (v_div, 0.01, 50.0,  -2) != 0)
  67.     return scope_err;
  68.   if (scope_invalid_integer_range (coupling, 0, 2,  -3) != 0)
  69.     return scope_err;
  70.   if (scope_invalid_real_range (sec_div, 0.001, 1.0,  -4) != 0)
  71.     return scope_err;
  72.   if (chan == 1)  {
  73.     ch1_volts_per_div = v_div;
  74.     ch1_coupling = coupling;
  75.   }
  76.   else  {
  77.     ch2_volts_per_div = v_div;
  78.     ch2_coupling = coupling;
  79.   }
  80.   sec_per_div = sec_div;
  81.   switch (coupling)  {
  82.   case 0:
  83.     cou_name = "AC";
  84.     break;
  85.   case 1:
  86.     cou_name = "DC";
  87.     break;
  88.   case 2:
  89.     cou_name = "GND";
  90.     break;
  91.   default:
  92.     return scope_err;
  93.   }
  94.   Fmt (cmd, "CH%d VOL:%f,COU:%s;HOR ASE:%f;", chan, v_div, cou_name, sec_div);
  95.   if (scope_write_data (cmd, NumFmtdBytes ()) != 0)
  96.     return  scope_err;
  97.   scope_err = 0;
  98.   return  scope_err;
  99. }
  100.  
  101. /* ========================================================================= */
  102. int CVIFUNC scope_read_waveform (int sou, double wvfm[100], double *xin, double *xzero)
  103. {
  104.   int i;
  105.   int coupling;
  106.   double offset;
  107.   double mult;
  108.  
  109.   *xzero = 0.0;
  110.   coupling = 0;
  111.   mult = 0.0;
  112.   offset = 0.0;
  113.   if (scope_device_closed () != 0)
  114.     return scope_err;
  115.   if (scope_invalid_integer_range (sou, 1, 2,  -1) != 0)
  116.     return scope_err;
  117.   if (sou == 1)
  118.     coupling = ch1_coupling;
  119.   else
  120.     coupling = ch2_coupling;
  121.   if (coupling != 2)  {
  122.     if (sou == 1)
  123.       mult = 0.5/ch1_volts_per_div;
  124.     else
  125.       mult = 0.05/ch2_volts_per_div;
  126.     if (coupling == 1)
  127.       offset = 1;
  128.   }
  129.   Fmt (cmd, "DAT SOU:CH%d;CURV?", sou);
  130.   if (scope_write_data (cmd, NumFmtdBytes ()) != 0)
  131.     return scope_err;
  132.   if (sou == 1) {
  133.       i = 0;
  134.       while (i < 100)  {
  135.         wvfm[i] = mult * sin (2.0 * 3.14159 * (sec_per_div / 0.1) * (double)i) + (offset - 1);
  136.         i++;
  137.        }
  138.      }
  139.   else if (sou == 2) {
  140.     i = 0;
  141.     while (i < 100) {
  142.         wvfm[i] = mult * (((rand()/32767.0) * 2.0)- 1.0);
  143.         i++;
  144.         }
  145.   }
  146.   *xin = sec_per_div / 100.0;
  147.   *xzero = 0.0;
  148.   scope_err = 0;
  149.   return scope_err;
  150. }
  151.  
  152. /* ========================================================================= */
  153. int scope_device_closed (void)
  154. {
  155.  
  156.   if (bd <= 0)
  157.     scope_err = 232;
  158.   else
  159.     scope_err = 0;
  160.   return scope_err;
  161. }
  162.  
  163. /* ========================================================================= */
  164. int scope_invalid_integer_range (val, min, max, err_code)
  165. int val;
  166. int min;
  167. int max;
  168. int err_code;
  169. {
  170.  
  171.   if (val < min || val > max)  {
  172.     scope_err = err_code;
  173.     return 1;
  174.   }
  175.   return 0;
  176. }
  177.  
  178. /* ========================================================================= */
  179. int scope_invalid_real_range (val, min, max, err_code)
  180. double val;
  181. double min;
  182. double max;
  183. int err_code;
  184. {
  185.  
  186.   if (val < min || val > max)  {
  187.     scope_err = err_code;
  188.     return 1;
  189.   }
  190.   return 0;
  191. }
  192.  
  193. /* ========================================================================= */
  194. int scope_write_data (cmd, cnt)
  195. char *cmd;
  196. int cnt;
  197. {
  198.  
  199.   /*       if (ibwrt (bd, cmd, cnt) <= 0)   */
  200.   /*           scope_err = 230;             */
  201.   /*       else                             */
  202.   scope_err = 0;
  203.   return scope_err;
  204. }
  205.  
  206.