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