Rev 1302 Rev 1303
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.0"
3 #define ID "$Id: irmrak4.c 1302 2009-01-17 09:03:34Z kakl $" 3 #define ID "$Id: irmrak4.c 1303 2009-01-17 09:06:15Z 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
9 #bit OERR = 0x18.1 9 #bit OERR = 0x18.1
10 #bit FERR = 0x18.2 10 #bit FERR = 0x18.2
11   11  
12 #include <string.h> 12 #include <string.h>
13   13  
14 #CASE // Case sensitive compiler 14 #CASE // Case sensitive compiler
15   15  
16 #define MAXHEAT 20 // Number of cycles for heating 16 #define MAXHEAT 20 // Number of cycles for heating
17 #define MAXOPEN 20 // Number of cycles for dome open 17 #define MAXOPEN 20 // Number of cycles for dome open
18 #define MEASURE_DELAY 6000 // Delay to a next measurement 18 #define MEASURE_DELAY 6000 // Delay to a next measurement
19 #define RESPONSE_DELAY 100 // Reaction time after receiving a command 19 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
20 #define SAFETY_COUNT 90 // Time of one emergency cycle 20 #define SAFETY_COUNT 90 // Time of one emergency cycle
21 #define SEND_DELAY 50 // Time between two characters on RS232 21 #define SEND_DELAY 50 // Time between two characters on RS232
22   22  
23 #define DOME PIN_B4 // Dome controll port 23 #define DOME PIN_B4 // Dome controll port
24 #define HEATING PIN_B3 // Heating for defrosting 24 #define HEATING PIN_B3 // Heating for defrosting
25   25  
26   26  
27 char VER[4]=VERSION; // Buffer for concatenate of a version string 27 char VER[4]=VERSION; // Buffer for concatenate of a version string
28   28  
29 int8 heat; // Status variables 29 int8 heat; // Status variables
30 int8 open; 30 int8 open;
31   31  
32 inline void toggle_dome(void) // Wire exercise 32 inline void toggle_dome(void) // Wire exercise
33 { 33 {
34 if (open>0) 34 if (open>0)
35 {output_toggle(DOME);} // Toggle = Open Dome 35 {output_toggle(DOME);} // Toggle = Open Dome
36 else 36 else
37 {output_high(DOME);} // Do not toggle = Close Dome 37 {output_high(DOME);} // Do not toggle = Close Dome
38 } 38 }
39   39  
40 void delay(int16 cycles) // Wire exercise with delay 40 void delay(int16 cycles) // Wire exercise with delay
41 { 41 {
42 int16 i; 42 int16 i;
43   43  
44 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);} 44 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
45 } 45 }
46   46  
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# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message 52 printf("\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message
53 printf("#%s\n\r",&REV[4]); 53 printf("#%s\n\r",&REV[4]);
54 printf("# h - Switch on heating for 20s.\n\r"); 54 printf("# h - Switch on heating for 20s.\n\r");
55 printf("# c - Need Colder. Switch off heating.\n\r"); 55 printf("# c - Need Colder. Switch off heating.\n\r");
56 printf("# o - Open the Dome for 20s.\n\r"); 56 printf("# o - Open the Dome for 20s.\n\r");
57 printf("# l - Lock the dome.\n\r"); 57 printf("# l - Lock the dome.\n\r");
58 printf("# x - Open the Dome and switch on heating.\n\r"); 58 printf("# x - Open the Dome and switch on heating.\n\r");
59 printf("# i - Print this Information.\n\r"); 59 printf("# i - Print this Information.\n\r");
60 printf("# r - Repeat measure every second.\n\r"); 60 printf("# r - Repeat measure every second.\n\r");
61 printf("# s - Single measure mode on demand.\n\r"); 61 printf("# s - Single measure mode on demand.\n\r");
62 printf("# u - Update firmware. Go to the Boot Loader.\n\r"); 62 printf("# u - Update firmware. Go to the Boot Loader.\n\r");
63 printf("#\n\r"); 63 printf("#\n\r");
64 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> "); 64 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> ");
65 printf("<heating[s]> <dome[s]> <check>\n\r\n\r"); 65 printf("<heating[s]> <dome[s]> <check>\n\r\n\r");
-   66 //---WDT
-   67 restart_wdt();
66 } 68 }
67   69  
68   70  
69 #include "smb.c" // System Management Bus driver 71 #include "smb.c" // System Management Bus driver
70   72  
71   73  
72 // Read sensor's RAM 74 // Read sensor's RAM
73 // Returns temperature in °K 75 // Returns temperature in °K
74 int16 ReadTemp(int8 addr, int8 select) 76 int16 ReadTemp(int8 addr, int8 select)
75 { 77 {
76 unsigned char arr[6]; // Buffer for the sent bytes 78 unsigned char arr[6]; // Buffer for the sent bytes
77 int8 crc; // Readed CRC 79 int8 crc; // Readed CRC
78 int16 temp; // Readed temperature 80 int16 temp; // Readed temperature
79   81  
80 addr<<=1; 82 addr<<=1;
81   83  
82 SMB_STOP_bit(); //If slave send NACK stop comunication 84 SMB_STOP_bit(); //If slave send NACK stop comunication
83 SMB_START_bit(); //Start condition 85 SMB_START_bit(); //Start condition
84 SMB_TX_byte(addr); 86 SMB_TX_byte(addr);
85 SMB_TX_byte(RAM_Access|select); 87 SMB_TX_byte(RAM_Access|select);
86 SMB_START_bit(); //Repeated Start condition 88 SMB_START_bit(); //Repeated Start condition
87 SMB_TX_byte(addr); 89 SMB_TX_byte(addr);
88 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK 90 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
89 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK 91 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
90 temp=make16(arr[1],arr[2]); 92 temp=make16(arr[1],arr[2]);
91 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK 93 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
92 SMB_STOP_bit(); //Stop condition 94 SMB_STOP_bit(); //Stop condition
93   95  
94 arr[5]=addr; 96 arr[5]=addr;
95 arr[4]=RAM_Access|select; 97 arr[4]=RAM_Access|select;
96 arr[3]=addr; 98 arr[3]=addr;
97 arr[0]=0; 99 arr[0]=0;
98 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC 100 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
99   101  
100 return temp; 102 return temp;
101 } 103 }
102   104  
103   105  
104 /*-------------------------------- MAIN --------------------------------------*/ 106 /*-------------------------------- MAIN --------------------------------------*/
105 void main() 107 void main()
106 { 108 {
107 unsigned int16 seq, temp, tempa; 109 unsigned int16 seq, temp, tempa;
108 signed int16 ta, to; 110 signed int16 ta, to;
109 int8 safety_counter; 111 int8 safety_counter;
110 int1 repeat; 112 int1 repeat;
111   113  
112 output_high(DOME); // Close Dome 114 output_high(DOME); // Close Dome
113 output_low(HEATING); // Heating off 115 output_low(HEATING); // Heating off
114   116  
115 delay_ms(1000); 117 delay_ms(1000);
116 restart_wdt(); 118 restart_wdt();
117   119  
118 seq=0; // Variables initiation 120 seq=0; // Variables initiation
119 heat=0; 121 heat=0;
120 open=0; 122 open=0;
121 repeat=TRUE; 123 repeat=TRUE;
122   124  
123 welcome(); 125 welcome();
124 //---WDT -  
125 restart_wdt(); -  
126   126  
127 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read 127 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
128 temp=ReadTemp(SA, RAM_Tobj1); 128 temp=ReadTemp(SA, RAM_Tobj1);
129   129  
130 delay_ms(1000); 130 delay_ms(1000);
131 //---WDT 131 //---WDT
132 restart_wdt(); 132 restart_wdt();
133   133  
134 while(TRUE) // Main Loop 134 while(TRUE) // Main Loop
135 { 135 {
136 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down 136 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down
137 do 137 do
138 { 138 {
139 if (safety_counter<SAFETY_COUNT) safety_counter++; 139 if (safety_counter<SAFETY_COUNT) safety_counter++;
140   140  
141 delay(RESPONSE_DELAY); 141 delay(RESPONSE_DELAY);
142   142  
143 if (safety_counter>=SAFETY_COUNT) 143 if (safety_counter>=SAFETY_COUNT)
144 { 144 {
145 if (heat>0) heat--; 145 if (heat>0) heat--;
146 if (open>0) open--; 146 if (open>0) open--;
147   147  
148 if (heat>0) { output_high(HEATING); } else { output_low(HEATING); } 148 if (heat>0) { output_high(HEATING); } else { output_low(HEATING); }
149   149  
150 safety_counter=0; 150 safety_counter=0;
151 //---WDT 151 //---WDT
152 restart_wdt(); 152 restart_wdt();
153 } 153 }
154 } while (!kbhit()&&!repeat); 154 } while (!kbhit()&&!repeat);
155   155  
156 //---WDT 156 //---WDT
157 restart_wdt(); 157 restart_wdt();
158 { // Retrieve command 158 { // Retrieve command
159 char ch='k'; 159 char ch='k';
160   160  
161 if(kbhit()) ch=getc(); 161 if(kbhit()) ch=getc();
162   162  
163 switch (ch) 163 switch (ch)
164 { 164 {
165 case 'h': 165 case 'h':
166 heat=MAXHEAT; // Need heating 166 heat=MAXHEAT; // Need heating
167 break; 167 break;
168   168  
169 case 'c': 169 case 'c':
170 heat=0; // Need colder 170 heat=0; // Need colder
171 break; 171 break;
172   172  
173 case 'o': 173 case 'o':
174 open=MAXOPEN; // Open the dome 174 open=MAXOPEN; // Open the dome
175 break; 175 break;
176   176  
177 case 'x': 177 case 'x':
178 open=MAXOPEN; // Open the dome 178 open=MAXOPEN; // Open the dome
179 heat=MAXHEAT; // Need heating 179 heat=MAXHEAT; // Need heating
180 break; 180 break;
181   181  
182 case 'l': 182 case 'l':
183 open=0; // Lock the dome 183 open=0; // Lock the dome
184 break; 184 break;
185   185  
186 case 'i': 186 case 'i':
187 if (open==0) welcome(); // Information about version, etc... 187 if (open==0) welcome(); // Information about version, etc...
188 break; // Only when dome is closed 188 break; // Only when dome is closed
189   189  
190 case 'r': 190 case 'r':
191 repeat=TRUE; // Repeated measure mode 191 repeat=TRUE; // Repeated measure mode
192 break; 192 break;
193   193  
194 case 's': 194 case 's':
195 repeat=FALSE; // Single measure mode 195 repeat=FALSE; // Single measure mode
196 break; 196 break;
197   197  
198 case 'u': 198 case 'u':
199 reset_cpu(); // Update firmware 199 reset_cpu(); // Update firmware
200 } 200 }
201 } 201 }
202 // while(kbhit()) getc(); // Flush USART buffer 202 // while(kbhit()) getc(); // Flush USART buffer
203 CREN=0; CREN=1; // Reinitialise USART 203 CREN=0; CREN=1; // Reinitialise USART
204   204  
205 seq++; // Increment the number of measurement 205 seq++; // Increment the number of measurement
206   206  
207 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor 207 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
208 temp=ReadTemp(SA, RAM_Tobj1); 208 temp=ReadTemp(SA, RAM_Tobj1);
209   209  
210 ta=tempa*2-27315; // °K -> °C 210 ta=tempa*2-27315; // °K -> °C
211 to=temp*2-27315; 211 to=temp*2-27315;
212   212  
213 { // printf 213 { // printf
214 char output[8]; // Output buffer 214 char output[8]; // Output buffer
215 int8 j; // String pointer 215 int8 j; // String pointer
216 int8 check=0; // Checksum is calculated between '$' and '*' 216 int8 check=0; // Checksum is calculated between '$' and '*'
217   217  
218 delay(SEND_DELAY); 218 delay(SEND_DELAY);
219 putc('$'); 219 putc('$');
220 delay(SEND_DELAY); 220 delay(SEND_DELAY);
221 sprintf(output,"M%s ",VER); 221 sprintf(output,"M%s ",VER);
222 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 222 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
223 sprintf(output,"%Lu ", seq); 223 sprintf(output,"%Lu ", seq);
224 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 224 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
225 sprintf(output,"%Ld ", ta); 225 sprintf(output,"%Ld ", ta);
226 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 226 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
227 sprintf(output,"%Ld ", to); 227 sprintf(output,"%Ld ", to);
228 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 228 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
229 sprintf(output,"%u ", heat); 229 sprintf(output,"%u ", heat);
230 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 230 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
231 sprintf(output,"%u ", open); 231 sprintf(output,"%u ", open);
232 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 232 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
233 sprintf(output,"*%X\n\r\0", check); 233 sprintf(output,"*%X\n\r\0", check);
234 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } 234 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
235 delay(SEND_DELAY); 235 delay(SEND_DELAY);
236 } 236 }
237   237  
238 delay(MEASURE_DELAY); // Delay to a next measurement 238 delay(MEASURE_DELAY); // Delay to a next measurement
239 //---WDT 239 //---WDT
240 restart_wdt(); 240 restart_wdt();
241 } 241 }
242 } 242 }
243   243  
244   244  
245 #include "dbloader.c" // Space reservation for the BootLoader 245 #include "dbloader.c" // Space reservation for the BootLoader