1 /* rt.c for ad612 */ 2 3 #include4 #include 5 #define PORT 0x378 6 7 static __inline unsigned char inb(unsigned int port); 8 static __inline void outb(unsigned int port, unsigned char data); 9 10 unsigned int rtnval; 11 clock_t start; 12 void main(void) 13 { 14 int fd, i; 15 FILE *fp; 16 float temp; 17 18 19 long t; 20 struct tm *tmbuf; 21 22 if((fd = open("/dev/io",2)) < 0) 23 error("can't open /dev/io", (char *)0); 24 25 if((fp = fopen("/nis/inoue/public_html/RT/thermo.dat", "a+")) < 0) 26 error("can't open thermo.dat", (char *)0); 27 28 t = time(0); 29 tmbuf = localtime(&t); 30 31 temp=0.08*(float)ReadChan(PORT, 0)-214.8; 32 33 fprintf(fp, "%2.2f, %2.3f \n", (float)(tmbuf->tm_hour)+(float)(tmbuf->tm_min)/60.0, temp); 34 start = clock(); 35 do{ 36 }while(clock() - start < 100); 37 fclose(fp); 38 close(fd); 39 } 40 41 error(s, t) char *s, *t; 42 { 43 fprintf(stderr, s, t); 44 fprintf(stderr, "\n"); 45 exit(1); 46 } 47 48 /* basic i/o ---------------------------------------------------------------*/ 49 50 static __inline unsigned char 51 inb(unsigned int port) 52 { 53 unsigned char data; 54 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 55 return(data); 56 } 57 58 static __inline void 59 outb(unsigned int port, unsigned char data) 60 { 61 unsigned char al; 62 al = data; 63 __asm __volatile("outb %0, %%dx" : : "a" (al), "d" (port)); 64 } 65 /*-------------------------------------------------------------------------*/ 66 67 /* ReadChan returns an int 0-4096 proportional to the voltage at the input */ 68 /* addrPort is the port address either 0x278, 0x378, or 0x3bc--------------*/ 69 /* nChan is the input channel number 0-5, 0 s connected to the RT sensor---*/ 70 71 int ReadChan(unsigned int addrPort, int nChan) 72 { 73 int i, result; 74 unsigned char filter; 75 long filt_result; 76 unsigned char count; 77 78 filter = 20; /* number of samples to average */ 79 filt_result = 0; 80 81 for(count = 0; count < filter; count++) 82 { 83 /*--------------------------------------------------------------------*/ 84 /* Clk CS Din */ 85 outb(addrPort, 0xf8); /* 0 0 0 */ 86 outb(addrPort, 0xfc); /* 1 0 0 */ 87 outb(addrPort, 0xf9); /* 0 0 1 */ 88 outb(addrPort, 0xfd); /* 1 0 1 start */ 89 outb(addrPort, 0xf9); /* 0 0 1 */ 90 outb(addrPort, 0xfd); /* 1 0 1 sgl/diff */ 91 /*--------------------------------------------------------------------*/ 92 93 switch(nChan) 94 { 95 case 0: 96 for(i = 1; i < 4; i++) 97 { 98 outb(addrPort, 0xf8); /* 0 0 0 */ 99 outb(addrPort, 0xfc); /* 1 0 0 30s */ 100 } 101 break; 102 103 case 1: 104 outb(addrPort, 0xf9); /* 0 0 1 */ 105 outb(addrPort, 0xfd); /* 1 0 1 */ 106 outb(addrPort, 0xf8); /* 0 0 0 */ 107 outb(addrPort, 0xfc); /* 1 0 0 */ 108 outb(addrPort, 0xf8); /* 0 0 0 */ 109 outb(addrPort, 0xfc); /* 1 0 0 */ 110 break; 111 112 case 2: 113 outb(addrPort, 0xf8); /* 0 0 0 */ 114 outb(addrPort, 0xfc); /* 1 0 0 */ 115 outb(addrPort, 0xf8); /* 0 0 0 */ 116 outb(addrPort, 0xfc); /* 1 0 0 */ 117 outb(addrPort, 0xf9); /* 0 0 1 */ 118 outb(addrPort, 0xfd); /* 1 0 1 */ 119 break; 120 121 case 3: 122 outb(addrPort, 0xf9); /* 0 0 1 */ 123 outb(addrPort, 0xfd); /* 1 0 1 */ 124 outb(addrPort, 0xf8); /* 0 0 0 */ 125 outb(addrPort, 0xfc); /* 1 0 0 */ 126 outb(addrPort, 0xf9); /* 0 0 1 */ 127 outb(addrPort, 0xfd); /* 1 0 1 */ 128 break; 129 130 case 4: 131 outb(addrPort, 0xf8); /* 0 0 0 */ 132 outb(addrPort, 0xfc); /* 1 0 0 */ 133 outb(addrPort, 0xf9); /* 0 0 1 */ 134 outb(addrPort, 0xfd); /* 1 0 1 */ 135 outb(addrPort, 0xf8); /* 0 0 0 */ 136 outb(addrPort, 0xfc); /* 1 0 0 */ 137 break; 138 139 case 5: 140 outb(addrPort, 0xf9); /* 0 0 1 */ 141 outb(addrPort, 0xfd); /* 1 0 1 */ 142 outb(addrPort, 0xf9); /* 0 0 1 */ 143 outb(addrPort, 0xfd); /* 1 0 1 */ 144 outb(addrPort, 0xf8); /* 0 0 0 */ 145 outb(addrPort, 0xfc); /* 1 0 0 */ 146 break; 147 148 default: 149 return(0xfff1); /* illegal channel number */ 150 } 151 152 outb(addrPort, 0xf9); /* 0 0 1 */ 153 outb(addrPort, 0xfd); /* 1 0 1 */ 154 /* unipolar */ 155 outb(addrPort, 0xf9); /* 0 0 1 */ 156 outb(addrPort, 0xfd); /* 1 0 1 */ 157 /* MSB first */ 158 outb(addrPort, 0xf9); /* 0 0 1 */ 159 outb(addrPort, 0xfd); /* 1 0 1 */ 160 outb(addrPort, 0xf9); /* 0 0 1 */ 161 outb(addrPort, 0xfd); /* 1 0 1 */ 162 outb(addrPort, 0xf9); /* 0 0 1 */ 163 /* end of control word-start of data */ 164 result = 0; 165 166 for(i = 1; i < 13; i++) 167 { 168 outb(addrPort, 0xfd); /* 1 0 1 */ 169 if(!(inb(addrPort + 1) & 0x80)) 170 { 171 result = result +1; 172 } 173 if(i < 12) 174 { 175 result = result *2; 176 } 177 outb(addrPort, 0xf9); /* 0 0 1 */ 178 } 179 outb(addrPort, 0xff); /* 1 1 1 */ 180 filt_result = filt_result + result; 181 } 182 rtnval = (unsigned int)filt_result/filter; 183 return(rtnval); 184 }