#include <16f877.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Low power osc < 200khz
#FUSES NOPUT //No Power Up Timer
#FUSES PROTECT //Code protected from reads
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,stop=1,parity=n) // RS232 protokolu 9600 bit/sn baud hizyida
// TX pin C6, RX pin C7 uclari
// parity biti yok, stop biti 1
#use fast_io(b)
#include <stdlib.h>
#include <math.h>
#include <string.h>
signed int16 pwmoutput=0;
float32 Dt,apoint,pwmfloat,izin,referans_deger,Kp,Ki,Kd,onceki_hata,error,integral,derivative;
int8 s1[4],s2[4],s3[4],s4[4];
int8 girdi_say=0;
int8 girdi;
int16 Bekleme;
#int_ext // Dis kesme
void rbo_kesmesi () {
if(input(pin_b5))
apoint=apoint-0.1;
else
apoint=apoint+0.1;
}
void temizle(){
s1[0]="";
s1[1]="";
s1[2]="";
s1[3]="";
s2[0]="";
s2[1]="";
s2[2]="";
s2[3]="";
s3[0]="";
s3[1]="";
s3[2]="";
s3[3]="";
s4[0]="";
s4[1]="";
s4[2]="";
s4[3]="";
}
void oku(){
girdi=getch();
if(strlen(s1)==0)
s1[0]=girdi;
else if(strlen(s2)==0)
s2[0]=girdi;
else if(strlen(s3)==0)
s3[0]=girdi;
else if(strlen(s4)==0)
s4[0]=girdi;
if(girdi==13){
girdi_say++;
if(girdi_say>5){
printf("\r\n");
printf("\r\n");
printf("\r");
printf("\r Kontrol Parametrelerini tekrar girmek icin Enter`a basiniz...");
girdi_say=0;
}
strcat(s1,s2);
strcat(s1,s3);
strcat(s1,s4);
if(girdi_say==1){
izin=atof(s1);
temizle();
printf("\r Referans Aci Degeri :\n");
} else if(girdi_say==2){
referans_deger=atof(s1);
temizle();
printf("\r Kp: \n");
} else if(girdi_say==3){
Kp=atof(s1);
temizle();
printf("\r Ki: \n");
} else if(girdi_say==4){
Ki=atof(s1);
temizle();
printf("\r Kd: \n");
} else if(girdi_say==5){
Kd=atof(s1);
temizle();
printf("\r Girilen Degerler \n");
printf("\r **************** \n");
printf("\r Referans Aci Degeri : %f \n", referans_deger);
printf("\r Kp Giriniz : %f \n", Kp);
printf("\r Ki Giriniz : %f \n", Ki);
printf("\r Kd Giriniz : %f \n", Kd);
}
}
delay_ms(100);
}
/********* ANA PROGRAM FONKSYYONU********/
void main (){
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP2(CCP_OFF);
setup_ccp1(CCP_PWM); // CCP1 birimi PWM cikis icin ayarlandi
setup_timer_2(T2_DIV_BY_16,255,1); // Timer2 ayarlari yapiliyor
set_pwm1_duty(pwmoutput); //Saykil set ediliyor
set_tris_b(0b00110001); //RB0 Encoder A
//RB5 Encoder B
output_b(0x00);
printf("\r Bitirme Projesi \n");
printf("\r Propeller Kumandali PID Sistem Kontrolu \n");
printf("\r Ocak 2010 \n");
printf("\r");
printf("\r Kontrol Parametrelerini girmek icin Enter`a basiniz...");
ext_int_edge(H_TO_L); //Kesme yukselen kenarda olacak
enable_interrupts(int_ext);
enable_interrupts(GLOBAL); // Aktif edilen tum kesmelere izin veriliyor
/****BASLANGIC DEGERLERINI ATA****/
onceki_hata=0;
integral=0;
/****BASLANGIC DEGERLERINI ATA****/
Dt=0.25; // ms degeri
Bekleme=(Dt*1000);
while(1) {
if(KbHit())
oku();
if(girdi_say==5){
error=(referans_deger-apoint);
integral=integral+(error*Dt);
derivative=(error-onceki_hata)/Dt;
pwmfloat=(kp*error)+(Ki*integral)+(Kd*derivative);
pwmoutput=pwmfloat+450;
onceki_hata=error;
if (pwmoutput>1023)
pwmoutput =1023;
if (pwmoutput<0)
pwmoutput =0;
set_pwm1_duty(pwmoutput);
delay_ms(Bekleme);
printf("\r PWM Duty ( 0 - 1023 ) : %Ld Guncel Konum: %f \n", pwmoutput, apoint);
}
}
}