Facebook
From Lousy Peafowl, 5 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 268
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "C:/Advantech/DAQNavi/Examples/C++_Console/inc/compatibility.h"
  5. #include "C:/Advantech/DAQNavi/Inc/bdaqctrl.h"
  6. #include <math.h>
  7. #include <Windows.h>
  8.  
  9. using namespace Automation::BDaq;
  10. //-----------------------------------------------------------------------------------
  11. // Configure the following three parameters before running the example
  12. //-----------------------------------------------------------------------------------
  13. #define deviceDescription L"PCI-1710HG,BID#0"
  14. #define PI 3.141592
  15. #define N 50
  16. int32 startChannel = 0;
  17. const int32 channelCount = 1;
  18.  
  19. inline void waitAnyKey()
  20. {
  21.         do { SLEEP(1); } while (!kbhit());
  22. }
  23. void InstantAO();
  24. void nanoSleep()
  25. {
  26.        
  27.  
  28. }
  29. int main(int argc, char* argv[])
  30. {
  31.        
  32.         nanoSleep();
  33.         InstantAO();
  34.         return 0;
  35. }
  36.  
  37.  
  38.  
  39. void InstantAO()
  40. {
  41.         ErrorCode ret;
  42.  
  43.         // Step 1: create 'InstantAoCtrl' object
  44.         InstantAoCtrl * ctrl = AdxInstantAoCtrlCreate();
  45.  
  46.         do
  47.         {
  48.                 // Step 2: select the device with 'ModeWriteWithReset' mode
  49.                 int deviceNumber = 0;
  50.                 DeviceInformation devInfo(deviceNumber);
  51.                 ret = ctrl->setSelectedDevice(devInfo);
  52.                 if (ret != Success) {
  53.                         printf("open device failed. error code = %x\n", ret);
  54.                         break;
  55.                 }
  56.  
  57.                 // Step 3:  *** configure the channels if needed ***
  58.                 AoChannelCollection *channels = ctrl->getChannels();
  59.                 for (int i = 0; i < channels->getCount(); ++i) {
  60.                         channels->getItem(i).setValueRange(V_0To5);
  61.                 }
  62.  
  63.                 // Step 4: scan the channels
  64.                 int32 const chStart = 0;
  65.                 int32 const chCount = 2;
  66.  
  67.                 // generate waveform data
  68.                 double values[chCount];
  69.                 double valuess[chCount];
  70.                 char c;
  71.                 double Am = 1;
  72.                 double v[N];
  73.                 double T = 0.00001; //1s
  74.                 double delta = T / N;
  75.                
  76.                 double faza=0;
  77.                 printf("Podaj faze: ");
  78.                 scanf("%lf", &faza);
  79.  
  80.                 double volt;
  81.                 int i = 0;
  82.                 LARGE_INTEGER start, end, freq;
  83.                 QueryPerformanceCounter(&start);
  84.                 QueryPerformanceFrequency(&freq);
  85.                 printf("%ld", freq.QuadPart);
  86.  
  87.                 while (1)
  88.                 {
  89.                         if( kbhit()) {
  90.                                 c = getchar();
  91.                                 switch (c) {
  92.  
  93.                                 case 'w': Am += 0.1; break;
  94.  
  95.                                 case 's':Am += -0.1; break;
  96.                                        
  97.  
  98.  
  99.                                 case 'd': {T += 50;
  100.                                        
  101.                                         break; }
  102.  
  103.  
  104.                                 case 'a': {T += -50;
  105.                                        
  106.                                         break; }
  107.                                 case 'e': exit(0) ;
  108.  
  109.                                 case 'f': {faza += 0.1;
  110.                                         break; }
  111.                                 case 'v': {faza += -0.1;
  112.                                        
  113.                                         break; }
  114.                                 }
  115.                         }
  116.                        
  117.                         values[0] = Am*sin(i *  2*PI/N) +2.5;
  118.                         ctrl->Write(chStart, 1, values);
  119.  
  120.                         valuess[0] = Am*sin(i * 2 * PI / N +faza) + 2.5;
  121.                         ctrl->Write(chStart+1, 1, valuess);
  122.  
  123.                         do{
  124.  
  125.                                 QueryPerformanceCounter(&end);
  126.  
  127.                         } while (end.QuadPart < (start.QuadPart + 290));
  128.                         start.QuadPart += 290;
  129.                                
  130.  
  131.                         i++;
  132.                         i %= N;
  133.  
  134.                 }
  135.  
  136.                 //ctrl->Write(chStart, chCount, values);
  137.         } while (false);
  138.  
  139.         // Step 5: close device and release any allocated resource.
  140.         ctrl->Cleanup();
  141.  
  142.         // Step 6: destroy the object
  143.         ctrl->Dispose();
  144. }