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