Line 1... |
Line 1... |
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 |
Line 22... |
Line 22... |
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) |
Line 51... |
Line 59... |
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); |
Line 92... |
Line 102... |
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); |
Line 141... |
Line 146... |
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) |
Line 163... |
Line 168... |
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 |
|
Line 178... |
Line 192... |
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); |