1 | float pi_regler(float temp_sensor_value,float temperature)
|
2 | {
|
3 |
|
4 | DDRB = (1 << PB1 ); // PB1 als Ausgang für PWM
|
5 | DDRB = (1 << PB2 ); // PB3 als Ausgang für PWM
|
6 |
|
7 | //Einstellungen für WGM-Mode 5 (Fast PWM 8-Bit)
|
8 | //Output Compare mode ist nicht invertierender Modus
|
9 | TCCR1A = 0b10100001; //COM1A1|B1=1 - COM1A0|B0=0 - WGM11=0,WGM10=1
|
10 | TCCR1B = 0b00001011; //WGM13=0 - WGM12=1 - Vorteiler=64
|
11 |
|
12 | //TCCR2 = 0b01001000;
|
13 |
|
14 | //TCCR1A = (1<<WGM10)|(1<<COM1A1); //1<<WGM10 --> 8 Bit PWM //1<<COM1A1 Nicht invertierende PWM
|
15 | //TCCR1B = (1<<CS12) | (1<<CS10); // Takt von CK / 1024
|
16 |
|
17 |
|
18 | float iwert=0,pwert=0,e=0,y=0; //I-Anteil //P-anteil
|
19 | float ki= 1.5; //integrations beiwert(verstärkung der itegration) //Regeldifferenz //Stellgröße
|
20 | float kp = 0.5; //Proportionalanteil
|
21 | float ta = 0.01; //Abtastzeit 10ms
|
22 | float esum=0;
|
23 |
|
24 | //temp_sensor_value = Adwandlung();
|
25 | e = temperature - temp_sensor_value; //aktuelle Regelabweichung bestimmen
|
26 | if (e>50) //e Begrenzen auf +50°
|
27 | {
|
28 | e=50;
|
29 | }
|
30 | if (e<-50) //e begrenzen auf -50°
|
31 | {
|
32 | e=-50;
|
33 | }
|
34 | pwert=(kp*e); //P-Anteil ausrechnen
|
35 | iwert=(ki*ta*esum); //I-Anteil ausrechen
|
36 | y=pwert+iwert; //Stellgröße ausrechnen
|
37 | if ((y<50)&&(y>0)) //Integration nur in diesen bereich ausrechen
|
38 | { //Wenn kein abweichnung da ist Inegration Einfrieren
|
39 | esum=esum+e; //Integration I anteil
|
40 | }
|
41 | if ((y>-50)&&(y<0)) //Wenn stellgröße negativ ist PRÜFEEENNNN!!!!!!!!!!!!!!
|
42 | {
|
43 | esum=esum-e;
|
44 | }
|
45 | if (y==0) //wenn die stellgröße gleich 0 ist
|
46 | {
|
47 | esum=0; //Setz Integration zurück
|
48 | }
|
49 | if (e>0) //wenn e größer 0 ist muss die heizung angehen
|
50 | {
|
51 | OCR1AL = y; //vergleichwert für compare match y
|
52 | //OCR2 = 0; //wenn heizung an soll der lütfter aus sein
|
53 | OCR1BL=0;
|
54 | }
|
55 | if (e<0) //wenn e kleiner 0 ist muss die lüftung angehen
|
56 | {
|
57 | //OCR2 = -y ; //vergleichwert für compare match y
|
58 | OCR1BL=-y;
|
59 | OCR1AL = 0; //wenn lüfter an soll die heizung aus sein
|
60 | }
|
61 |
|
62 | //
|
63 | return (esum);
|
64 | }
|