From: hackbard Date: Thu, 18 Jan 2007 22:33:35 +0000 (+0000) Subject: host computer tool to read from rs232 and compute temperature X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04ffc37ce69bf3d8c175ddae51b0fda6d7848310;p=my-code%2Fatmel.git host computer tool to read from rs232 and compute temperature --- diff --git a/thermo/temperature.c b/thermo/temperature.c new file mode 100644 index 0000000..1e3dd9e --- /dev/null +++ b/thermo/temperature.c @@ -0,0 +1,118 @@ +/* + * get voltage (drop) -> resistance => estimate temperature + * + * author: hackbard@hackdaworld.org + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * 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 \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; +} +