1274 |
kakl |
1 |
/**** IR Mrakomer 4 ****/ |
|
|
2 |
#define VERSION "4.0" |
1277 |
kakl |
3 |
#define ID "$Id: irmrak4.c 1282 2009-01-07 13:07:23Z 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 |
|
|
8 |
#define MEASURE_DELAY 10000 // Delay to a next measurement |
|
|
9 |
#define RESPONSE_DELAY 100 // Reaction time after receiving a command |
|
|
10 |
#define SAFETY_COUNT 100 // Time of one emergency cycle |
|
|
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 |
|
1275 |
kakl |
27 |
inline void toggle_dome(void) |
|
|
28 |
{ |
|
|
29 |
if (open>0) |
|
|
30 |
{output_toggle(DOME);} |
|
|
31 |
else |
|
|
32 |
{output_low(DOME);} |
|
|
33 |
} |
1274 |
kakl |
34 |
|
1275 |
kakl |
35 |
void delay(int16 cycles) |
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 |
|
|
|
42 |
|
1275 |
kakl |
43 |
#include "smb.c" |
1274 |
kakl |
44 |
|
|
|
45 |
|
1280 |
kakl |
46 |
// Read sensor RAM |
|
|
47 |
// Returns temperature in °K |
1281 |
kakl |
48 |
int16 ReadTemp(int8 addr, int8 select) |
1274 |
kakl |
49 |
{ |
|
|
50 |
unsigned char arr[6]; // Buffer for the sent bytes |
|
|
51 |
int8 crc; // Readed CRC |
|
|
52 |
int16 temp; // Readed temperature |
|
|
53 |
|
1275 |
kakl |
54 |
addr<<=1; |
|
|
55 |
|
|
|
56 |
SMB_STOP_bit(); //If slave send NACK stop comunication |
|
|
57 |
SMB_START_bit(); //Start condition |
|
|
58 |
SMB_TX_byte(addr); |
|
|
59 |
SMB_TX_byte(RAM_Access|select); |
|
|
60 |
SMB_START_bit(); //Repeated Start condition |
|
|
61 |
SMB_TX_byte(addr); |
|
|
62 |
arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK |
|
|
63 |
arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK |
1274 |
kakl |
64 |
temp=MAKE16(arr[1],arr[2]); |
1275 |
kakl |
65 |
crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK |
|
|
66 |
SMB_STOP_bit(); //Stop condition |
1274 |
kakl |
67 |
|
|
|
68 |
arr[5]=addr; |
|
|
69 |
arr[4]=RAM_Access|select; |
|
|
70 |
arr[3]=addr; |
|
|
71 |
arr[0]=0; |
|
|
72 |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
|
|
73 |
|
|
|
74 |
return temp; |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
void main() |
|
|
78 |
{ |
1280 |
kakl |
79 |
unsigned int16 seq, temp, tempa; |
1274 |
kakl |
80 |
signed int16 ta, to; |
1280 |
kakl |
81 |
int8 safety_counter; |
1274 |
kakl |
82 |
|
|
|
83 |
output_low(HEATING); // Heating off |
|
|
84 |
setup_wdt(WDT_2304MS); // Setup Watch Dog |
|
|
85 |
setup_adc_ports(NO_ANALOGS); |
|
|
86 |
setup_adc(ADC_OFF); |
|
|
87 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
88 |
setup_timer_1(T1_DISABLED); |
|
|
89 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
90 |
setup_comparator(NC_NC_NC_NC); |
|
|
91 |
setup_vref(FALSE); |
|
|
92 |
// setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc |
|
|
93 |
setup_oscillator(OSC_4MHZ|OSC_INTRC); |
|
|
94 |
|
|
|
95 |
delay_ms(1000); |
|
|
96 |
restart_wdt(); |
|
|
97 |
printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message |
|
|
98 |
printf("* %s *\n\r",REV); |
1282 |
kakl |
99 |
printf("<#sequence> <ambient [1/100 C]> <sky [1/100 C]> "); |
1280 |
kakl |
100 |
printf("<heating [s]> <dome [s]>\n\r\n\r"); |
1274 |
kakl |
101 |
tempa=ReadTemp(SA, RAM_Tamb); // Dummy read |
|
|
102 |
temp=ReadTemp(SA, RAM_Tobj1); |
|
|
103 |
|
1280 |
kakl |
104 |
seq=0; |
1274 |
kakl |
105 |
heat=0; |
|
|
106 |
open=0; |
|
|
107 |
|
|
|
108 |
// enable_interrupts(GLOBAL); |
|
|
109 |
// enable_interrupts(INT_RDA); |
|
|
110 |
|
1280 |
kakl |
111 |
//---WDT |
1274 |
kakl |
112 |
restart_wdt(); |
|
|
113 |
|
|
|
114 |
while(TRUE) |
|
|
115 |
{ |
|
|
116 |
while(kbhit()) getc(); // Flush USART buffer |
|
|
117 |
CREN=0; CREN=1; // Reinitialise USART |
|
|
118 |
|
1280 |
kakl |
119 |
safety_counter=0; |
|
|
120 |
|
1278 |
kakl |
121 |
do |
1274 |
kakl |
122 |
{ |
1280 |
kakl |
123 |
if (safety_counter<SAFETY_COUNT) safety_counter++; |
1274 |
kakl |
124 |
|
1280 |
kakl |
125 |
delay(RESPONSE_DELAY); |
|
|
126 |
|
|
|
127 |
if (safety_counter>=SAFETY_COUNT) |
|
|
128 |
{ |
|
|
129 |
if (heat>0) |
|
|
130 |
{ |
|
|
131 |
output_high(HEATING); |
|
|
132 |
heat--; |
|
|
133 |
} |
|
|
134 |
else |
|
|
135 |
{ |
|
|
136 |
output_low(HEATING); |
|
|
137 |
} |
1281 |
kakl |
138 |
|
1280 |
kakl |
139 |
if (open>0) open--; |
|
|
140 |
|
|
|
141 |
safety_counter=0; |
|
|
142 |
//---WDT |
|
|
143 |
restart_wdt(); |
|
|
144 |
} |
1274 |
kakl |
145 |
} while (!kbhit()); |
|
|
146 |
|
1280 |
kakl |
147 |
//---WDT |
|
|
148 |
restart_wdt(); |
1274 |
kakl |
149 |
{ |
|
|
150 |
char ch; |
1278 |
kakl |
151 |
|
1274 |
kakl |
152 |
ch=getc(); |
1278 |
kakl |
153 |
|
1274 |
kakl |
154 |
switch (ch) |
|
|
155 |
{ |
|
|
156 |
case 'h': |
1280 |
kakl |
157 |
heat=MAXHEAT; // Need heating |
1274 |
kakl |
158 |
break; |
1278 |
kakl |
159 |
|
1274 |
kakl |
160 |
case 'c': |
1280 |
kakl |
161 |
heat=0; // Need colder |
1274 |
kakl |
162 |
break; |
1278 |
kakl |
163 |
|
1274 |
kakl |
164 |
case 'o': |
|
|
165 |
open=MAXOPEN; // Open the dome |
|
|
166 |
break; |
1278 |
kakl |
167 |
|
1274 |
kakl |
168 |
case 'l': |
|
|
169 |
open=0; // Lock the dome |
|
|
170 |
break; |
|
|
171 |
} |
|
|
172 |
} |
|
|
173 |
|
1280 |
kakl |
174 |
seq++; // Increment the number of measurement |
1274 |
kakl |
175 |
|
|
|
176 |
tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor |
|
|
177 |
temp=ReadTemp(SA, RAM_Tobj1); |
|
|
178 |
|
|
|
179 |
ta=tempa*2-27315; // °K -> °C |
|
|
180 |
to=temp*2-27315; |
|
|
181 |
|
|
|
182 |
{ // printf |
1282 |
kakl |
183 |
char output[10]; // Output buffer |
1275 |
kakl |
184 |
int8 j; // Counter |
1278 |
kakl |
185 |
|
1282 |
kakl |
186 |
delay(SEND_DELAY); |
|
|
187 |
sprintf(output,"#%Lu ", seq); |
|
|
188 |
j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } |
|
|
189 |
sprintf(output,"%Ld ", ta); |
|
|
190 |
j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } |
|
|
191 |
sprintf(output,"%Ld ", to); |
|
|
192 |
j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } |
|
|
193 |
sprintf(output,"%u", heat); |
|
|
194 |
j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } |
|
|
195 |
sprintf(output,"%u\n\r\0", open); |
|
|
196 |
j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } |
1274 |
kakl |
197 |
} |
1280 |
kakl |
198 |
|
|
|
199 |
delay(MEASURE_DELAY); // Delay to a next measurement |
|
|
200 |
//---WDT |
|
|
201 |
restart_wdt(); |
1274 |
kakl |
202 |
} |
|
|
203 |
} |
|
|
204 |
|