Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Tested with pelles c
- #include <stdio.h>
- #include <windows.h>
- #include <process.h>
- typedef void(CALLBACK* t_func)(int );
- typedef void(CALLBACK* t_func0)();
- typedef int(CALLBACK* t_func1)();
- typedef void(CALLBACK* t_func2)(int *, int *);
- typedef void(CALLBACK* t_func3)(int , int );
- typedef int(CALLBACK* t_func4)(int );
- typedef boolean(CALLBACK* t_func5)(int );
- t_func4 OpenDevice;
- t_func0 CloseDevice;
- t_func0 Version_;
- t_func4 ReadAnalogChannel;
- t_func2 ReadAllAnalog;
- t_func3 OutputAnalogChannel;
- t_func3 OutputAllAnalog;
- t_func ClearAnalogChannel;
- t_func0 ClearAllAnalog;
- t_func SetAnalogChannel;
- t_func0 SetAllAnalog;
- t_func WriteAllDigital;
- t_func ClearDigitalChannel;
- t_func0 ClearAllDigital;
- t_func SetDigitalChannel;
- t_func0 SetAllDigital;
- t_func5 ReadDigitalChannel;
- t_func1 ReadAllDigital;
- int init(void);
- float GetPt1000Temperature(float);
- int analog_cnt = -1;
- // change buffer size here when needed
- #define buffer_size 64
- float analog_buffer[buffer_size] = {0};
- void __cdecl analog_reader (void* d){
- int a1, a2;
- while(1){
- analog_cnt++;
- if(analog_cnt == buffer_size) analog_cnt = 0;
- ReadAllAnalog(&a1, &a2);
- analog_buffer[analog_cnt] = (a1 + a2) / 2;
- // maybe add a call to Sleep here...
- }
- }
- float get_analog_value(void){
- float rval = 0;
- for(int i = 0; i < buffer_size; i++) rval += analog_buffer[i];
- return rval / buffer_size;
- }
- int main(int argc, char *argv[])
- {
- if(init() != 0) return -1;
- if(OpenDevice(0) != 0) return -2;
- // Start Worker
- _beginthread(&analog_reader,0,0);
- // Make sure the analog_buffer is filled bevor using
- Sleep(2000);
- // set v_in in Volt and r1 in Ohm
- float v_in = 5;
- float r1 = 470;
- while(1){
- // Calculate Vout
- float v_out = v_in / 255 * get_analog_value();
- // Calculate R2
- float r2 = r1 / ((v_in/v_out) -1) ;
- // Translate R2 to Temperature [°C]
- float t = GetPt1000Temperature(r2);
- // Print temperature, rounded to 1 digit after the decimal point
- printf("%.1f\n",t);
- Sleep(1000);
- }
- }
- int init()
- {
- HINSTANCE hDLL = LoadLibrary("k8055d");
- if (hDLL != NULL)
- {
- OpenDevice = (t_func4) GetProcAddress(hDLL, "OpenDevice");
- if (!OpenDevice)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- CloseDevice = (t_func0) GetProcAddress(hDLL, "CloseDevice");
- if (!CloseDevice)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ReadAnalogChannel = (t_func4) GetProcAddress(hDLL, "ReadAnalogChannel");
- if (!ReadAnalogChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ReadAllAnalog = (t_func2) GetProcAddress(hDLL, "ReadAllAnalog");
- if (!ReadAllAnalog)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- OutputAnalogChannel = (t_func3) GetProcAddress(hDLL, "OutputAnalogChannel");
- if (!OutputAnalogChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- OutputAllAnalog = (t_func3) GetProcAddress(hDLL, "OutputAllAnalog");
- if (!OutputAllAnalog)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ClearAnalogChannel = (t_func) GetProcAddress(hDLL, "ClearAnalogChannel");
- if (!ClearAnalogChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ClearAllAnalog = (t_func0) GetProcAddress(hDLL, "ClearAllAnalog");
- if (!ClearAllAnalog)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- SetAnalogChannel = (t_func) GetProcAddress(hDLL, "SetAnalogChannel");
- if (!SetAnalogChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- SetAllAnalog = (t_func0) GetProcAddress(hDLL, "SetAllAnalog");
- if (!SetAllAnalog)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- WriteAllDigital = (t_func) GetProcAddress(hDLL, "WriteAllDigital");
- if (!WriteAllDigital)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ClearDigitalChannel = (t_func) GetProcAddress(hDLL, "ClearDigitalChannel");
- if (!ClearDigitalChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ClearAllDigital = (t_func0) GetProcAddress(hDLL, "ClearAllDigital");
- if (!ClearAllDigital)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- SetDigitalChannel = (t_func) GetProcAddress(hDLL, "SetDigitalChannel");
- if (!SetDigitalChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- SetAllDigital = (t_func0) GetProcAddress(hDLL, "SetAllDigital");
- if (!SetAllDigital)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ReadDigitalChannel = (t_func5) GetProcAddress(hDLL, "ReadDigitalChannel");
- if (!ReadDigitalChannel)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- ReadAllDigital = (t_func1) GetProcAddress(hDLL, "ReadAllDigital");
- if (!ReadAllDigital)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- Version_ = (t_func0) GetProcAddress(hDLL, "Version");
- if (!Version_)
- { // handle the error
- FreeLibrary(hDLL);
- return -2;
- }
- return 0; // ok
- }
- return -1; // error load DLL
- }
- // from here: http://en.wikipedia.org/wiki/Resistance_thermometer
- float GetPt100Temperature(float r)
- {
- float const Pt100[] = { 80.31, 82.29, 84.27, 86.25, 88.22, 90.19, 92.16, 94.12, 96.09, 98.04,
- 100.0, 101.95, 103.9, 105.85, 107.79, 109.73, 111.67, 113.61, 115.54, 117.47,
- 119.4, 121.32, 123.24, 125.16, 127.07, 128.98, 130.89, 132.8, 134.7, 136.6,
- 138.5, 140.39, 142.29, 157.31, 175.84, 195.84};
- int t = -50, i, dt = 0;
- if (r > Pt100[i = 0])
- while (250 > t) {
- dt = (t < 110) ? 5 : (t > 110) ? 50 : 40;
- if (r < Pt100[++i])
- return t + (r - Pt100[i-1]) * dt / (Pt100[i] - Pt100[i-1]);
- t += dt;
- };
- return t;
- }
- float GetPt1000Temperature(float r)
- {
- return GetPt100Temperature(r / 10);
- }
Advertisement
Add Comment
Please, Sign In to add comment