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