Rev Author Line No. Line
1274 kakl 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0"
1277 kakl 3 #define ID "$Id: irmrak4.c 1301 2009-01-17 08:51:50Z 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");
62 printf("# U - Update firmware. Go to the Boot Loader.\n\r");
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();
1285 kakl 124  
1274 kakl 125 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
126 temp=ReadTemp(SA, RAM_Tobj1);
127  
1286 kakl 128 delay_ms(1000);
1280 kakl 129 //---WDT
1274 kakl 130 restart_wdt();
131  
1284 kakl 132 while(TRUE) // Main Loop
1274 kakl 133 {
1284 kakl 134 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down
1278 kakl 135 do
1274 kakl 136 {
1280 kakl 137 if (safety_counter<SAFETY_COUNT) safety_counter++;
1274 kakl 138  
1280 kakl 139 delay(RESPONSE_DELAY);
140  
141 if (safety_counter>=SAFETY_COUNT)
142 {
1288 kakl 143 if (heat>0) heat--;
1280 kakl 144 if (open>0) open--;
145  
1290 kakl 146 if (heat>0) { output_high(HEATING); } else { output_low(HEATING); }
147  
1280 kakl 148 safety_counter=0;
149 //---WDT
150 restart_wdt();
151 }
1288 kakl 152 } while (!kbhit()&&!repeat);
1285 kakl 153  
1280 kakl 154 //---WDT
155 restart_wdt();
1284 kakl 156 { // Retrieve command
1288 kakl 157 char ch='k';
1278 kakl 158  
1288 kakl 159 if(kbhit()) ch=getc();
1278 kakl 160  
1274 kakl 161 switch (ch)
162 {
163 case 'h':
1280 kakl 164 heat=MAXHEAT; // Need heating
1274 kakl 165 break;
1278 kakl 166  
1274 kakl 167 case 'c':
1280 kakl 168 heat=0; // Need colder
1274 kakl 169 break;
1278 kakl 170  
1274 kakl 171 case 'o':
172 open=MAXOPEN; // Open the dome
173 break;
1278 kakl 174  
1284 kakl 175 case 'x':
176 open=MAXOPEN; // Open the dome
177 heat=MAXHEAT; // Need heating
178 break;
179  
1274 kakl 180 case 'l':
181 open=0; // Lock the dome
182 break;
1284 kakl 183  
184 case 'i':
1287 kakl 185 if (open==0) welcome(); // Information about version, etc...
186 break; // Only when dome is closed
1288 kakl 187  
188 case 'r':
1292 kakl 189 repeat=TRUE; // Repeated measure mode
1288 kakl 190 break;
191  
192 case 's':
193 repeat=FALSE; // Single measure mode
194 break;
1293 kakl 195  
196 case 'u':
1300 kakl 197 reset_cpu(); // Update firmware
1274 kakl 198 }
199 }
1288 kakl 200 // while(kbhit()) getc(); // Flush USART buffer
201 CREN=0; CREN=1; // Reinitialise USART
1274 kakl 202  
1280 kakl 203 seq++; // Increment the number of measurement
1274 kakl 204  
205 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
206 temp=ReadTemp(SA, RAM_Tobj1);
207  
208 ta=tempa*2-27315; // °K -> °C
209 to=temp*2-27315;
210  
211 { // printf
1284 kakl 212 char output[8]; // Output buffer
213 int8 j; // String pointer
1293 kakl 214 int8 check=0; // Checksum is calculated between '$' and '*'
1278 kakl 215  
1282 kakl 216 delay(SEND_DELAY);
1293 kakl 217 putc('$');
218 delay(SEND_DELAY);
219 sprintf(output,"M%s ",VER);
220 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
221 sprintf(output,"%Lu ", seq);
222 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
1282 kakl 223 sprintf(output,"%Ld ", ta);
1293 kakl 224 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
1282 kakl 225 sprintf(output,"%Ld ", to);
1293 kakl 226 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
1283 kakl 227 sprintf(output,"%u ", heat);
1293 kakl 228 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
229 sprintf(output,"%u ", open);
230 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
231 sprintf(output,"*%X\n\r\0", check);
1282 kakl 232 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
1293 kakl 233 delay(SEND_DELAY);
1274 kakl 234 }
1280 kakl 235  
236 delay(MEASURE_DELAY); // Delay to a next measurement
237 //---WDT
238 restart_wdt();
1274 kakl 239 }
240 }
1298 kakl 241  
242  
1300 kakl 243 #include "dbloader.c" // Space reservation for the BootLoader