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