new prescaler + debug outs
[my-code/atmel.git] / thermo / temperature.c
1 /*
2  * get voltage (drop) -> resistance => estimate temperature
3  *
4  * author: hackbard@hackdaworld.org
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <termios.h>
14 #include <sys/ioctl.h>
15 #include <errno.h>
16 #include <math.h>
17
18 /*
19  * user defineable values of used components, you want to edit this!
20  */
21
22 #define COUNTER (256*256)
23 #define PRESCALE 64
24 #define FREQ 8000000
25 #define CAP 0.0001
26 #define RES_NEXT_CAP 10000
27 #define U0 5.0
28 #define RES_NTC 3550
29 #define NTC_R_TO_T(x)  (-3e-5*(x)*(x)+0.2388*(x)-110.09)
30
31 #define MAGIC_U 0.2
32
33 int main(int argc,char **argv) {
34
35         int fd,ret;
36         unsigned short c;
37         unsigned int sum,count;
38         unsigned char hl[2];
39         double max,u_ntc,r_ntc;
40         struct termios term;
41
42         if(argc!=2) {
43                 printf("usage: %s <serial device>\n",argv[0]);
44                 return -1;
45         }
46
47         max=U0*(1.0-exp(-1.0*COUNTER*PRESCALE/FREQ/(RES_NEXT_CAP*CAP)));
48
49         memset(&term,0,sizeof(struct termios));
50
51         fd=open(argv[1],O_RDONLY);
52         if(fd<0) {
53                 perror("open call");
54                 return -1;
55         }
56
57         tcgetattr(fd,&term);
58
59         cfsetispeed(&term,B9600);
60         cfsetospeed(&term,B9600);
61         
62         term.c_cflag&=~PARENB;
63         term.c_cflag&=~CSTOPB;
64         term.c_cflag&=~CSIZE;
65         term.c_cflag|=CS8;
66
67         term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
68
69         term.c_iflag&=~(IXON|IXOFF|IXANY);
70
71         tcsetattr(fd,TCSANOW,&term);
72
73         count=0;
74         sum=0;
75
76         printf("\n");
77         printf("low cost adc using analog comparator!\n");
78         printf("-------------------------------------\n");
79         printf("\n");
80         printf("this piece of code:\n");
81         printf("  - is estimating the voltage drop V at the ntc\n");
82         printf("  - calculates the resistance R of the ntc\n");
83         printf("  - tells us the corresponding temperature T\n");
84         printf("\n");
85         printf("here we go ....\n");
86         printf("\n");
87
88         while(1) {
89
90                 ret=read(fd,hl,2);
91                 if(ret!=2) {
92                         perror("read 2");
93                         return -1;
94                 }
95
96                 c=(hl[0]<<8)|hl[1];
97                 count+=1;
98                 sum+=c;
99
100                 u_ntc=MAGIC_U+c*max/COUNTER;
101                 r_ntc=u_ntc/(U0-u_ntc)*RES_NTC;
102
103                 printf("\rV: %f | %f [%f]  #  R: %f  #  T: %f",
104                        c*max/COUNTER,sum*max/(count*COUNTER),u_ntc,
105                        r_ntc,NTC_R_TO_T(r_ntc));
106                 fflush(stdout);
107
108                 if(count==5) {
109                         sum/=count;
110                         count=1;
111                 }
112         }
113
114         close(fd);
115
116         return 0;
117 }
118