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