Rev 1285 Rev 1286
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.0"
3 #define ID "$Id: irmrak4.c 1285 2009-01-08 00:31:13Z kakl $" 3 #define ID "$Id: irmrak4.c 1286 2009-01-08 00:34:14Z 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);} // Toggle = Open Dome 30 {output_toggle(DOME);} // Toggle = Open Dome
31 else 31 else
32 {output_high(DOME);} // Do not toggle = Close 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_high(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 seq=0; // Variables initiation 108 seq=0; // Variables initiation
109 heat=0; 109 heat=0;
110 open=0; 110 open=0;
111   111  
112 welcome(); 112 welcome();
113   113  
114 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read 114 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
115 temp=ReadTemp(SA, RAM_Tobj1); 115 temp=ReadTemp(SA, RAM_Tobj1);
116   116  
-   117 delay_ms(1000);
117 //---WDT 118 //---WDT
118 restart_wdt(); 119 restart_wdt();
119   120  
120 while(TRUE) // Main Loop 121 while(TRUE) // Main Loop
121 { 122 {
122 while(kbhit()) getc(); // Flush USART buffer 123 while(kbhit()) getc(); // Flush USART buffer
123 CREN=0; CREN=1; // Reinitialise USART 124 CREN=0; CREN=1; // Reinitialise USART
124   125  
125 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down 126 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down
126 do 127 do
127 { 128 {
128 if (safety_counter<SAFETY_COUNT) safety_counter++; 129 if (safety_counter<SAFETY_COUNT) safety_counter++;
129   130  
130 delay(RESPONSE_DELAY); 131 delay(RESPONSE_DELAY);
131   132  
132 if (safety_counter>=SAFETY_COUNT) 133 if (safety_counter>=SAFETY_COUNT)
133 { 134 {
134 if (heat>0) 135 if (heat>0)
135 { 136 {
136 output_high(HEATING); 137 output_high(HEATING);
137 heat--; 138 heat--;
138 } 139 }
139 else 140 else
140 { 141 {
141 output_low(HEATING); 142 output_low(HEATING);
142 } 143 }
143   144  
144 if (open>0) open--; 145 if (open>0) open--;
145   146  
146 safety_counter=0; 147 safety_counter=0;
147 //---WDT 148 //---WDT
148 restart_wdt(); 149 restart_wdt();
149 } 150 }
150 } while (!kbhit()); 151 } while (!kbhit());
151   152  
152 //---WDT 153 //---WDT
153 restart_wdt(); 154 restart_wdt();
154 { // Retrieve command 155 { // Retrieve command
155 char ch; 156 char ch;
156   157  
157 ch=getc(); 158 ch=getc();
158   159  
159 switch (ch) 160 switch (ch)
160 { 161 {
161 case 'h': 162 case 'h':
162 heat=MAXHEAT; // Need heating 163 heat=MAXHEAT; // Need heating
163 break; 164 break;
164   165  
165 case 'c': 166 case 'c':
166 heat=0; // Need colder 167 heat=0; // Need colder
167 break; 168 break;
168   169  
169 case 'o': 170 case 'o':
170 open=MAXOPEN; // Open the dome 171 open=MAXOPEN; // Open the dome
171 break; 172 break;
172   173  
173 case 'x': 174 case 'x':
174 open=MAXOPEN; // Open the dome 175 open=MAXOPEN; // Open the dome
175 heat=MAXHEAT; // Need heating 176 heat=MAXHEAT; // Need heating
176 break; 177 break;
177   178  
178 case 'l': 179 case 'l':
179 open=0; // Lock the dome 180 open=0; // Lock the dome
180 break; 181 break;
181   182  
182 case 'i': 183 case 'i':
183 welcome(); // Information about version, etc... 184 welcome(); // Information about version, etc...
184 break; 185 break;
185 } 186 }
186 } 187 }
187   188  
188 seq++; // Increment the number of measurement 189 seq++; // Increment the number of measurement
189   190  
190 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor 191 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
191 temp=ReadTemp(SA, RAM_Tobj1); 192 temp=ReadTemp(SA, RAM_Tobj1);
192   193  
193 ta=tempa*2-27315; // °K -> °C 194 ta=tempa*2-27315; // °K -> °C
194 to=temp*2-27315; 195 to=temp*2-27315;
195   196  
196 { // printf 197 { // printf
197 char output[8]; // Output buffer 198 char output[8]; // Output buffer
198 int8 j; // String pointer 199 int8 j; // String pointer
199   200  
200 delay(SEND_DELAY); 201 delay(SEND_DELAY);
201 sprintf(output,"#%Lu ", seq); 202 sprintf(output,"#%Lu ", seq);
202 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } 203 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
203 sprintf(output,"%Ld ", ta); 204 sprintf(output,"%Ld ", ta);
204 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } 205 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
205 sprintf(output,"%Ld ", to); 206 sprintf(output,"%Ld ", to);
206 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++]); }
207 sprintf(output,"%u ", heat); 208 sprintf(output,"%u ", heat);
208 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++]); }
209 sprintf(output,"%u\n\r\0", open); 210 sprintf(output,"%u\n\r\0", open);
210 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++]); }
211 } 212 }
212   213  
213 delay(MEASURE_DELAY); // Delay to a next measurement 214 delay(MEASURE_DELAY); // Delay to a next measurement
214 //---WDT 215 //---WDT
215 restart_wdt(); 216 restart_wdt();
216 } 217 }
217 } 218 }
218   219