2 * get voltage (drop) -> resistance => estimate temperature
4 * author: hackbard@hackdaworld.org
10 #include <sys/types.h>
14 #include <sys/ioctl.h>
19 * user defineable values of used components, you want to edit this!
22 #define COUNTER (256*256)
26 #define RES_NEXT_CAP 10000
29 #define NTC_R_TO_T(x) (-3e-5*(x)*(x)+0.2388*(x)-110.09)
33 int main(int argc,char **argv) {
37 unsigned int sum,count;
39 double max,u_ntc,r_ntc;
43 printf("usage: %s <serial device>\n",argv[0]);
47 max=U0*(1.0-exp(-1.0*COUNTER*PRESCALE/FREQ/(RES_NEXT_CAP*CAP)));
49 memset(&term,0,sizeof(struct termios));
51 fd=open(argv[1],O_RDONLY);
59 cfsetispeed(&term,B9600);
60 cfsetospeed(&term,B9600);
62 term.c_cflag&=~PARENB;
63 term.c_cflag&=~CSTOPB;
67 term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
69 term.c_iflag&=~(IXON|IXOFF|IXANY);
71 tcsetattr(fd,TCSANOW,&term);
77 printf("low cost adc using analog comparator!\n");
78 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");
85 printf("here we go ....\n");
100 u_ntc=MAGIC_U+c*max/COUNTER;
101 r_ntc=u_ntc/(U0-u_ntc)*RES_NTC;
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));