1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
| // LCD module bağlantıları
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
// Değişken Atamaları
unsigned int Tpos,pozisyon,proportional,last_proportional,integral,derivative,power_differance;
unsigned char duty;
char volti,volty;
unsigned int IIO,IYO;
float Kp=2,Ki=0.2,Kd=1;
float II,IY;
char *text="000";
char *IIYAZ="000";
char *IYYAZ="000";
// Genel Ayarlar
init(){
ADCON1=0b10001101;
ADCON0=0X85;
Trisa=0xff;
Trisc=0x00;
Trisd=0x00;
Pwm1_Init(1000);
lcd_Init();
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
}
// ADC Okuma, PID Hesaplama
oku(){
IIO=Adc_Read(0);
delay_ms(5);
IYO=Adc_Read(1);
delay_ms(5);
II=(long)IIO*5000;
II=II/1022;
volti=II/100;
IY=(long)IYO*5000;
IY=IY/1022;
volty=IY/100;
if(volti>=volty){
Tpos=volti;
pozisyon=volty;
proportional=Tpos-pozisyon;
derivative=proportional-last_proportional;
integral=integral+proportional;
last_proportional=proportional;
power_differance=proportional*Kp+derivative*Kd+integral*Ki;
if(duty>240) power_differance=128;
duty=power_differance+127;
}
if(volti<volty) {
Tpos=volti;
pozisyon=volty;
proportional=Tpos-pozisyon;
derivative=proportional-last_proportional;
integral=integral+proportional;
last_proportional=proportional;
power_differance=proportional*Kp+derivative*Kd+integral*Ki;
if(duty<10) power_differance=127;
duty=power_differance-127;
}
}
pwm(){
PWM1_Set_duty(duty);
PWM1_start();
}
LCD(){
text[0]=(duty/100)+48;
text[1]=(duty/10)%10+48;
text[2]=duty%10+48;
Lcd_Out(1,13,text);
IIYAZ[0]=(volti/100)+48;
IIYAZ[1]=(volti/10)%10+48;
IIYAZ[2]=volti%10+48;
IYYAZ[0]=(volty/100)+48;
IYYAZ[1]=(volty/10)%10+48;
IYYAZ[2]=volty%10+48;
Lcd_Out(1,1,"INV:");
Lcd_Out(1,5,IIYAZ);
Lcd_Out(2,1,"YUK:");
Lcd_Out(2,5,IYYAZ);
Lcd_Out(1,9,"DTY:");
}
void main() {
init();
while(1){
LCD();
oku();
pwm();
}
} |