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