Guest User

K8055 & PT1000

a guest
Nov 29th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.05 KB | None | 0 0
  1. // Tested with pelles c
  2.  
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <process.h>
  6.  
  7. typedef void(CALLBACK* t_func)(int );
  8. typedef void(CALLBACK* t_func0)();
  9. typedef int(CALLBACK* t_func1)();
  10. typedef void(CALLBACK* t_func2)(int *, int *);
  11. typedef void(CALLBACK* t_func3)(int , int );
  12. typedef int(CALLBACK* t_func4)(int );
  13. typedef boolean(CALLBACK* t_func5)(int );
  14.  
  15. t_func4 OpenDevice;
  16. t_func0 CloseDevice;
  17. t_func0 Version_;
  18. t_func4 ReadAnalogChannel;
  19. t_func2 ReadAllAnalog;
  20. t_func3 OutputAnalogChannel;
  21. t_func3 OutputAllAnalog;
  22. t_func ClearAnalogChannel;
  23. t_func0 ClearAllAnalog;
  24. t_func SetAnalogChannel;
  25. t_func0 SetAllAnalog;
  26. t_func WriteAllDigital;
  27. t_func ClearDigitalChannel;
  28. t_func0 ClearAllDigital;
  29. t_func SetDigitalChannel;
  30. t_func0 SetAllDigital;
  31. t_func5 ReadDigitalChannel;
  32. t_func1 ReadAllDigital;
  33. int init(void);
  34. float GetPt1000Temperature(float);
  35. int analog_cnt = -1;
  36.  
  37. // change buffer size here when needed
  38. #define buffer_size 64
  39. float analog_buffer[buffer_size] = {0};
  40.  
  41.  
  42. void __cdecl analog_reader (void* d){
  43.     int a1, a2;
  44.     while(1){
  45.         analog_cnt++;
  46.         if(analog_cnt == buffer_size) analog_cnt = 0;
  47.         ReadAllAnalog(&a1, &a2);
  48.         analog_buffer[analog_cnt] = (a1 + a2) / 2;
  49.         // maybe add a call to Sleep here...
  50.     }
  51. }
  52.  
  53. float get_analog_value(void){
  54.     float rval = 0;
  55.     for(int i = 0; i < buffer_size; i++) rval += analog_buffer[i];
  56.     return rval / buffer_size;
  57. }
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61.     if(init() != 0) return -1;
  62.     if(OpenDevice(0) != 0) return -2;
  63.    
  64.     // Start Worker
  65.     _beginthread(&analog_reader,0,0);
  66.  
  67.     // Make sure the analog_buffer is filled bevor using
  68.     Sleep(2000);
  69.    
  70.     // set v_in in Volt and r1 in Ohm
  71.     float v_in = 5;
  72.     float r1 = 470;    
  73.  
  74.     while(1){
  75.        
  76.         // Calculate Vout
  77.         float v_out = v_in / 255 * get_analog_value();
  78.         // Calculate R2
  79.         float r2 = r1 / ((v_in/v_out) -1) ;
  80.         // Translate R2 to Temperature [°C]
  81.         float t = GetPt1000Temperature(r2);
  82.        
  83.         // Print temperature, rounded to 1 digit after the decimal point
  84.         printf("%.1f\n",t);
  85.         Sleep(1000);
  86.     }
  87.  
  88. }
  89.  
  90. int init()
  91. {
  92.     HINSTANCE hDLL = LoadLibrary("k8055d");
  93.     if (hDLL != NULL)
  94.     {
  95.         OpenDevice = (t_func4) GetProcAddress(hDLL, "OpenDevice");
  96.         if (!OpenDevice)       
  97.         {                       // handle the error
  98.             FreeLibrary(hDLL); 
  99.             return -2;
  100.         }
  101.         CloseDevice = (t_func0) GetProcAddress(hDLL, "CloseDevice");
  102.         if (!CloseDevice)
  103.         {                       // handle the error
  104.             FreeLibrary(hDLL); 
  105.             return -2;
  106.         }
  107.         ReadAnalogChannel = (t_func4) GetProcAddress(hDLL, "ReadAnalogChannel");
  108.         if (!ReadAnalogChannel)
  109.         {                       // handle the error
  110.             FreeLibrary(hDLL); 
  111.             return -2;
  112.         }
  113.         ReadAllAnalog = (t_func2) GetProcAddress(hDLL, "ReadAllAnalog");
  114.         if (!ReadAllAnalog)
  115.         {                       // handle the error
  116.             FreeLibrary(hDLL); 
  117.             return -2;
  118.         }
  119.         OutputAnalogChannel = (t_func3) GetProcAddress(hDLL, "OutputAnalogChannel");
  120.         if (!OutputAnalogChannel)
  121.         {                       // handle the error
  122.             FreeLibrary(hDLL); 
  123.             return -2;
  124.         }
  125.         OutputAllAnalog = (t_func3) GetProcAddress(hDLL, "OutputAllAnalog");
  126.         if (!OutputAllAnalog)      
  127.         {                       // handle the error
  128.             FreeLibrary(hDLL); 
  129.             return -2;
  130.         }
  131.         ClearAnalogChannel = (t_func) GetProcAddress(hDLL, "ClearAnalogChannel");
  132.         if (!ClearAnalogChannel)
  133.         {                       // handle the error
  134.             FreeLibrary(hDLL); 
  135.             return -2;
  136.         }
  137.         ClearAllAnalog = (t_func0) GetProcAddress(hDLL, "ClearAllAnalog");
  138.         if (!ClearAllAnalog)
  139.         {                       // handle the error
  140.             FreeLibrary(hDLL); 
  141.             return -2;
  142.         }
  143.         SetAnalogChannel = (t_func) GetProcAddress(hDLL, "SetAnalogChannel");
  144.         if (!SetAnalogChannel)
  145.         {                       // handle the error
  146.             FreeLibrary(hDLL); 
  147.             return -2;
  148.         }
  149.         SetAllAnalog = (t_func0) GetProcAddress(hDLL, "SetAllAnalog");
  150.         if (!SetAllAnalog)
  151.         {                       // handle the error
  152.             FreeLibrary(hDLL); 
  153.             return -2;
  154.         }
  155.         WriteAllDigital = (t_func) GetProcAddress(hDLL, "WriteAllDigital");
  156.         if (!WriteAllDigital)
  157.         {                       // handle the error
  158.             FreeLibrary(hDLL); 
  159.             return -2;
  160.         }
  161.         ClearDigitalChannel = (t_func) GetProcAddress(hDLL, "ClearDigitalChannel");
  162.         if (!ClearDigitalChannel)
  163.         {                       // handle the error
  164.             FreeLibrary(hDLL); 
  165.             return -2;
  166.         }
  167.         ClearAllDigital = (t_func0) GetProcAddress(hDLL, "ClearAllDigital");
  168.         if (!ClearAllDigital)      
  169.         {                       // handle the error
  170.             FreeLibrary(hDLL); 
  171.             return -2;
  172.         }
  173.         SetDigitalChannel = (t_func) GetProcAddress(hDLL, "SetDigitalChannel");
  174.         if (!SetDigitalChannel)
  175.         {                       // handle the error
  176.             FreeLibrary(hDLL); 
  177.             return -2;
  178.         }
  179.         SetAllDigital = (t_func0) GetProcAddress(hDLL, "SetAllDigital");
  180.         if (!SetAllDigital)    
  181.         {                       // handle the error
  182.             FreeLibrary(hDLL); 
  183.             return -2;
  184.         }
  185.         ReadDigitalChannel = (t_func5) GetProcAddress(hDLL, "ReadDigitalChannel");
  186.         if (!ReadDigitalChannel)
  187.         {                       // handle the error
  188.             FreeLibrary(hDLL); 
  189.             return -2;
  190.         }
  191.         ReadAllDigital = (t_func1) GetProcAddress(hDLL, "ReadAllDigital");
  192.         if (!ReadAllDigital)
  193.         {                       // handle the error
  194.             FreeLibrary(hDLL); 
  195.             return -2;
  196.         }
  197.         Version_ = (t_func0) GetProcAddress(hDLL, "Version");
  198.         if (!Version_)
  199.         {                       // handle the error
  200.             FreeLibrary(hDLL); 
  201.             return -2;
  202.         }
  203.         return 0;               // ok
  204.     }      
  205.     return -1;                  // error load DLL
  206. }
  207.  
  208. // from here: http://en.wikipedia.org/wiki/Resistance_thermometer
  209.  
  210. float GetPt100Temperature(float r)
  211. {
  212.     float const Pt100[] = {  80.31,   82.29,  84.27,  86.25,  88.22,  90.19,  92.16,  94.12,  96.09,  98.04,
  213.                                 100.0,    101.95, 103.9,  105.85, 107.79, 109.73, 111.67, 113.61, 115.54, 117.47,
  214.                                 119.4,  121.32, 123.24, 125.16, 127.07, 128.98, 130.89, 132.8,  134.7,  136.6,
  215.                                 138.5,  140.39, 142.29, 157.31, 175.84, 195.84};
  216.     int t = -50, i, dt = 0;
  217.     if (r > Pt100[i = 0])
  218.       while (250 > t) {
  219.         dt = (t < 110) ? 5 : (t > 110) ? 50 : 40;
  220.         if (r < Pt100[++i])
  221.           return t + (r - Pt100[i-1]) * dt / (Pt100[i] - Pt100[i-1]);
  222.         t += dt;
  223.       };
  224.  
  225.     return t;
  226. }
  227.  
  228. float GetPt1000Temperature(float r)
  229. {
  230.     return GetPt100Temperature(r / 10);
  231. }
  232.  
  233.  
Advertisement
Add Comment
Please, Sign In to add comment