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