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