Rev Author Line No. Line
1274 kakl 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0"
1277 kakl 3 #define ID "$Id: irmrak4.c 1293 2009-01-11 22:53:46Z 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 #include "bloader.c" // Boot Loader driver
14  
1289 kakl 15 #CASE // Case sensitive compiler
16  
1280 kakl 17 #define MAXHEAT 20 // Number of cycles for heating
18 #define MAXOPEN 20 // Number of cycles for dome open
1287 kakl 19 #define MEASURE_DELAY 6000 // Delay to a next measurement
1280 kakl 20 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
1287 kakl 21 #define SAFETY_COUNT 90 // Time of one emergency cycle
1280 kakl 22 #define SEND_DELAY 50 // Time between two characters on RS232
1275 kakl 23  
1274 kakl 24 #define DOME PIN_B4 // Dome controll port
25 #define HEATING PIN_B3 // Heating for defrosting
26  
27  
1280 kakl 28 char VER[4]=VERSION; // Buffer for concatenate of a version string
1274 kakl 29  
1280 kakl 30 int8 heat; // Status variables
1274 kakl 31 int8 open;
32  
1284 kakl 33 inline void toggle_dome(void) // Wire exercise
1275 kakl 34 {
35 if (open>0)
1285 kakl 36 {output_toggle(DOME);} // Toggle = Open Dome
37 else
38 {output_high(DOME);} // Do not toggle = Close Dome
1275 kakl 39 }
1274 kakl 40  
1284 kakl 41 void delay(int16 cycles) // Wire exercise with delay
1274 kakl 42 {
1275 kakl 43 int16 i;
1278 kakl 44  
1275 kakl 45 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
46 }
1274 kakl 47  
1284 kakl 48 void welcome(void) // Welcome message
49 {
1293 kakl 50 char REV[50]=ID; // Buffer for concatenate of a version string
51  
52 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
53 printf("\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message
54 printf("#%s\n\r",&REV[4]);
55 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> ");
56 printf("<heating[s]> <dome[s]> <check>\n\r\n\r");
1284 kakl 57 }
1274 kakl 58  
59  
1284 kakl 60 #include "smb.c" // System Management Bus driver
1274 kakl 61  
1284 kakl 62  
1293 kakl 63 // Read sensor's RAM
1280 kakl 64 // Returns temperature in °K
1281 kakl 65 int16 ReadTemp(int8 addr, int8 select)
1274 kakl 66 {
67 unsigned char arr[6]; // Buffer for the sent bytes
68 int8 crc; // Readed CRC
69 int16 temp; // Readed temperature
70  
1275 kakl 71 addr<<=1;
72  
1284 kakl 73 SMB_STOP_bit(); //If slave send NACK stop comunication
74 SMB_START_bit(); //Start condition
1275 kakl 75 SMB_TX_byte(addr);
76 SMB_TX_byte(RAM_Access|select);
1284 kakl 77 SMB_START_bit(); //Repeated Start condition
1275 kakl 78 SMB_TX_byte(addr);
1284 kakl 79 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
80 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
1289 kakl 81 temp=make16(arr[1],arr[2]);
1284 kakl 82 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
83 SMB_STOP_bit(); //Stop condition
1274 kakl 84  
85 arr[5]=addr;
86 arr[4]=RAM_Access|select;
87 arr[3]=addr;
88 arr[0]=0;
89 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
90  
91 return temp;
92 }
93  
1284 kakl 94 /*-----------------------------------------------------------------------*/
1274 kakl 95 void main()
96 {
1280 kakl 97 unsigned int16 seq, temp, tempa;
1274 kakl 98 signed int16 ta, to;
1280 kakl 99 int8 safety_counter;
1288 kakl 100 int1 repeat;
1274 kakl 101  
1285 kakl 102 output_high(DOME); // Close Dome
1274 kakl 103 output_low(HEATING); // Heating off
104 setup_wdt(WDT_2304MS); // Setup Watch Dog
105 setup_adc_ports(NO_ANALOGS);
106 setup_adc(ADC_OFF);
107 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
108 setup_timer_1(T1_DISABLED);
109 setup_timer_2(T2_DISABLED,0,1);
110 setup_comparator(NC_NC_NC_NC);
111 setup_vref(FALSE);
112 // setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc
1283 kakl 113 setup_oscillator(OSC_8MHZ|OSC_INTRC);
1274 kakl 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':
197 load_program(); // 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 }