Rev 2314 Rev 2315
1 #include "reflow.h" 1 #include "reflow.h"
2 #include "process.h" 2 #include "process.h"
3 #include <math.h> 3 #include <math.h>
4   4  
5 // nastaveni teplot a casu 5 // nastaveni teplot a casu
6 #define TEPLOTA_PREDEHREVU 120 6 #define TEPLOTA_PREDEHREVU 120
7 #define DOBA_PREDEHREVU 60 7 #define DOBA_PREDEHREVU 60
8   8  
9 #define TEPLOTA_VRCHOLU 210 9 #define TEPLOTA_VRCHOLU 210
10 #define DOBA_VRCHOLU 5 10 #define DOBA_VRCHOLU 5
11   11  
12 // CPU IO rozhrani 12 // CPU IO rozhrani
13 #define LCD_RS PIN_C1 // rizeni registru LCD displeje 13 #define LCD_RS PIN_C1 // rizeni registru LCD displeje
14 #define LCD_E PIN_C2 // enable LCD displeje 14 #define LCD_E PIN_C2 // enable LCD displeje
15 #define LCD_DATA_LSB PIN_D0 // data LCD 15 #define LCD_DATA_LSB PIN_D0 // data LCD
16 #include "lcd.c" 16 #include "lcd.c"
17   17  
18 #define TL1 PIN_B3 // tlacitko S1 18 #define TL1 PIN_B3 // tlacitko S1
19 #define TL2 PIN_B2 // tlacitko S2 19 #define TL2 PIN_B2 // tlacitko S2
20 #define TL3 PIN_B1 // tlacitko S3 20 #define TL3 PIN_B1 // tlacitko S3
21 #define TL4 PIN_B0 // tlacitko S4 21 #define TL4 PIN_B0 // tlacitko S4
22   22  
23 #define POWER_T3 PIN_C4 // ovladani optotriaku T3 23 #define POWER_T3 PIN_C4 // ovladani optotriaku T3
24 #define POWER_T4 PIN_C5 // ovladani optotriaku T4 24 #define POWER_T4 PIN_C5 // ovladani optotriaku T4
25 #define POWER_T5 PIN_C6 // ovladani optotriaku T5 25 #define POWER_T5 PIN_C6 // ovladani optotriaku T5
26   26  
27 #define ADC_PIN PIN_A0 //info, nelze menit - pin pouzit jako input analog 27 #define ADC_PIN PIN_A0 //info, nelze menit - pin pouzit jako input analog
28 #define ADC_PIN_NC PIN_A1 //info, nelze menit - pin pouzit jako input analog 28 #define ADC_PIN_NC PIN_A1 //info, nelze menit - pin pouzit jako input analog
29 #define REF_PIN PIN_A3 //info, nelze menit - pin pouzit jako input reference 2.5V 29 #define REF_PIN PIN_A3 //info, nelze menit - pin pouzit jako input reference 2.5V
30   30  
31 // interni 31 // interni
32 #define PowerOn() output_low(POWER_T4);output_low(POWER_T5) 32 #define PowerOn() output_low(POWER_T4);output_low(POWER_T5)
33 #define PowerOff() output_high(POWER_T4);output_high(POWER_T5) 33 #define PowerOff() output_high(POWER_T4);output_high(POWER_T5)
34   34  
35 // globalni promenne 35 // globalni promenne
36 struct time 36 struct time
37 { 37 {
38 volatile signed int8 hod; 38 volatile signed int8 hod;
39 volatile signed int8 min; 39 volatile signed int8 min;
40 volatile signed int8 sec; 40 volatile signed int8 sec;
41 }cas; 41 }cas;
42   42  
43 unsigned int top_heat_power=0; // range 0-200% nad 100% je ale teleso jiz pretizene 43 unsigned int top_heat_power=0; // range 0-200% nad 100% je ale teleso jiz pretizene
44 unsigned int bottom_heat_power=0; // contains heating power range 0-100% 44 unsigned int bottom_heat_power=0; // contains heating power range 0-100%
45 unsigned int period; 45 unsigned int period;
46   46  
47 float temp_last=0; 47 float temp_last=0;
48 float temp_slope=0; 48 float temp_slope=0;
49   49  
50   50  
51 void GeneralCpuInit() // inicializace 51 void GeneralCpuInit() // inicializace
52 { 52 {
53 output_high(POWER_T4); 53 output_high(POWER_T4);
54 output_high(POWER_T5); 54 output_high(POWER_T5);
55 port_b_pullups(true); 55 port_b_pullups(true);
56 56
57 setup_psp(PSP_DISABLED); 57 setup_psp(PSP_DISABLED);
58 setup_spi(SPI_SS_DISABLED); 58 setup_spi(SPI_SS_DISABLED);
59 59
60 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64); //nepouzit 60 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64); //nepouzit
61 setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // rizeni 61 setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // rizeni
62 setup_timer_2(T2_DIV_BY_16,249,10); //rtc 40ms 62 setup_timer_2(T2_DIV_BY_16,249,10); //rtc 40ms
63 63
64 setup_comparator(NC_NC_NC_NC); 64 setup_comparator(NC_NC_NC_NC);
65 setup_vref(FALSE); 65 setup_vref(FALSE);
66 66
67 enable_interrupts(GLOBAL); 67 enable_interrupts(GLOBAL);
68 enable_interrupts(INT_TIMER2); 68 enable_interrupts(INT_TIMER2);
69 enable_interrupts(INT_TIMER0); 69 enable_interrupts(INT_TIMER0);
70 70
71 setup_adc_ports(AN0_AN1_VSS_VREF); //A0 vstup cidla, A1 nepozit, A3 - ref. 2.5V 71 setup_adc_ports(AN0_AN1_VSS_VREF); //A0 vstup cidla, A1 nepozit, A3 - ref. 2.5V
72 setup_adc(ADC_CLOCK_DIV_8); 72 setup_adc(ADC_CLOCK_DIV_8);
73 SET_ADC_CHANNEL(0); //AN0, PIN_A0 73 SET_ADC_CHANNEL(0); //AN0, PIN_A0
74 } 74 }
75   75  
76 void heat_failure() // exception in case of heating fail 76 void heat_failure() // exception in case of heating fail
77 { 77 {
78 top_heat_power=0; 78 top_heat_power=0;
79 bottom_heat_power=0; 79 bottom_heat_power=0;
80   80  
81 lcd_gotoxy(1,2); 81 lcd_gotoxy(1,2);
82 printf(lcd_putc,"HEATING FAILURE!"); 82 printf(lcd_putc,"HEATING FAILURE!");
83 83
84 while(true); 84 while(true);
85   85  
86 } 86 }
87   87  
88 unsigned int16 adc(void) // adc read and filtering 88 unsigned int16 adc(void) // adc read and filtering
89 { 89 {
90 unsigned int16 analog; 90 unsigned int16 analog;
91 unsigned int8 a; 91 unsigned int8 a;
92   92  
93 analog = 0; 93 analog = 0;
94 for (a=0;a<32;a++) 94 for (a=0;a<32;a++)
95 { 95 {
96 analog += read_adc(); 96 analog += read_adc();
97 delay_us(50); 97 delay_us(50);
98 } 98 }
99 return (analog >> 5 ); // prumer = analog/32 99 return (analog >> 5 ); // prumer = analog/32
100 } 100 }
101   101  
102 float teplota(void) // temperature measuring 102 float teplota(void) // temperature measuring
103 { 103 {
104 return (0.674201*adc() - 294.35); // temperature calculaton (linear aproximation) 104 return (0.674201*adc() - 294.35); // temperature calculaton (linear aproximation)
105 } 105 }
106   106  
107 void top_heating() 107 void top_heating()
108 { 108 {
109 if (period < top_heat_power){ 109 if (period < top_heat_power){
110 output_low(POWER_T4); 110 output_low(POWER_T4);
111 output_low(POWER_T5); 111 output_low(POWER_T5);
112 } 112 }
113 else{ 113 else{
114 output_high(POWER_T4); 114 output_high(POWER_T4);
115 output_high(POWER_T5); 115 output_high(POWER_T5);
116 } 116 }
117 } 117 }
118   118  
119 void bottom_heating() 119 void bottom_heating()
120 { 120 {
121   121  
122 if (period < 2*bottom_heat_power){ 122 if (period < 2*bottom_heat_power){
123 output_low(POWER_T3); 123 output_low(POWER_T3);
124 } 124 }
125 else{ 125 else{
126 output_high(POWER_T3); 126 output_high(POWER_T3);
127 } 127 }
128 } 128 }
129   129  
130 #int_TIMER0 130 #int_TIMER0
131 void heating_control() //rizeni topnych teles pri preteceni casovace 131 void heating_control() //rizeni topnych teles pri preteceni casovace
132 { 132 {
133 float temp; 133 float temp;
134   134  
135 top_heating(); 135 top_heating();
136 bottom_heating(); 136 bottom_heating();
137 137
138 138
139 if ((period == 100) || (period == 0)) 139 if ((period == 100) || (period == 0))
140 { 140 {
141 temp=teplota(); 141 temp=teplota();
142 temp_slope=(temp - temp_last) /(100.0*256.0/62500.0); // vypocet strmosti narustu teploty ve stupnich/s 142 temp_slope=(temp - temp_last) /(100.0*256.0/62500.0); // vypocet strmosti narustu teploty ve stupnich/s
143 temp_last = temp; 143 temp_last = temp;
144 printf("%02u %02u %3.3f \r\n",cas.min,cas.sec,temp); //vypis pro zaznam profilu 144 printf("%02u %02u %3.3f \r\n",cas.min,cas.sec,temp); //vypis pro zaznam profilu
145 } 145 }
146 146
147 if (period < 200) period++; 147 if (period < 200) period++;
148 else period=0; 148 else period=0;
149 } 149 }
150   150  
151 #int_TIMER2 151 #int_TIMER2
152 void Rtc(void) //40ms 152 void Rtc(void) //40ms
153 { 153 {
154 static unsigned int8 ms40=0; 154 static unsigned int8 ms40=0;
155 struct time* time; 155 struct time* time;
156 156
157 time=&cas; 157 time=&cas;
158 if ( ++ms40 < 25) return; 158 if ( ++ms40 < 25) return;
159 159
160 ms40=0; 160 ms40=0;
161 if (++(time->sec) >= 60) 161 if (++(time->sec) >= 60)
162 { 162 {
163 time->sec=0; //1min 163 time->sec=0; //1min
164 if (++(time->min) >= 60) 164 if (++(time->min) >= 60)
165 { 165 {
166 time->min = 0; //1hod 166 time->min = 0; //1hod
167 (time->hod)++; 167 (time->hod)++;
168 } 168 }
169 } 169 }
170 } 170 }
171   171  
172 void slope_control(float ramp, unsigned int balance) // P proporcionalni rizeni narustu teploty predpoklada periodicke volani 1x/s 172 void slope_control(float ramp, unsigned int balance) // P proporcionalni rizeni narustu teploty predpoklada periodicke volani 1x/s
173 { 173 {
174 float slope_deviation; 174 float slope_deviation;
175   175  
176 slope_deviation = temp_slope - ramp; // vypocet strmosti a odchylky od pozadovane strmosti 176 slope_deviation = temp_slope - ramp; // vypocet strmosti a odchylky od pozadovane strmosti
177   177  
178 if(slope_deviation < 0) 178 if(slope_deviation < 0)
179 { 179 {
180 top_heat_power= 60 + balance; 180 top_heat_power= 60 + balance;
181 bottom_heat_power= 100; 181 bottom_heat_power= 100;
182 } 182 }
183 else{ 183 else{
184 top_heat_power=0; 184 top_heat_power=0;
185 bottom_heat_power=0; 185 bottom_heat_power=0;
186 } 186 }
187 } 187 }
188   188  
189 void level_control(float level) // P proporcionalni rizeni teploty 189 void level_control(float level) // P proporcionalni rizeni teploty
190 { 190 {
191 if (teplota() > level) 191 if (teplota() > level)
192 { 192 {
193 top_heat_power=0; 193 top_heat_power=0;
194 bottom_heat_power=0; 194 bottom_heat_power=0;
195 } 195 }
196 else 196 else
197 { 197 {
198 top_heat_power=70; 198 top_heat_power=70;
199 bottom_heat_power=80; 199 bottom_heat_power=80;
200 } 200 }
201 } 201 }
202   202  
203   203  
204 void nullcas(struct time* time) 204 void nullcas(struct time* time)
205 { 205 {
206 disable_interrupts(INT_TIMER2); 206 disable_interrupts(INT_TIMER2);
207 207
208 time->sec=0; 208 time->sec=0;
209 time->hod=0; 209 time->hod=0;
210 time->min=0; 210 time->min=0;
211 211
212 enable_interrupts(INT_TIMER2); 212 enable_interrupts(INT_TIMER2);
213 } 213 }
214   214  
215 void reflow_solder() 215 void reflow_solder()
216 { 216 {
217   217  
218 struct time process_time; 218 struct time process_time;
219   219  
220 // ------------------- PREHEAT --------------------- 220 // ------------------- PREHEAT ---------------------
221   221  
222 nullcas(&cas); 222 nullcas(&cas);
223 lcd_gotoxy(1,2); 223 lcd_gotoxy(1,2);
224 printf(lcd_putc,"PREHEAT"); 224 printf(lcd_putc,"PREHEAT");
225 printf("#PREHEAT\r\n"); 225 printf("#PREHEAT\r\n");
226   226  
227 do { 227 do {
228 slope_control(PREHEAT_SLOPE, 0); // hlida strmost predehrevu 228 slope_control(PREHEAT_SLOPE, 0); // hlida strmost predehrevu
229   229  
230 lcd_gotoxy(1,1); 230 lcd_gotoxy(1,1);
231 printf(lcd_putc,"%3.1f\21C ",teplota()); 231 printf(lcd_putc,"%3.1f\21C ",teplota());
232   232  
233 lcd_gotoxy(12,1); 233 lcd_gotoxy(12,1);
234 printf(lcd_putc,"%02u:%02u",cas.min,cas.sec); 234 printf(lcd_putc,"%02u:%02u",cas.min,cas.sec);
235   235  
236 lcd_gotoxy(10,2); 236 lcd_gotoxy(10,2);
237 printf(lcd_putc,"%1.1f\21C/s ",temp_slope); 237 printf(lcd_putc,"%1.1f\21C/s ",temp_slope);
238   238  
239 delay_ms(200); 239 delay_ms(200);
240 if (cas.min>3) heat_failure(); 240 if (cas.min>3) heat_failure();
241 } 241 }
242 while (teplota() < SOAK_TEMP); 242 while (teplota() < SOAK_TEMP);
243   243  
244 // ----------- SOAK --------------- 244 // ----------- SOAK ---------------
245 nullcas(&cas); 245 nullcas(&cas);
246 process_time.min = SOAK_TIME/60; 246 process_time.min = SOAK_TIME/60;
247 process_time.sec = SOAK_TIME - process_time.min*60; 247 process_time.sec = SOAK_TIME - process_time.min*60;
248   248  
249 lcd_clr(); 249 lcd_clr();
250 lcd_gotoxy(1,2); 250 lcd_gotoxy(1,2);
251 printf(lcd_putc,"SOAK "); 251 printf(lcd_putc,"SOAK ");
252 printf("#SOAK\r\n"); 252 printf("#SOAK\r\n");
253   253  
254 while (process_time.sec!=0 || process_time.min!=0) 254 while (process_time.sec!=0 || process_time.min!=0)
255 { 255 {
256 level_control(SOAK_TEMP); 256 level_control(SOAK_TEMP);
257   257  
258 lcd_gotoxy(1,1); 258 lcd_gotoxy(1,1);
259 printf(lcd_putc,"%3.1f\21C ",teplota()); 259 printf(lcd_putc,"%3.1f\21C ",teplota());
260   260  
261 if ((process_time.sec = process_time.sec - cas.sec)<0) process_time.sec=59; 261 if ((process_time.sec = process_time.sec - cas.sec)<0) process_time.sec=59;
262   262  
263 process_time.min = (SOAK_TIME - cas.min*60 - cas.sec)/60; 263 process_time.min = (SOAK_TIME - cas.min*60 - cas.sec)/60;
264 process_time.sec = (SOAK_TIME - cas.min*60 - cas.sec) - process_time.min*60; 264 process_time.sec = (SOAK_TIME - cas.min*60 - cas.sec) - process_time.min*60;
265   265  
266 lcd_gotoxy(9,1); 266 lcd_gotoxy(9,1);
267 printf(lcd_putc,"%02u:%02u", process_time.min, process_time.sec); 267 printf(lcd_putc,"%02u:%02u", process_time.min, process_time.sec);
268 delay_ms(200); 268 delay_ms(200);
269 } 269 }
270 270
271 //----------------- solder ---------------------------- 271 //----------------- solder ----------------------------
272   272  
273 nullcas(&cas); 273 nullcas(&cas);
274 lcd_clr(); 274 lcd_clr();
275 lcd_gotoxy(1,2); 275 lcd_gotoxy(1,2);
276 printf(lcd_putc,"SOLDER"); 276 printf(lcd_putc,"SOLDER");
277 printf("#SOLDER\r\n"); 277 printf("#SOLDER\r\n");
278   278  
279 do { 279 do {
280 slope_control(SOLDER_SLOPE, 10); // hlida strmost predehrevu 280 slope_control(SOLDER_SLOPE, 10); // hlida strmost predehrevu
281   281  
282 lcd_gotoxy(1,1); 282 lcd_gotoxy(1,1);
283 printf(lcd_putc,"%3.1f\21C ",teplota()); 283 printf(lcd_putc,"%3.1f\21C ",teplota());
284   284  
285 lcd_gotoxy(12,1); 285 lcd_gotoxy(12,1);
286 printf(lcd_putc,"%02u:%02u",cas.min,cas.sec); 286 printf(lcd_putc,"%02u:%02u",cas.min,cas.sec);
287   287  
288 lcd_gotoxy(10,2); 288 lcd_gotoxy(10,2);
289 printf(lcd_putc,"%1.1f\21C/s ",temp_slope); 289 printf(lcd_putc,"%1.1f\21C/s ",temp_slope);
290   290  
291 delay_ms(200); 291 delay_ms(200);
292 if (cas.min>2) heat_failure(); 292 if (cas.min>2) heat_failure();
293 } 293 }
294 while (teplota() < SOLDER_TEMP); 294 while (teplota() < SOLDER_TEMP);
295   295  
296 // ---------------- TAO ------------------------ 296 // ---------------- TAO ------------------------
297   297  
298   298  
299 while (process_time.sec!=0 || process_time.min!=0) 299 while (process_time.sec!=0 || process_time.min!=0)
300 { 300 {
301 level_control(SOLDER_TEMP); 301 level_control(SOLDER_TEMP);
302   302  
303 lcd_gotoxy(1,1); 303 lcd_gotoxy(1,1);
304 printf(lcd_putc,"%3.1f\21C ",teplota()); 304 printf(lcd_putc,"%3.1f\21C ",teplota());
305   305  
306 if ((process_time.sec = process_time.sec - cas.sec)<0) process_time.sec=59; 306 if ((process_time.sec = process_time.sec - cas.sec)<0) process_time.sec=59;
307   307  
308 process_time.min = (SOLDER_TIME - cas.min*60 - cas.sec)/60; 308 process_time.min = (SOLDER_TIME - cas.min*60 - cas.sec)/60;
309 process_time.sec = (SOLDER_TIME - cas.min*60 - cas.sec) - process_time.min*60; 309 process_time.sec = (SOLDER_TIME - cas.min*60 - cas.sec) - process_time.min*60;
310   310  
311 lcd_gotoxy(9,1); 311 lcd_gotoxy(9,1);
312 printf(lcd_putc,"%02u:%02u", process_time.min, process_time.sec); 312 printf(lcd_putc,"%02u:%02u", process_time.min, process_time.sec);
313 313
314 delay_ms(200); 314 delay_ms(200);
315 315
316 } 316 }
317   317  
318 // ---------------- COOLING ------------------------ 318 // ---------------- COOLING ------------------------
319   319  
320 top_heat_power=0; 320 top_heat_power=0;
321 bottom_heat_power=0; 321 bottom_heat_power=0;
322   322  
323 lcd_clr(); 323 lcd_clr();
324   324  
325 lcd_gotoxy(1,2); 325 lcd_gotoxy(1,2);
326 printf(lcd_putc,"REFLOW COMPLETE"); 326 printf(lcd_putc,"REFLOW COMPLETE");
327 printf("COOLING \r\n"); 327 printf("COOLING \r\n");
328   328  
329   329  
330 while(true) 330 while(true)
331 { 331 {
332 lcd_gotoxy(1,1); 332 lcd_gotoxy(1,1);
333 printf(lcd_putc,"%3.1f\21C ",teplota()); 333 printf(lcd_putc,"%3.1f\21C ",teplota());
334   334  
335 lcd_gotoxy(10,1); 335 lcd_gotoxy(10,1);
336 printf(lcd_putc,"%1.1f\21C/s ",temp_slope); 336 printf(lcd_putc,"%1.1f\21C/s ",temp_slope);
337 } 337 }
338 } 338 }
339   339  
340 void main() // main loop 340 void main() // main loop
341 { 341 {
342 GeneralCpuInit(); 342 GeneralCpuInit();
343 PowerOff(); 343 PowerOff();
344 344
345 lcd_init(); 345 lcd_init();
346 lcd_define_char(1,LCD_CHAR_STUPEN); 346 lcd_define_char(1,LCD_CHAR_STUPEN);
347 347
348 nullcas(&cas); 348 nullcas(&cas);
349 349
350 while(true) 350 while(true)
351 { 351 {
352 delay_ms(300); 352 delay_ms(300);
353 reflow_solder(); 353 reflow_solder();
354 354
355 } 355 }
356 } 356 }