Rev 1306 Rev 1720
Line 1... Line 1...
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.1"
3 #define ID "$Id: irmrak4.c 1306 2009-01-17 12:25:40Z kakl $" 3 #define ID "$Id: irmrak4.c 1720 2010-12-10 20:47:52Z kakl $"
4   4  
5 #include "irmrak4.h" 5 #include "irmrak4.h"
6   6  
7 #bit CREN = 0x18.4 // USART registers 7 #bit CREN = 0x18.4 // USART registers
8 #bit SPEN = 0x18.7 8 #bit SPEN = 0x18.7
Line 47... Line 47...
47 void welcome(void) // Welcome message 47 void welcome(void) // Welcome message
48 { 48 {
49 char REV[50]=ID; // Buffer for concatenate of a version string 49 char REV[50]=ID; // Buffer for concatenate of a version string
50   50  
51 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; 51 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
52 printf("\n\r\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message 52 printf("\r\n\r\n# Mrakomer %s (C) 2007 UST\n\r",VER); // Welcome message
53 printf("#%s\n\r",&REV[4]); 53 printf("#%s\r\n",&REV[4]);
54 printf("#\n\r"); 54 // printf("#\r\n");
55 printf("# h - Switch On Heating for 20s.\n\r"); 55 printf("# commands: h, c, o, l, x, i, r, a, s, u\r\n");
56 printf("# c - Need Colder. Switch Off Heating.\n\r"); -  
57 printf("# o - Open the Dome for 20s.\n\r"); 56 // printf("# h_eat, c_old, o_pen, l_ock, x_open, ");
58 printf("# l - Lock the Dome.\n\r"); -  
59 printf("# x - Open the Dome and switch On Heating.\n\r"); -  
60 printf("# i - Print this Information.\n\r"); -  
61 printf("# r - Repeat measure every second.\n\r"); 57 // printf("i_nfo, r_epeat, a_uto, s_single, u_pdate\r\n");
62 printf("# s - Single measure.\n\r"); -  
63 printf("# u - Update firmware. Go to the Boot Loader.\n\r"); -  
64 printf("#\n\r"); 58 // printf("#\r\n");
65 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> "); 59 printf("# ver seq in[1/100 C] sky[1/100 C] sky[1/100 C] ");
66 printf("<heating[s]> <dome[s]> <check>\n\r\n\r"); 60 printf("out[1/100 C] heat[s] dome[s] check\r\n\r\n");
-   61  
67 //---WDT 62 //---WDT
68 restart_wdt(); 63 restart_wdt();
69 } 64 }
70   65  
71   66  
72 #include "smb.c" // System Management Bus driver 67 #include "smb.c" // System Management Bus driver
-   68 #include "TOUCH.C"
73   69  
74   70  
75 // Read sensor's RAM 71 // Read sensor's RAM
76 // Returns temperature in °K 72 // Returns temperature in °K
77 int16 ReadTemp(int8 addr, int8 select) 73 int16 ReadTemp(int8 addr, int8 select)
Line 101... Line 97...
101 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC 97 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
102   98  
103 return temp; 99 return temp;
104 } 100 }
105   101  
-   102 // compute CRC
-   103 // *sn - pointer to the byte array
-   104 // num - length of array
-   105 inline int8 TM_check_CRC(unsigned int8 *sn, unsigned int8 num)
-   106 {
-   107 // CRC table
-   108 const int8 TouchCRC[256]= {
-   109 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
-   110 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
-   111 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
-   112 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
-   113 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
-   114 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
-   115 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
-   116 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
-   117 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
-   118 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
-   119 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
-   120 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
-   121 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
-   122 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
-   123 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
-   124 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
-   125  
-   126 int8 CRC;
-   127 int8 i;
-   128  
-   129 CRC=0;
-   130 for(i=0;i<num;i++) CRC=TouchCRC[CRC ^ *(sn+i)];
-   131 return(CRC);
-   132 }
-   133  
106   134  
107 /*-------------------------------- MAIN --------------------------------------*/ 135 /*-------------------------------- MAIN --------------------------------------*/
108 void main() 136 void main()
109 { 137 {
110 unsigned int16 seq, temp, tempa; 138 unsigned int16 seq, temp, tempa;
111 signed int16 ta, to; 139 signed int16 ta, to1, to2, tTouch;
-   140 int8 tLSB,tMSB; // Temperatures from TouchMemory
112 int8 safety_counter; 141 int8 safety_counter;
-   142 int1 repeat; // Status flags
113 int1 repeat; 143 int1 automatic;
114   144  
115 output_high(DOME); // Close Dome 145 output_high(DOME); // Close Dome
116 output_low(HEATING); // Heating off 146 output_low(HEATING); // Heating off
117   147  
118 delay_ms(1000); 148 delay_ms(1000);
Line 120... Line 150...
120   150  
121 seq=0; // Variables initiation 151 seq=0; // Variables initiation
122 heat=0; 152 heat=0;
123 open=0; 153 open=0;
124 repeat=TRUE; 154 repeat=TRUE;
-   155 automatic=FALSE;
125   156  
126 welcome(); 157 welcome();
127   158  
128 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read 159 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
129 temp=ReadTemp(SA, RAM_Tobj1); 160 temp=ReadTemp(SA, RAM_Tobj1);
-   161 touch_present(); //Issues a reset of Touch Memory device
-   162 touch_write_byte(0xCC);
-   163 touch_write_byte(0x44);
130   164  
131 delay_ms(1000); 165 delay_ms(1000);
132 //---WDT 166 //---WDT
133 restart_wdt(); 167 restart_wdt();
134   168  
Line 163... Line 197...
163   197  
164 switch (ch) 198 switch (ch)
165 { 199 {
166 case 'h': 200 case 'h':
167 heat=MAXHEAT; // Need heating 201 heat=MAXHEAT; // Need heating
-   202 automatic=FALSE;
168 break; 203 break;
169   204  
170 case 'c': 205 case 'c':
171 heat=0; // Need colder 206 heat=0; // Need colder
-   207 automatic=FALSE;
172 break; 208 break;
173   209  
174 case 'o': 210 case 'o':
175 open=MAXOPEN; // Open the dome 211 open=MAXOPEN; // Open the dome
-   212 automatic=FALSE;
176 break; 213 break;
177   214  
178 case 'x': 215 case 'x':
179 open=MAXOPEN; // Open the dome 216 open=MAXOPEN; // Open the dome
180 heat=MAXHEAT; // Need heating 217 heat=MAXHEAT; // Need heating
-   218 automatic=FALSE;
181 break; 219 break;
182   220  
183 case 'l': 221 case 'l':
184 open=0; // Lock the dome 222 open=0; // Lock the dome
-   223 automatic=FALSE;
185 break; 224 break;
186   225  
187 case 'i': 226 case 'i':
188 if (open==0) welcome(); // Information about version, etc... 227 if (open==0) welcome(); // Information about version, etc...
189 break; // Only when dome is closed 228 break; // Only when dome is closed
190   229  
191 case 'r': 230 case 'r':
192 repeat=TRUE; // Repeated measure mode 231 repeat=TRUE; // Repeated measure mode
-   232 automatic=FALSE;
193 break; 233 break;
194   234  
195 case 's': 235 case 's':
196 repeat=FALSE; // Single measure mode 236 repeat=FALSE; // Single measure mode
-   237 automatic=FALSE;
-   238 break;
-   239  
-   240 case 'a':
-   241 repeat=TRUE; // Automatic mode
-   242 automatic=TRUE;
197 break; 243 break;
198   244  
199 case 'u': 245 case 'u':
200 reset_cpu(); // Update firmware 246 reset_cpu(); // Update firmware
201 } 247 }
202 } 248 }
203 // while(kbhit()) getc(); // Flush USART buffer -  
204 CREN=0; CREN=1; // Reinitialise USART 249 CREN=0; CREN=1; // Reinitialise USART
205   250  
206 seq++; // Increment the number of measurement 251 seq++; // Increment the number of measurement
207   252  
208 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor 253 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
-   254 ta=tempa*2-27315; // °K -> °C
-   255  
209 temp=ReadTemp(SA, RAM_Tobj1); 256 temp=ReadTemp(SA, RAM_Tobj1);
-   257 if (temp>0x48E1) {to1=-27315;} else {to1=temp*2-27315;}
-   258 temp=ReadTemp(SA, RAM_Tobj2);
-   259 if (temp>0x48E1) {to2=-27315;} else {to2=temp*2-27315;}
-   260  
-   261 touch_present(); //Issues a reset of Touch Memory device
-   262 touch_write_byte(0xCC);
-   263 touch_write_byte(0x44);
-   264
-   265 //---WDT
-   266 restart_wdt();
-   267 delay(MEASURE_DELAY); // Delay to a next measurement
210   268  
-   269 {
-   270 int8 SN[10];
-   271 int8 n;
-   272  
-   273 touch_present(); //Issues a reset and returns true if the touch device is there.
-   274 touch_write_byte(0xCC);
-   275 touch_write_byte(0xBE);
-   276 for(n=0;n<9;n++) SN[n]=touch_read_byte();
-   277 tLSB=SN[0];
-   278 tMSB=SN[1];
-   279 if ((SN[8]==TM_check_CRC(SN,8))&&(SN[7]==0x10)) // Check CRC and family code to prevent O's error
-   280 {
211 ta=tempa*2-27315; // °K -> °C 281 tTouch=make16(tMSB,tLSB);
-   282 tTouch=tTouch*6+tTouch/4; // 1bit = 0,0625gradC recalculate to 1/100gradC
-   283 }
-   284 else
-   285 {
212 to=temp*2-27315; 286 tTouch=-27315;
-   287 }
-   288 }
-   289
-   290 if(automatic) // Solve automatic mode
-   291 {
-   292 if(ta<1800) heat=MAXHEAT; // Need heating
-   293 if((abs(to1-to2)<80)&&(tTouch>to1)&&(abs(tTouch-to1)>1500))
-   294 open=MAXOPEN; // Open the dome
-   295 }
213   296  
214 { // printf 297 { // printf
215 char output[8]; // Output buffer 298 char output[8]; // Output buffer
216 int8 j; // String pointer 299 int8 j; // String pointer
217 int8 check=0; // Checksum is calculated between '$' and '*' 300 int8 check=0; // Checksum is calculated between '$' and '*'
218   301  
219 delay(SEND_DELAY); 302 delay(SEND_DELAY);
220 putc('$'); 303 putc('$');
221 delay(SEND_DELAY); 304 delay(SEND_DELAY);
222 sprintf(output,"M%s ",VER); 305 sprintf(output,"M%s \0",VER);
-   306 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
-   307 sprintf(output,"%Lu \0", seq);
223 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 308 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
224 sprintf(output,"%Lu ", seq); 309 sprintf(output,"%Ld \0", ta);
225 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 310 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
226 sprintf(output,"%Ld ", ta); 311 sprintf(output,"%Ld \0", to1);
227 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 312 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
228 sprintf(output,"%Ld ", to); 313 sprintf(output,"%Ld \0", to2);
229 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 314 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
230 sprintf(output,"%u ", heat); 315 sprintf(output,"%Ld \0",tTouch);
231 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 316 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
232 sprintf(output,"%u ", open); 317 sprintf(output,"%u \0", heat);
233 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 318 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
-   319 sprintf(output,"%u \0", open);
-   320 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
234 sprintf(output,"*%X\n\r\0", check); 321 sprintf(output,"*%X\r\n\0", check);
235 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } 322 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
236 delay(SEND_DELAY); 323 delay(SEND_DELAY);
237 } 324 }
238   325
239 delay(MEASURE_DELAY); // Delay to a next measurement -  
240 //---WDT 326 //---WDT
241 restart_wdt(); 327 restart_wdt();
242 } 328 }
243 } 329 }
244   330