Rev Author Line No. Line
1384 mija 1 /* mija 2009 - process
2  
3 SW pro ATmega8 mereni + regulace teploty
4 Fosc = 8192000Hz externi krystal
5  
6  
7 ver.: 0.0 - start
8 */
9  
10 #include <avr/io.h>
11 #include <stdio.h>
12 #include <math.h>
13  
14 #include "process.h"
15 #include "key.h"
16 #include "lcd.h"
17 #include "clock.h"
18  
19  
20 uint8_t ProcesStart(char *buffer,uint8_t key,uint16_t temp)
21 {
22 static uint8_t proces = SOLDER;
23 static uint8_t displ = 1;
24 static uint16_t LastTemp;
25  
26 HeatOff();
27 if (key == TL1){
28 displ = 1;
29 return proces;
30 }
31 if ((key == TL2)){
32 switch (proces)
33 {
34 case POSITIV: proces = DRY;
35 break;
36 case DRY: proces = SOLDER;
37 break;
38 case SOLDER:
39 default: proces = POSITIV;
40 break;
41 }
42 displ = 1;
43 }
44 if (displ){
45 switch(proces)
46 {
47 case POSITIV:sprintf(buffer,"\fPOSITIV\n%3dC ch",temp);
48 break;
49 case DRY: sprintf(buffer,"\fDRY\n%3dC ch",temp);
50 break;
51 case SOLDER:
52 default: sprintf(buffer,"\fSOLDER\n%3dC ch",temp);
53 }
54 displ = 0;
55 }
56 else{
57 if (temp !=LastTemp){
58 sprintf(buffer,"\n%3d",temp);
59 LastTemp=temp;
60 }
61 }
62 return START;
63 }
64  
65 /*uint8_t ProcesSolder(char *buffer,uint8_t key,uint16_t temp,Clockt_t *clock)
66 {
67 enum{SOLDER_START,SOLDER_PREHEAT,SOLDER_SOLDER,SOLDER_COOL};
68 static uint8_t stav=SOLDER_START;
69 static uint8_t timer=PREHEATTIME;
70 static uint8_t TimerFlag=0;
71 static uint8_t PrevSec;
72  
73 if (key != NOKEY){
74 stav = SOLDER_START;
75 return START;
76 }
77 switch(stav)
78 {
79 case SOLDER_START:
80 timer = PREHEATTIME+1;
81 TimerFlag = 0;
82 stav = SOLDER_PREHEAT;
83 ClockInit(0,0,0,clock);
84 PrevSec = 0xff;
85 sprintf(buffer,"\f");
86 break;
87 case SOLDER_PREHEAT:
88 if(temp >= PREHEATTEMPSTART) TimerFlag=1;
89 ProcesHeat(PREHEATTEMP,temp);
90 if (!timer){
91 stav = SOLDER_SOLDER;
92 timer = SOLDERTIME+1;
93 TimerFlag = 0;
94 PrevSec = 0xff;
95 }
96 sprintf(buffer,"\rpreheat\n%3dC %3ds",temp,timer-1);
97 break;
98 case SOLDER_SOLDER:
99 if(temp >= SOLDERTEMP) TimerFlag=1;
100 ProcesHeat(SOLDERTEMP,temp);
101 if (!timer){
102 stav = SOLDER_COOL;
103 timer = COOLTIME+1;
104 TimerFlag = 0;
105 PrevSec = 0xff;
106 }
107 sprintf(buffer,"\rsolder \n%3dC %3ds",temp,timer-1);
108 break;
109 case SOLDER_COOL:
110 HeatOff();
111 TimerFlag=1;
112 if (!timer){
113 stav = SOLDER_START;
114 PrevSec = 0xff;
115 return START;
116 }
117 sprintf(buffer,"\rcool \n%3dC %3ds",temp,timer-1);
118 break;
119 default: HeatOff();
120 stav = SOLDER_START;
121 return START;
122 }
123 if ((PrevSec != clock->sec) && TimerFlag){
124 PrevSec = clock->sec;
125 timer --;
126 }
127 return SOLDER;
128 }*/
129  
130 uint8_t ProcesSolder(char *buffer,uint8_t key,uint16_t temp,Clockt_t *clock)
131 {
132 enum{SOLDER_START,SOLDER_PREHEAT,SOLDER_SOLDER,SOLDER_COOL};
133 static uint8_t stav=SOLDER_START;
134 static uint8_t timer=PREHEATTIME;
135 static uint8_t Timer2=0;
136 static uint8_t TimerFlag2=0;
137 static uint8_t TimerFlag=0;
138 static uint8_t FlagSolder=0;
139 static uint8_t PrevSec;
140 static uint16_t LastTemp=0;
141  
142 if (key != NOKEY){
143 stav = SOLDER_START;
144 return START;
145 }
146 switch(stav)
147 {
148 case SOLDER_START:
149 timer = 0;
150 TimerFlag = 0;
151 Timer2 = 0;
152 TimerFlag2 = 0;
153 stav = SOLDER_PREHEAT;
154 ClockInit(0,0,0,clock);
155 PrevSec = 0xff;
156 LastTemp = 0;
157 sprintf(buffer,"\f");
158 break;
159 case SOLDER_PREHEAT:
160 if(temp >= PREHEATTEMPSTART) TimerFlag=1;
161 if(temp >= PREHEATTEMP) TimerFlag2 = 1;
162 if(temp >= PREHEATTEMPSTOP){
163 stav = SOLDER_SOLDER;
164  
165 TimerFlag = 0;
166 FlagSolder = 0;
167 TimerFlag2 = 0;
168 Timer2 = timer;
169 timer=0;
170 break;
171 }
172 if (TimerFlag2 && (Timer2 < PREHEATTIMEOFF)){
173 if (LastTemp > temp) HeatOn();
174 else {
175 HeatOff();
176 if ((clock->sec & 0x4) == 0x4) LastTemp = temp; //4sec vzorkovani;
177 }
178 //LastTemp=temp;
179 }
180 else HeatOn();
181  
182 sprintf(buffer,"\rpreheat\n%3dC %3ds",temp,timer);
183 break;
184 case SOLDER_SOLDER:
185 if(temp >= SOLDERTEMPSTART) TimerFlag=1;
186 if(temp >= SOLDERTEMP) FlagSolder = 1;
187 if (FlagSolder){
188 HeatOff();
189 if(temp <= SOLDERTEMPSTART){
190 stav = SOLDER_COOL;
191 timer=0x1;
192 TimerFlag = 0;
193 break;
194 }
195 }
196 else HeatOn();
197 if (TimerFlag) Timer2=timer;
198 sprintf(buffer,"\rsolder \n%3dC %3ds",temp,Timer2);
199 break;
200 case SOLDER_COOL:
201 HeatOff();
202 TimerFlag=1;
203 if (!timer){
204 stav = SOLDER_START;
205 //PrevSec = 0xff;
206 return START;
207 }
208 sprintf(buffer,"\rcool \n%3dC",temp);
209 break;
210 default: HeatOff();
211 stav = SOLDER_START;
212 return START;
213 }
214 if ((PrevSec != clock->sec)){
215 PrevSec = clock->sec;
216 if (TimerFlag)timer ++;
217 if (TimerFlag2) Timer2 ++;
218 }
219  
220 return SOLDER;
221 }
222  
223 uint8_t ProcesPositiv(char *buffer,uint8_t key,uint16_t temp,Clockt_t *clock)
224 {
225 static uint8_t time = 0;
226 static uint8_t PrevSec;
227  
228 if (!time){
229 time = POSITIVTIME+1;
230 ClockInit(0,0,0,clock);
231 PrevSec = 1;
232 }
233 ProcesHeat(POSITIVTEMP,temp);
234 if (key == TL1) time +=5;
235 if (key == TL2) {
236 time = 0;
237 return START;
238 }
239 if (PrevSec != clock->sec){
240 PrevSec = clock->sec;
241 if (PrevSec == 0) time --;
242 }
243 sprintf(buffer,"\n%3dC %3dm",temp,time);
244 if (!time) return START;
245 return POSITIV;
246 }
247  
248 uint8_t ProcesDry(char *buffer,uint8_t key,uint16_t temp,Clockt_t *clock)
249 {
250 static uint8_t time;
251 static uint8_t PrevSec;
252  
253 if (!time){
254 time = DRYTIME+1;
255 ClockInit(0,0,0,clock);
256 PrevSec = 1;
257 }
258 ProcesHeat(DRYTEMP,temp);
259 if (key == TL1) time +=5;
260 if (key == TL2) {
261 time = 0;
262 return START;
263 }
264 if (PrevSec != clock->sec){
265 PrevSec = clock->sec;
266 if (PrevSec == 0) time --;
267 }
268 sprintf(buffer,"\n%3dC %3dm",temp,time);
269 if (!time) return START;
270 return DRY;
271 }
272  
273 /*
274 uint8_t ProcesTest(char *buffer,uint8_t key,uint16_t temp,Clockt_t *clock)
275 {
276  
277 HeatOff();
278 sprintf(buffer,"\r%2d:%02d:%02d\n%3dC ",clock->hour,clock->min,clock->sec,temp);
279  
280 if (key == TL2) return START;
281 return TEST;
282 }*/
283  
284 void ProcesDispl(char *data)
285 {
286 uint8_t a,b;
287  
288 for (a=0;a<MAXDISPLBUFFER;a++)
289 {
290 switch (*(data+a))
291 {
292 case 0: *data = 0;return;
293 case '\n': LCD_gotoxy(1,2);break;
294 case '\r': LCD_gotoxy(1,1);break;
295 case '\f': LCD_gotoxy(1,1);
296 for (b=0;b<8;b++) LCD_putc(0x20);
297 LCD_gotoxy(1,2);
298 for (b=0;b<8;b++) LCD_putc(0x20);
299 LCD_gotoxy(1,1);
300 break;
301 default : LCD_putc(*(data+a));
302 }
303 }
304 *data = 0;
305 }
306  
307 void ProcesTemp(uint16_t adc,uint16_t *temp)
308 {
309 double Rpt;
310 double Tpt;
311  
312 Rpt = 1.0*adc*2350.0/1024.0;
313  
314 #define A 3.90802e-3
315 #define B -5.802e-7
316  
317 Tpt = (-A + sqrt(A*A - 4*B*(1.0 - Rpt/1000.0)))/2.0/B;
318 //Tpt = (-A + sqrt(A*A - 4*B*(1.0 - Rpt)))/2.0/B;
319 //Tpt = (Rpt/1000.0 - 1.0)/A;
320  
321 *temp = (int16_t)Tpt;
322 }
323  
324 /*
325 void ProcesHeat(uint16_t temp,uint16_t ActualTemp)
326 {
327 enum{HEAT_STABLE,HEAT_UP_1,HEAT_UP_2,HEAT_UP_3,HEAT_UP_4,HEAT_UP_5,HEAT_DOWN_1,HEAT_DOWN_2,HEAT_DOWN_3,HEAT_DOWN_4,HEAT_DOWN_5};
328 static uint8_t stav = HEAT_STABLE;
329 static uint8_t PrevTemp = 0;
330 static uint8_t TimeChange=0;
331  
332 if (ActualTemp < (temp)
333  
334 switch(stav)
335 {
336 case HEAT_STABLE:
337 if (ActualTemp>temp) HeatOff();
338 else HeatOn();
339 break;
340 case HEAT_UP_1:
341 break;
342 case HEAT_UP_2:
343 break;
344 case HEAT_UP_3:
345 break;
346 case HEAT_UP_4:
347 break;
348 case HEAT_UP_5:
349 break;
350 case HEAT_DOWN_1:
351 break;
352 case HEAT_DOWN_2:
353 break;
354 case HEAT_DOWN_3:
355 break;
356 case HEAT_DOWN_4:
357 break;
358 case HEAT_DOWN_5:
359 break;
360 }
361 if (ActualTemp>temp) HeatOff();
362 else HeatOn();
363 }
364 */
365 /*
366 void ProcesHeat(uint16_t GequestTemp,uint16_t ActualTemp)
367 {
368  
369 #define TERM_STOP 15 //teplotni rozdil do regulace - trouba topi
370 #define T1 20 //vzorkovani pøi nabìhu teploty [s]
371 #define T2 10 //vzorkovani pøi poklesu teploty [s]
372 #define T3 5 //vzorkovani pøi ustalene teplotì [s]
373 #define T4 5 //doba topení [s]
374 #define MAINCYKLSPEED 100
375  
376 static uint8_t delta_s=0;
377 static uint16_t T_LAST=0;
378 static uint8_t clock=MAINCYKLSPEED; //pocet opakovani teto fce za 1 sekundu
379  
380  
381  
382 if (!(-- clock)) {delta_s++;clock = MAINCYKLSPEED;}
383  
384 if( ActualTemp < (GequestTemp - TERM_STOP)){
385 HeatOn();
386 delta_s=0;
387 T_LAST=ActualTemp;
388 }
389 else{
390 if (ActualTemp < GequestTemp){
391 if (ActualTemp == T_LAST){
392 if (delta_s < T1) HeatOff();
393 else HeatOn();
394 if (delta_s > (T1 + T4)) delta_s = 0;
395 }
396 else{
397 if (T_LAST > ActualTemp) delta_s=(T1 - T2);
398 else delta_s=0;
399 T_LAST=ActualTemp;HeatOff();
400 }
401 }
402 else{
403 HeatOff();T_LAST=ActualTemp;
404 if (ActualTemp == GequestTemp) delta_s = (T1 - T3);
405 else delta_s = 0;
406 }
407 }
408 }
409 */
410 /*
411 void ProcesHeat(uint16_t RequestTemp,uint16_t ActualTemp)
412 {
413  
414 #define TERM_STOP 15 //teplotni rozdil do regulace - trouba topi
415 #define T1 20 //vzorkovani pøi nabìhu teploty [s]
416 #define T2 10 //vzorkovani pøi poklesu teploty [s]
417 #define T3 5 //vzorkovani pøi ustalene teplotì [s]
418 #define T4 5 //doba topení [s]
419 #define PERIOD 15 //[s]
420 #define MAINCYKLSPEED 100
421  
422 static uint8_t delta_s=0;
423 static uint16_t LastTemp=0;
424 static uint8_t clock=MAINCYKLSPEED; //pocet opakovani teto fce za 1 sekundu
425 static uint8_t Pwm=0;
426 static uint8_t DutyPwm = 0;
427 static uint8_t FlagTemp = 1;
428 static uint8_t FlagPwm = 0;
429  
430  
431  
432 if (!(-- clock)){
433 delta_s++;
434 clock = MAINCYKLSPEED;
435 if ((++Pwm) > PERIOD){
436 Pwm = 0;
437 FlagPwm = 1;
438 }
439 }
440  
441 if( ActualTemp < (RequestTemp - TERM_STOP)){
442 DutyPwm = PERIOD;
443 LastTemp = ActualTemp;
444 FlagTemp=1;
445 }
446 else{
447 if (FlagTemp){
448 DutyPwm=0;
449 FlagTemp = 0;
450 }
451 else
452 if (FlagPwm)
453 {
454 FlagPwm=0;
455 if (ActualTemp >= RequestTemp){
456 if(LastTemp < ActualTemp ) { if (DutyPwm) DutyPwm --;}
457 }
458 else{
459 if (LastTemp >= ActualTemp) {if (DutyPwm<PERIOD)DutyPwm ++;}
460 }
461 LastTemp = ActualTemp;
462 }
463 }
464  
465 if(ActualTemp<RequestTemp){
466 if (DutyPwm >= Pwm) HeatOn();
467 else HeatOff();
468 }
469 else HeatOff();
470  
471 }*/
472  
473 void ProcesHeat(uint16_t RequestTemp,uint16_t ActualTemp)
474 {
475  
476 #define TERM_STOP 15 //teplotni rozdil do regulace - trouba topi
477  
478 #define PERIOD 15 //[s]
479 #define MAINCYKLSPEED 100
480  
481 static uint8_t delta_s=0;
482 static uint16_t LastTemp=0;
483 static uint8_t clock=MAINCYKLSPEED; //pocet opakovani teto fce za 1 sekundu
484 static uint8_t Pwm=0;
485 static uint8_t DutyPwm = 0;
486 static uint8_t FlagTemp = 1;
487 static uint8_t FlagPwm = 0;
488  
489  
490  
491 if (!(-- clock)){
492 clock = MAINCYKLSPEED;
493 if ((++Pwm) > PERIOD){
494 Pwm = 0;
495 FlagPwm = 1;
496 }
497 }
498  
499 if( ActualTemp < (RequestTemp - TERM_STOP)){
500 DutyPwm = PERIOD;
501 LastTemp = ActualTemp;
502 FlagTemp=1;
503 }
504 else{
505 if (FlagTemp){
506 DutyPwm=0;
507 FlagTemp = 0;
508 }
509 else
510 if (FlagPwm)
511 {
512 FlagPwm=0;
513 if (ActualTemp >= RequestTemp){
514 if (ActualTemp != RequestTemp)
515 if(LastTemp < ActualTemp ){
516 //if ((ActualTemp-LastTemp) > 1)
517 if (DutyPwm) DutyPwm --;
518 }
519 }
520 else{
521 if (LastTemp >= ActualTemp){
522 //if ((ActulaTemp - LastTemp)>1)
523 if (DutyPwm<PERIOD)DutyPwm ++;
524 }
525 if(LastTemp < ActualTemp ){
526 if ((ActualTemp-LastTemp) > 5)
527 if (DutyPwm) DutyPwm --;
528 }
529 }
530 LastTemp = ActualTemp;
531 }
532 }
533  
534 if(ActualTemp < RequestTemp)
535 {
536 if (DutyPwm == PERIOD) HeatOn();
537 else{
538 if (DutyPwm > Pwm) HeatOn();
539 else HeatOff();
540 }
541  
542 }
543 else HeatOff();
544  
545 }