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