Rev Author Line No. Line
1274 kakl 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0"
3 #define ID "$Id: irmrak3.c 1215 2008-08-08 12:25:25Z kakl $"
4 #include "irmrak4.h"
5  
6 #define DOME PIN_B4 // Dome controll port
7 #define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default)
8 #define RAM_Access 0x00 // RAM access command
9 #define RAM_Tobj1 0x07 // To1 address in the eeprom
10 #define RAM_Tamb 0x06 // Ta address in the eeprom
11 #define HEATING PIN_B3 // Heating for defrosting
12 #define MAXHEAT 10 // Number of cycles for heating
13 #define MAXOPEN 10 // Number of cycles for dome open
14  
15 #bit CREN = 0x18.4 // USART registers
16 #bit SPEN = 0x18.7
17 #bit OERR = 0x18.1
18 #bit FERR = 0x18.2
19  
20 char VER[4]=VERSION;
21 char REV[50]=ID;
22  
23 int8 heat;
24 int8 open;
25  
26  
27 unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation
28 {
29 unsigned char crc[6];
30 unsigned char BitPosition=47;
31 unsigned char shift;
32 unsigned char i;
33 unsigned char j;
34 unsigned char temp;
35  
36 do
37 {
38 crc[5]=0; /* Load CRC value 0x000000000107 */
39 crc[4]=0;
40 crc[3]=0;
41 crc[2]=0;
42 crc[1]=0x01;
43 crc[0]=0x07;
44 BitPosition=47; /* Set maximum bit position at 47 */
45 shift=0;
46  
47 //Find first 1 in the transmited message
48 i=5; /* Set highest index */
49 j=0;
50 while((pec[i]&(0x80>>j))==0 && i>0)
51 {
52 BitPosition--;
53 if(j<7)
54 {
55 j++;
56 }
57 else
58 {
59 j=0x00;
60 i--;
61 }
62 }/*End of while */
63  
64 shift=BitPosition-8; /*Get shift value for crc value*/
65  
66  
67 //Shift crc value
68 while(shift)
69 {
70 for(i=5; i<0xFF; i--)
71 {
72 if((crc[i-1]&0x80) && (i>0))
73 {
74 temp=1;
75 }
76 else
77 {
78 temp=0;
79 }
80 crc[i]<<=1;
81 crc[i]+=temp;
82 }/*End of for*/
83 shift--;
84 }/*End of while*/
85  
86 //Exclusive OR between pec and crc
87 for(i=0; i<=5; i++)
88 {
89 pec[i] ^=crc[i];
90 }/*End of for*/
91 } while(BitPosition>8);/*End of do-while*/
92  
93 return pec[0];
94 }/*End of PEC_calculation*/
95  
96  
97 int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM
98 {
99 unsigned char arr[6]; // Buffer for the sent bytes
100 int8 crc; // Readed CRC
101 int16 temp; // Readed temperature
102  
103 disable_interrupts(GLOBAL);
104 i2c_stop();
105 i2c_start();
106 i2c_write(addr);
107 i2c_write(RAM_Access|select); // Select the teperature sensor in device
108 i2c_start();
109 i2c_write(addr);
110 arr[2]=i2c_read(1); // lo
111 arr[1]=i2c_read(1); // hi
112 temp=MAKE16(arr[1],arr[2]);
113 crc=i2c_read(0); //crc
114 i2c_stop();
115 enable_interrupts(GLOBAL);
116  
117 arr[5]=addr;
118 arr[4]=RAM_Access|select;
119 arr[3]=addr;
120 arr[0]=0;
121 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
122  
123 return temp;
124 }
125  
126 void delay(int16 cycles)
127 {
128 int16 i;
129  
130 if (open>0)
131 for(i=0; i<cycles; i++) {output_toggle(DOME); delay_us(100);}
132 else
133 for(i=0; i<cycles; i++) {output_low(DOME); delay_us(100);}
134  
135 restart_wdt();
136 }
137  
138 void main()
139 {
140 unsigned int16 n, temp, tempa;
141 signed int16 ta, to;
142  
143 output_low(HEATING); // Heating off
144 setup_wdt(WDT_2304MS); // Setup Watch Dog
145 setup_adc_ports(NO_ANALOGS);
146 setup_adc(ADC_OFF);
147 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
148 setup_timer_1(T1_DISABLED);
149 setup_timer_2(T2_DISABLED,0,1);
150 setup_comparator(NC_NC_NC_NC);
151 setup_vref(FALSE);
152 // setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc
153 setup_oscillator(OSC_4MHZ|OSC_INTRC);
154  
155 delay_ms(1000);
156 restart_wdt();
157 printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message
158 printf("* %s *\n\r",REV);
159 printf("<#seq.> <ambient temp.> <space temp.> <status>\n\r\n\r");
160 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
161 temp=ReadTemp(SA, RAM_Tobj1);
162  
163 n=0;
164 heat=0;
165 open=0;
166  
167 // enable_interrupts(GLOBAL);
168 // enable_interrupts(INT_RDA);
169  
170 restart_wdt();
171  
172 while(TRUE)
173 {
174 while(kbhit()) getc(); // Flush USART buffer
175 CREN=0; CREN=1; // Reinitialise USART
176  
177 do
178 {
179 delay(10000);
180 if (heat>0)
181 {
182 output_high(HEATING);
183 heat--;
184 }
185 else
186 {
187 output_low(HEATING);
188 }
189  
190 if (open>0) open--;
191 } while (!kbhit());
192  
193 {
194 char ch;
195  
196 ch=getc();
197  
198 switch (ch)
199 {
200 case 'h':
201 heat=MAXHEAT; // Needs heating
202 break;
203  
204 case 'c':
205 heat=0; // Needs colder
206 break;
207  
208 case 'o':
209 open=MAXOPEN; // Open the dome
210 break;
211  
212 case 'l':
213 open=0; // Lock the dome
214 break;
215 }
216 }
217  
218 n++; // Increment the number of measurement
219  
220 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
221 temp=ReadTemp(SA, RAM_Tobj1);
222  
223 ta=tempa*2-27315; // °K -> °C
224 to=temp*2-27315;
225  
226 { // printf
227 char output[30];
228 int8 j;
229  
230 sprintf(output,"#%Lu %Ld %Ld %u %u\n\r\0", n, ta, to, heat, open);
231  
232 j=0;
233 while(output[j]!=0)
234 {
235 delay(50);
236 putc(output[j++]);
237 output_toggle(DOME);
238 }
239 }
240 }
241 }
242