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