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