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