]> hackdaworld.org Git - my-code/atmel.git/commitdiff
host computer tool to read from rs232 and compute temperature
authorhackbard <hackbard>
Thu, 18 Jan 2007 22:33:35 +0000 (22:33 +0000)
committerhackbard <hackbard>
Thu, 18 Jan 2007 22:33:35 +0000 (22:33 +0000)
thermo/temperature.c [new file with mode: 0644]

diff --git a/thermo/temperature.c b/thermo/temperature.c
new file mode 100644 (file)
index 0000000..1e3dd9e
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * get voltage (drop) -> resistance => estimate temperature
+ *
+ * author: hackbard@hackdaworld.org
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <math.h>
+
+/*
+ * user defineable values of used components, you want to edit this!
+ */
+
+#define COUNTER (256*256)
+#define PRESCALE 64
+#define FREQ 8000000
+#define CAP 0.0001
+#define RES_NEXT_CAP 10000
+#define U0 5.0
+#define RES_NTC 3550
+#define NTC_R_TO_T(x)  (-3e-5*(x)*(x)+0.2388*(x)-110.09)
+
+#define MAGIC_U 0.2
+
+int main(int argc,char **argv) {
+
+       int fd,ret;
+       unsigned short c;
+       unsigned int sum,count;
+       unsigned char hl[2];
+       double max,u_ntc,r_ntc;
+       struct termios term;
+
+       if(argc!=2) {
+               printf("usage: %s <serial device>\n",argv[0]);
+               return -1;
+       }
+
+       max=U0*(1.0-exp(-1.0*COUNTER*PRESCALE/FREQ/(RES_NEXT_CAP*CAP)));
+
+       memset(&term,0,sizeof(struct termios));
+
+       fd=open(argv[1],O_RDONLY);
+       if(fd<0) {
+               perror("open call");
+               return -1;
+       }
+
+       tcgetattr(fd,&term);
+
+       cfsetispeed(&term,B9600);
+       cfsetospeed(&term,B9600);
+       
+       term.c_cflag&=~PARENB;
+       term.c_cflag&=~CSTOPB;
+       term.c_cflag&=~CSIZE;
+       term.c_cflag|=CS8;
+
+       term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
+
+       term.c_iflag&=~(IXON|IXOFF|IXANY);
+
+       tcsetattr(fd,TCSANOW,&term);
+
+       count=0;
+       sum=0;
+
+       printf("\n");
+       printf("low cost adc using analog comparator!\n");
+       printf("-------------------------------------\n");
+       printf("\n");
+       printf("this piece of code:\n");
+       printf("  - is estimating the voltage drop V at the ntc\n");
+       printf("  - calculates the resistance R of the ntc\n");
+       printf("  - tells us the corresponding temperature T\n");
+       printf("\n");
+       printf("here we go ....\n");
+       printf("\n");
+
+       while(1) {
+
+               ret=read(fd,hl,2);
+               if(ret!=2) {
+                       perror("read 2");
+                       return -1;
+               }
+
+               c=(hl[0]<<8)|hl[1];
+               count+=1;
+               sum+=c;
+
+               u_ntc=MAGIC_U+c*max/COUNTER;
+               r_ntc=u_ntc/(U0-u_ntc)*RES_NTC;
+
+               printf("\rV: %f | %f [%f]  #  R: %f  #  T: %f",
+                      c*max/COUNTER,sum*max/(count*COUNTER),u_ntc,
+                      r_ntc,NTC_R_TO_T(r_ntc));
+               fflush(stdout);
+
+               if(count==5) {
+                       sum/=count;
+                       count=1;
+               }
+       }
+
+       close(fd);
+
+       return 0;
+}
+